-
Notifications
You must be signed in to change notification settings - Fork 1
/
goprint-email.py
executable file
·302 lines (248 loc) · 11.9 KB
/
goprint-email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/python
## use yum to install python setup tools
## then, as root, type easy_install pip
## (also as root) pip install psycopg2
import psycopg2
import sys
import os
# imports for actually sending the email
import smtplib
from email.mime.text import MIMEText
# import to check date
from datetime import date, timedelta
# All the database information goes here
DATABASE = '127.0.0.1'
DB_NAME = 'goprint'
USER = 'goprint_email'
try:
PASSWORD = os.environ['GOPRINTPASS']
except KeyError:
print("Environmental variable GOPRINTPASS not found")
sys.exit(1)
# List of lists of users that were contacted
# each follows the form ([Current Balance, Account ID, First Name, Last Name])
USERS_CONTACTED = []
# Setting up variable in order to determine previous
ONE_DAY = timedelta(days=1)
# Contacts the goprint database and grabs all the needed information
def main():
txtfile = 0
try:
txtfile = open("/root/luther/code/goprint-scripts/lastran.txt", "r")
except IOError:
LAST_CONTACTED = str(date.today() - ONE_DAY)
if txtfile != 0:
LAST_CONTACTED = str(txtfile.readline())
print("Last contacted on " + LAST_CONTACTED)
# Define our connection string
conn_string = "host='%s' dbname='%s' user='%s' password='%s'" % (DATABASE, DB_NAME, USER, PASSWORD)
# print the connection string we will use to connect
print("Connecting to %s on %s" % (DB_NAME, DATABASE))
# get a connection, if a connect cannot be made an exception will be raised here
try:
conn = psycopg2.connect(conn_string)
except Exception, e:
print(e)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
print "Connected!\n"
# rows is a list of lists containing all of the account information needed
rows = retrieve_rows_from_table(cursor, True)
if LAST_CONTACTED != str(date.today() - ONE_DAY) and LAST_CONTACTED != str(date.today()):
print "Not up to date"
rows.extend(retrieve_rows_from_table(cursor, False, LAST_CONTACTED))
rows = sorted(rows, key=lambda row: row[0])
parse_through_current_table(rows)
send_report(USERS_CONTACTED)
txtfile = open("/root/luther/code/goprint-scripts/lastran.txt", "w")
txtfile.seek(0)
txtfile.write(str(date.today()))
txtfile.truncate()
txtfile.close()
# Runs a postgreSQL query to grab all the needed information for the rest of the program
def retrieve_rows_from_table(cursor, today, LAST_CONTACTED = "NA"):
if today:
cursor.execute('''
SELECT acct.AccountKey, acct.AccountID, cl.ClassKey, purse.PurseKey,
u.LastName, u.FirstName,
lgr.Trandate, lgr.Debit,
coalesce(ab.Balance,0) + coalesce(ab.PurgedBalance,0) +
coalesce((select sum(Credit-Debit)
FROM tranledger tl
WHERE tl.AccountKey = acct.AccountKey AND tl.PurseKey = purse.PurseKey AND tl.AccountType = 'B'),0) AS Balance
FROM account acct
INNER JOIN goprintuser u ON acct.AccountKey = u.AccountKey
INNER JOIN classmember cm ON acct.AccountKey = cm.AccountKey
INNER JOIN purse purse ON cm.ClassKey = purse.ClassKey AND purse.Active = 'Y'
INNER JOIN class cl ON cm.ClassKey = cl.ClassKey AND cl.ClassType = 'U'
INNER JOIN tranledger lgr ON acct.AccountKey = lgr.AccountKey AND purse.PurseKey = lgr.PurseKey
LEFT JOIN accountbalance ab ON acct.AccountKey = ab.AccountKey AND purse.PurseKey = ab.PurseKey AND ab.AccountType = 'B'
WHERE u.Active = 'Y' AND lgr.Debit > 0.000 AND cl.ClassKey = 20 AND purse.PurseKey = 45
'''
)
else:
cursor.execute('''
SELECT acct.AccountKey, acct.AccountID, cl.ClassKey, purse.PurseKey,
u.LastName, u.FirstName,
hist.Trandate, hist.Debit,
coalesce(ab.Balance,0) + coalesce(ab.PurgedBalance,0) +
coalesce((select sum(Credit-Debit)
FROM tranledger tl
WHERE tl.AccountKey = acct.AccountKey AND tl.PurseKey = purse.PurseKey AND tl.AccountType = 'B'),0) AS Balance
FROM account acct
INNER JOIN goprintuser u ON acct.AccountKey = u.AccountKey
INNER JOIN classmember cm ON acct.AccountKey = cm.AccountKey
INNER JOIN purse purse ON cm.ClassKey = purse.ClassKey AND purse.Active = 'Y'
INNER JOIN class cl ON cm.ClassKey = cl.ClassKey AND cl.ClassType = 'U'
INNER JOIN tranhistory hist ON acct.AccountKey = hist.AccountKey AND purse.PurseKey = hist.PurseKey
LEFT JOIN accountbalance ab ON acct.AccountKey = ab.AccountKey AND purse.PurseKey = ab.PurseKey AND ab.AccountType = 'B'
WHERE u.Active = 'Y' AND hist.Debit > 0.000 AND cl.ClassKey = 20 AND purse.PurseKey = 45 AND hist.updatebyuserid != 'EXTERNAL AUTHENTICATION' AND hist.TranDate > ''' + "'" + LAST_CONTACTED + " 00:00:00.0'"
)
return cursor.fetchall()
# Runs through the table grabbing out all the rows and adding all identical users' debits together
def parse_through_current_table(rows):
account_key = -1
account_id = ''
class_key = 0
purse_key = 0
last_name = ''
first_name = ''
debit = 0
current_balance = -1
txtfile = open("useraccounts.txt", "w")
txtfile.seek(0)
for row in rows:
if account_key == -1:
account_key = row[0]
account_id = row[1]
class_key = row[2]
purse_key = row[3]
last_name = row[4]
# because darn those people who don't have first names for some reason
try:
first_name = row[5].split()[0]
except AttributeError:
first_name = last_name
debit = float(row[7])
current_balance = float(row[8])
elif account_key == row[0] and purse_key == row[3]:
debit = debit + float(row[7])
else:
if current_balance < 0.05 and (current_balance + debit) >= 0.05:
send_email(account_id, last_name, first_name, current_balance, 0)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
elif current_balance < 1.0 and (current_balance + debit) >= 1.0:
send_email(account_id, last_name, first_name, current_balance, 1)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
elif current_balance < 2.0 and (current_balance + debit) >= 2.0:
send_email(account_id, last_name, first_name, current_balance, 2)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
txtfile.write(str(account_key) + " " + account_id + " " + first_name + " " + last_name + " " + str(purse_key) + " " + str(debit) + " " + str(current_balance) + "\n")
account_key = row[0]
account_id = row[1]
class_key = row[2]
purse_key = row[3]
last_name = row[4]
# See line 141
try:
first_name = row[5].split()[0]
except AttributeError:
first_name = last_name
debit = float(row[7])
current_balance = float(row[8])
txtfile.write(str(account_key) + " " + account_id + " " + first_name + " " + last_name + " " + str(purse_key) + " " + str(debit) + " " + str(current_balance) + "\n")
if current_balance < 0.05 and (current_balance + debit) >= 0.05:
send_email(account_id, last_name, first_name, current_balance, 0)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
elif current_balance < 1.0 and (current_balance + debit) >= 1.0:
send_email(account_id, last_name, first_name, current_balance, 1)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
elif current_balance < 2.0 and (current_balance + debit) >= 2.0:
send_email(account_id, last_name, first_name, current_balance, 2)
current_balance = str(round(current_balance,2))
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
USERS_CONTACTED.append([current_balance, account_id, first_name, last_name])
txtfile.truncate()
txtfile.close()
# Default message based on money left in account
def email_msg(first_name, current_balance):
if len(current_balance.split(".")[1]) == 1:
current_balance = current_balance + "0"
msg = '''Hello %s,\n
Your GoPrint balance is currently $%s. Please visit the Office for Financial Services during business hours to add money to your NordiCash account. See http://www.luther.edu/lis/goprint for information on after hours printing.\n
If you have any questions, please contact the LIS Technology Help Desk at 563-387-1000, helpdesk@luther.edu, or enter you request online at https://help.luther.edu''' % (first_name, current_balance)
return msg
# Python alternative to a switch statement
#AMOUNT_LEVEL = {0 : zero_dollar_msg , 1 : one_dollar_msg , 2 : two_dollar_msg}
# As definition implies, this sends an email to the user
def send_email(email, last_name, first_name, current_balance, money_amount):
sender = "helpdesk@luther.edu"
receiver = email + "@luther.edu"
# Modifying current balance so it's not just crazy broken
current_balance = str(round(current_balance,2))
# Create a string to use as the message body.
#message = AMOUNT_LEVEL[money_amount](first_name, current_balance)
message = email_msg(first_name, current_balance)
# Create a text/plain message.
msg = MIMEText(message)
if money_amount != 0:
msg['Subject'] = "GoPrint Balance Alert: Under $%s remaining" % (money_amount)
else:
msg['Subject'] = "GoPrint Balance Alert: Balance at $%s" % (money_amount)
msg['From'] = sender
msg['To'] = receiver
# Setting up smtp with mail server. When left blank it defaults to localhost.
s = smtplib.SMTP()
s.connect()
s.sendmail(sender, [receiver], msg.as_string())
s.quit()
# As definition implies, this sends a report to the HD Managers at the end of each day
def send_report(users):
if len(users) == 0:
print("Sending empty report")
elif len(users) == 1:
print("Sending report of one user contacted today")
else:
print("Sending report of " + str(len(users)) + " users contacted today")
sender = "goprint@luther.edu"
receiver = "hdmanagers@luther.edu"
message = ""
if len(users) > 0:
users = sorted(users, key=lambda user: user[1])
# Create the string for the report email body.
message = "These are all the students who were contacted and their current balances.\n"
for user in users:
message = message + "\n" + "$" + user[0] + (" " * (10 - len(user[0]))) + user[1] + (" " * (15 - len(user[1]))) + user[2] + " " + user[3]
else:
message = "There were no students that crossed any of the thresholds today."
# Create a text/plain message.
msg = MIMEText(message)
if len(users) > 0:
msg['Subject'] = "GoPrint Student Balance Report"
else:
msg['Subject'] = "GoPrint Student Balance Report - EMPTY"
msg['From'] = sender
msg['To'] = receiver + ", gossmand@luther.edu"
# Setting up smtp with mail server. When left blank it defaults to localhost.
s = smtplib.SMTP()
s.connect()
s.sendmail(sender, [receiver, "gossmand@luther.edu"], msg.as_string())
s.quit()
if __name__ == "__main__":
main()