forked from matfystutor/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutormail2017.py
124 lines (95 loc) · 3.62 KB
/
tutormail2017.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
# encoding: utf8
from __future__ import unicode_literals
'''
1. Læs email2017.txt igennem for fejl (og datoer osv.) - og fiks dem
2. Fjern udkommentering af "u.save()" i set_passwords_2017.py og kør den
3. Fjern udkommentering af send_messages i denne fil og kør den
'''
import os
import json
import codecs
os.environ['DJANGO_SETTINGS_MODULE'] = 'mftutor.settings'
import django
django.setup()
from django.template import Template, Context
from django.template.loader import get_template
from django.core.mail import EmailMessage
from mftutor.tutor.models import TutorProfile, Tutor, TutorGroup
email_backend_type = 'django.core.mail.backends.smtp.EmailBackend'
def send_messages(messages, backend_type):
from django.core.mail import get_connection
email_backend = get_connection(backend=backend_type)
return email_backend.send_messages(messages)
YEAR = 2017
def main():
with open('passwords_2017.json') as fp:
passwords = json.load(fp)
emails = []
for tutor in Tutor.members(2017):
tp = tutor.profile
groups = tutor.groups.filter(visible=True)
arbejdstutor = any(g.handle == 'arbejdstutor' for g in groups)
buret = any(g.handle == 'buret' for g in groups)
normal_groups = [g for g in groups
if g.handle not in ('arbejdstutor', 'buret')]
group_names = [g.name for g in normal_groups]
ansv_names = [g.name for g in normal_groups if g.leader == tutor]
context = dict(
navn=tp.name.strip(),
year=YEAR,
studentnumber=tp.studentnumber,
groups=' og '.join(group_names),
webfar='Alexandra Hou',
password=passwords[tp.studentnumber],
group=' og '.join(ansv_names),
)
# afvist.txt: navn, year
# ansv.txt: navn, year, studentnumber, password, groups, group, webfar
# buret.txt: navn, groups, studentnumber, password, webfar
# buretansv.txt: navn, groups, studentnumber, password, group, webfar
# tutors.txt: navn, year, studentnumber, password, groups, webfar
if buret:
if ansv_names:
tpl = 'buretansv.txt'
subject = 'Du er blevet gruppeansvarlig!'
else:
tpl = 'buret.txt'
subject = 'Du er blevet tutor!'
elif arbejdstutor:
if ansv_names:
tpl = 'arbejdansv.txt'
subject = 'Du er blevet gruppeansvarlig!'
else:
tpl = 'arbejd.txt'
subject = 'Du er blevet tutor!'
else:
if ansv_names:
tpl = 'ansv.txt'
subject = 'Du er blevet gruppeansvarlig!'
else:
tpl = 'tutors.txt'
subject = 'Du er blevet tutor!'
tpl = get_template('emails/%s' % tpl)
email_body = tpl.render(context)
email = EmailMessage(
subject=subject,
body=email_body,
from_email='"Alexandra Hou" <[email protected]>',
to=[tp.email],
)
emails.append(email)
# emails = [
# email
# for email in emails
# if '[email protected]' in email.to
# ]
with codecs.open('email2017.txt', 'w', encoding='utf-8') as fp:
for email in emails:
fp.write(79*'=' + '\n')
fp.write("Fra: %s\n" % email.from_email)
fp.write("Til: %s\n" % email.to[0])
fp.write("Emne: %s\n" % email.subject)
fp.write('\n%s\n' % (email.body))
# send_messages(emails, email_backend_type)
if __name__ == '__main__':
main()