Loading sucsam.py +174 −0 Original line number Diff line number Diff line debugmode = 1 import os import pwd import sys import readline import psycopg2 import datetime import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def correctPaidUntilStr(): #same logic as suapi stuff # if it's greater than Sept then year+1 # if it's less than Spet then year if (month >= 9): paidyear = year +1 elif (month < 8): paidyear = year else: print("Something went really wrong") sys.exit(3) return "Sept. " + str(paidyear) # annoyingly when people have paid until is stored as a string in the DB # as something like "Sept. $YEAR" # so explode the str on " " cast $year to int and compare to correct year def stripFuturePeople(listofpeeps): badpeeps = [] for person in listofpeeps: if person[1] == "delete": badpeeps.append(person) else: paidyeararray = person[1].split(' ') if (month >= 9): paidyear = year + 1 elif (month < 8): paidyear = year else: print("Something went really wrong") sys.exit(3) if int(paidyeararray[1]) < paidyear: badpeeps.append(person) return badpeeps # ok this needs explaning # in comes a list [('user1', 'Sept. 2015'), ('user2', 'Sept. 2014')] # we need to work out what account type it is, # select type from members where username=$peep[0] # then from that find the right email text file and send # easy peasy def sendReminderEmail(listofpeeps): for person in listofpeeps: cur = DBconn.cursor() cur.execute("SELECT * from members WHERE username=%(user)s",{"user" : person[0]}) DBdata = cur.fetchall() cur.close() username = DBdata[0][1] realname = DBdata[0][2] email = DBdata[0][3] type = DBdata[0][7] adminname = pwd.getpwuid(os.geteuid())[0] if os.path.isfile(str(type)+"-email"): file = open(str(type)+"-email","r") data = file.read() file.close() data = data.replace("{$realname}",realname) data = data.replace("{$username}",username) data = data.replace("{$adminname}",adminname) sender = 'staff@sucs.org' if debugmode > 0 receiver = 'imranh@sucs.org' else: receiver = email message = MIMEMultipart() message['From'] = sender message['To'] = receiver message['Subject'] = 'Your SUCS account is due for renewal' messageBody = MIMEText(data, 'plain') message.attach(messageBody) smtpConn = smtplib.SMTP('localhost') smtpConn.sendmail(sender, receiver, message.as_string()) smtpConn.quit() sys.exit(0) else: print("Renewal email template not found!") sys.exit(4) def mainMenu(): print("\nPlease choose an option") print("[h] Diplay some help") print("[qs] View quick stats of all accounts") print("[la] View a list of ALL accounts") print("[r] Renewals & Reminders") print("[d] Delete accounts") print("[q] Quit") option = input("Option: ") Loading @@ -25,6 +114,12 @@ def mainMenu(): elif (option == "la"): listUsers() mainMenu() elif (option == "r"): reminderMenu() mainMenu() elif (option == "d"): deleteAccounts() mainMenu() else: mainMenu() Loading Loading @@ -69,7 +164,78 @@ def listUsers(): for hon in hons: print(str(hon[0]) + " (" + str(hon[1]) + ")") def reminderMenu(): print("") print("Double check https://sucs.org/Admin/SU that everyone that has paid to renew has been marked as renwed. This tool doesn't do that job yet!") print("") print("1. View a list of accounts that aren't marked as renewed in the DB") print("2. Semd email to all students reminding them to renew") print("0. Return to main menu") option = input("Option: ") if (option == "1"): for student in studentsBad: print(str(student[0]) + " because the DB says: " + str(student[1]) ) for soc in soceitiesBad: print(str(soc[0]) + " because the DB says: " + str(soc[1]) ) for ass in associatesBad: print(str(ass[0]) + " because the DB says: " + str(ass[1]) ) reminderMenu() if (option == "2"): sendReminderEmail(studentsBad) if (option == "0"): mainMenu() else: reminderMenu() def deleteMenu(deleteArray): print("") print("1. View list of accounts to be deleted") print("2. Add/delete users from delete list") print("3. Auto populate list of users to be deleted") print("9. Do the delete") print("0. Go back to main menu") option = input("Option: ") if (option == "1"): print("\nThe following users will be deleted: ") for user in deleteArray: print(user) deleteMenu(deleteArray) elif (option == "2"): print("Enter a username to add or remoeve it from the list.") user = input("Username: ") if (user in deleteArray): deleteArray.remove(user) print(str(user) + " won't be deleted.") else: deleteArray.append(user) print(str(user) + " will be deleted.") deleteMenu(deleteArray) elif (option == "3"): for student in studentsBad: print("Adding " + str(student[0]) + " to the list because the DB says: " + str(student[1]) ) deleteArray.append(student[0]) for soc in societiesBad: print("Adding " + str(soc[0]) + " to the list because the DB says: " + str(soc[1]) ) deleteArray.append(soc[0]) deleteMenu(deleteArray) elif (option == "9"): print("Nope not yet") deleteMenu(deleteArray) elif (option == "0"): mainMenu() else: deleteMenu(deleteArray) def deleteAccounts(): #list that store usernames to be deleted #gets reset everytime you go into this bit on purpose tobedeleted = [] deleteMenu(tobedeleted) ### MAIN ### Loading @@ -77,6 +243,11 @@ if (os.geteuid() == 0): print("Don't run this as root!") sys.exit(1) # get some date/time info ready now = datetime.datetime.now() year = now.year month = now.month #This tool was written for python *3* #but doesn't mean we can't support 2 as well :) try: Loading @@ -96,10 +267,13 @@ cur = DBconn.cursor() cur.execute("SELECT username,paid from members WHERE type=1 ORDER BY paid") students = cur.fetchall() studentsBad = stripFuturePeople(students) cur.execute("SELECT username,paid from members WHERE type=2 ORDER BY paid") societies = cur.fetchall() societiesBad = stripFuturePeople(societies) cur.execute("SELECT username,paid from members WHERE type=5 ORDER BY paid") associates = cur.fetchall() associatesBad = stripFuturePeople(associates) cur.execute("SELECT username,paid from members WHERE type=4 ORDER BY username") lifers = cur.fetchall() cur.execute("SELECT username,paid from members WHERE type=3 ORDER BY username") Loading Loading
sucsam.py +174 −0 Original line number Diff line number Diff line debugmode = 1 import os import pwd import sys import readline import psycopg2 import datetime import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def correctPaidUntilStr(): #same logic as suapi stuff # if it's greater than Sept then year+1 # if it's less than Spet then year if (month >= 9): paidyear = year +1 elif (month < 8): paidyear = year else: print("Something went really wrong") sys.exit(3) return "Sept. " + str(paidyear) # annoyingly when people have paid until is stored as a string in the DB # as something like "Sept. $YEAR" # so explode the str on " " cast $year to int and compare to correct year def stripFuturePeople(listofpeeps): badpeeps = [] for person in listofpeeps: if person[1] == "delete": badpeeps.append(person) else: paidyeararray = person[1].split(' ') if (month >= 9): paidyear = year + 1 elif (month < 8): paidyear = year else: print("Something went really wrong") sys.exit(3) if int(paidyeararray[1]) < paidyear: badpeeps.append(person) return badpeeps # ok this needs explaning # in comes a list [('user1', 'Sept. 2015'), ('user2', 'Sept. 2014')] # we need to work out what account type it is, # select type from members where username=$peep[0] # then from that find the right email text file and send # easy peasy def sendReminderEmail(listofpeeps): for person in listofpeeps: cur = DBconn.cursor() cur.execute("SELECT * from members WHERE username=%(user)s",{"user" : person[0]}) DBdata = cur.fetchall() cur.close() username = DBdata[0][1] realname = DBdata[0][2] email = DBdata[0][3] type = DBdata[0][7] adminname = pwd.getpwuid(os.geteuid())[0] if os.path.isfile(str(type)+"-email"): file = open(str(type)+"-email","r") data = file.read() file.close() data = data.replace("{$realname}",realname) data = data.replace("{$username}",username) data = data.replace("{$adminname}",adminname) sender = 'staff@sucs.org' if debugmode > 0 receiver = 'imranh@sucs.org' else: receiver = email message = MIMEMultipart() message['From'] = sender message['To'] = receiver message['Subject'] = 'Your SUCS account is due for renewal' messageBody = MIMEText(data, 'plain') message.attach(messageBody) smtpConn = smtplib.SMTP('localhost') smtpConn.sendmail(sender, receiver, message.as_string()) smtpConn.quit() sys.exit(0) else: print("Renewal email template not found!") sys.exit(4) def mainMenu(): print("\nPlease choose an option") print("[h] Diplay some help") print("[qs] View quick stats of all accounts") print("[la] View a list of ALL accounts") print("[r] Renewals & Reminders") print("[d] Delete accounts") print("[q] Quit") option = input("Option: ") Loading @@ -25,6 +114,12 @@ def mainMenu(): elif (option == "la"): listUsers() mainMenu() elif (option == "r"): reminderMenu() mainMenu() elif (option == "d"): deleteAccounts() mainMenu() else: mainMenu() Loading Loading @@ -69,7 +164,78 @@ def listUsers(): for hon in hons: print(str(hon[0]) + " (" + str(hon[1]) + ")") def reminderMenu(): print("") print("Double check https://sucs.org/Admin/SU that everyone that has paid to renew has been marked as renwed. This tool doesn't do that job yet!") print("") print("1. View a list of accounts that aren't marked as renewed in the DB") print("2. Semd email to all students reminding them to renew") print("0. Return to main menu") option = input("Option: ") if (option == "1"): for student in studentsBad: print(str(student[0]) + " because the DB says: " + str(student[1]) ) for soc in soceitiesBad: print(str(soc[0]) + " because the DB says: " + str(soc[1]) ) for ass in associatesBad: print(str(ass[0]) + " because the DB says: " + str(ass[1]) ) reminderMenu() if (option == "2"): sendReminderEmail(studentsBad) if (option == "0"): mainMenu() else: reminderMenu() def deleteMenu(deleteArray): print("") print("1. View list of accounts to be deleted") print("2. Add/delete users from delete list") print("3. Auto populate list of users to be deleted") print("9. Do the delete") print("0. Go back to main menu") option = input("Option: ") if (option == "1"): print("\nThe following users will be deleted: ") for user in deleteArray: print(user) deleteMenu(deleteArray) elif (option == "2"): print("Enter a username to add or remoeve it from the list.") user = input("Username: ") if (user in deleteArray): deleteArray.remove(user) print(str(user) + " won't be deleted.") else: deleteArray.append(user) print(str(user) + " will be deleted.") deleteMenu(deleteArray) elif (option == "3"): for student in studentsBad: print("Adding " + str(student[0]) + " to the list because the DB says: " + str(student[1]) ) deleteArray.append(student[0]) for soc in societiesBad: print("Adding " + str(soc[0]) + " to the list because the DB says: " + str(soc[1]) ) deleteArray.append(soc[0]) deleteMenu(deleteArray) elif (option == "9"): print("Nope not yet") deleteMenu(deleteArray) elif (option == "0"): mainMenu() else: deleteMenu(deleteArray) def deleteAccounts(): #list that store usernames to be deleted #gets reset everytime you go into this bit on purpose tobedeleted = [] deleteMenu(tobedeleted) ### MAIN ### Loading @@ -77,6 +243,11 @@ if (os.geteuid() == 0): print("Don't run this as root!") sys.exit(1) # get some date/time info ready now = datetime.datetime.now() year = now.year month = now.month #This tool was written for python *3* #but doesn't mean we can't support 2 as well :) try: Loading @@ -96,10 +267,13 @@ cur = DBconn.cursor() cur.execute("SELECT username,paid from members WHERE type=1 ORDER BY paid") students = cur.fetchall() studentsBad = stripFuturePeople(students) cur.execute("SELECT username,paid from members WHERE type=2 ORDER BY paid") societies = cur.fetchall() societiesBad = stripFuturePeople(societies) cur.execute("SELECT username,paid from members WHERE type=5 ORDER BY paid") associates = cur.fetchall() associatesBad = stripFuturePeople(associates) cur.execute("SELECT username,paid from members WHERE type=4 ORDER BY username") lifers = cur.fetchall() cur.execute("SELECT username,paid from members WHERE type=3 ORDER BY username") Loading