Commit 69614cb9 authored by Fabian Gabel's avatar Fabian Gabel
Browse files

add classlist-generator

parent 83b405d0
import mysql.connector
import getpass
import sys
import mysql-config
from studip import Studip
#useful references:
# https://www.w3schools.com/python/python_mysql_update.asp
def generateClasslist(mydb):
mycursor = mydb.cursor()
print('Generating Classlist...')
# query database
mycursor.execute("SELECT student_id, last_name, first_name, status, comment, section, recitation, email_address, user_id, password, permission FROM classlist")
myresult = mycursor.fetchall()
# write classlist file, see https://webwork.maa.org/wiki/Classlist_Files#Example_classlist
fo = open("class.lst", "w")
line_template = '{}' + ',{}'*9 + ',{}' '\n'
for res in myresult:
line = line_template.format(*res)
fo.write(line)
fo.close()
print('Done.')
def sendLogin(mydb,lang='en'):
"""Send login data to students in database
Keyword arguments:
mydb -- mysql database object
lang -- language of the template files (default 'en')
"""
input('Sending Login-Mails. Press ENTER to continue...' )
# prepare template files and subject
switch(lang){
if (lang== 'de'):
template_file='password_template_de.txt'
subject='[WeBWorK] Wilkommen bei WeBWorK!'
elif (lang=='en'):
template_file='password_template_en.txt'
subject='[WeBWorK] Welcome to WeBWorK!'
else:
sys.exit(1)
# login at stud.IP
s = Studip(verbose=3)
login = input('Kerberos Login: ')
pswd = getpass.getpass('Kerberos Password: ')
if not s.login(login, pswd):
sys.exit(1)
# query database
mycursor = mydb.cursor()
#mailstatus=0 -> just registered
mycursor.execute("SELECT student_id, first_name, last_name, user_id FROM classlist WHERE mailstatus=0")
myresult = mycursor.fetchall()
# send mails
for res in myresult:
with open(template_file) as mail_file:
print('Mail for Database Entry: ' + res)
kerberos=res[0]
text = mail_file.read().format(*res[1:])
print(subject)
print(text)
input('Press ENTER to send Mail to ' + kerberos + ' ...' )
s.message_crsid(kerberos, subject, text, {'tags': 'WeBWorK'})
mycursor.execute("UPDATE classlist SET mailstatus=1 WHERE student_id='" + kerberos + "'")
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
print('Done.')
def sendNotification(mydb,template_file,subject,fields='student_id',user_idstatus=-1,update_status=-1):
"""Send notification to students in database
Keyword arguments:
mydb -- mysql database object
template_file -- text file with notification
subject -- mail subject
fields -- mydb fields to be queried (default='student_id')
status -- status parameter in database to target specific students (default =-1)
update_status -- status parameter in database to update after mail has been sent (default =-1)
"""
#
#todo check if student_id was provided
#
input('Sending Notification-Mails. Press ENTER to continue...' )
# login at stud.IP
s = Studip(verbose=3)
login = input('Kerberos Login: ')
pswd = getpass.getpass('Kerberos Password: ')
if not s.login(login, pswd):
sys.exit(1)
# query database
mycursor = mydb.cursor()
if status != -1:
mycursor.execute("SELECT " + fields + " FROM classlist WHERE mailstatus=" + status)
else:
mycursor.execute("SELECT " + fields + " FROM classlist")
myresult = mycursor.fetchall()
# send mails
for res in myresult:
with open(template_file) as mail_file:
print('Mail for Database Entry: ' + res)
kerberos=res[0]
text = mail_file.read().format(*res[1:])
print(subject)
print(text)
input('Press ENTER to send Mail to ' + kerberos + ' ...' )
s.message_crsid(kerberos, subject, text, {'tags': 'WeBWorK'})
# update database with update_status
if update_status != -1:
mycursor.execute("UPDATE classlist SET mailstatus=" + update_status + " WHERE student_id='" + kerberos + "'")
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
print('Done.')
#main two routines
generateClasslist(mydb)
sendLogin(mydb)
Dear {} {},
with this mail, you receive your access data for WebWorK.
Your **Username** is:
[code]{}[/code]
With this, you can log in at our site
https://webwork.fabian-gabel.de/webwork2/rep-la-en/
Your **Password** is the same as your **Username**, see above.
After completing the tasks, we would appreciate it if you could take part in our short survey (takes less than one minute). You will find the link to the survey at the bottom of this week's tasks.
If something doesn't work out, feel free to contact me!
Best regards,
Fabian Gabel
PS: If you know other students who are interested in testing WeBWorK, you can refer to my Stud.IP announcement:
https://bit.ly/3fTN79b
There is a link where you can request your login.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment