[1aaa525] | 1 | #!/usr/bin/python |
---|
| 2 | import csv |
---|
| 3 | import os |
---|
| 4 | import sys |
---|
| 5 | |
---|
| 6 | if __name__ == '__main__': |
---|
| 7 | cur_file = os.path.abspath(__file__) |
---|
| 8 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
| 9 | sys.path.append(django_dir) |
---|
| 10 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
| 11 | |
---|
| 12 | from django.core import mail |
---|
| 13 | from django.template import Context, Template |
---|
| 14 | from django.template.loader import get_template |
---|
| 15 | |
---|
| 16 | import groups.models |
---|
| 17 | |
---|
| 18 | def get_roles(): |
---|
[a451497] | 19 | require_any_role = False |
---|
| 20 | require_any_role = True |
---|
[1aaa525] | 21 | roles = [ |
---|
[a451497] | 22 | ['president', require_any_role, 'No president listed', ], |
---|
| 23 | ['treasurer', require_any_role, 'No treasurer listed', ], |
---|
[8cf11c9] | 24 | ['financial', False, 'No financial signatories listed. At minimum, this should generally be your president and treasurer.', ], |
---|
| 25 | ['reservation', False, 'No reservation signatories listed. Members reserving space for the group should be reservation signatories.', ], |
---|
[1aaa525] | 26 | ] |
---|
| 27 | for role in roles: |
---|
| 28 | obj = groups.models.OfficerRole.objects.get(slug=role[0]) |
---|
| 29 | role[0] = obj |
---|
| 30 | return roles |
---|
| 31 | |
---|
| 32 | def check_group(group, roles, ): |
---|
| 33 | problems = [] |
---|
[26fd6cf] | 34 | fail = True |
---|
[1aaa525] | 35 | if group.officer_email or group.description: |
---|
| 36 | pass |
---|
| 37 | else: |
---|
| 38 | problems.append("No basic group information listed") |
---|
[8cf11c9] | 39 | fail = True |
---|
| 40 | for role, cause_fail, msg in roles: |
---|
[1aaa525] | 41 | if len(group.officers(role=role)) == 0: |
---|
| 42 | problems.append(msg) |
---|
[8cf11c9] | 43 | if cause_fail: fail = True |
---|
[26fd6cf] | 44 | problems.append("You have not contacted us about being unsuspended (or should do so again)") |
---|
[8cf11c9] | 45 | return fail, problems |
---|
[1aaa525] | 46 | |
---|
[2a587e4] | 47 | def canonicalize_email(email): |
---|
| 48 | if '@' in email: return email |
---|
| 49 | elif email == '': return '' |
---|
| 50 | else: return email + "@mit.edu" |
---|
| 51 | |
---|
[1aaa525] | 52 | def officers_lists(fd, ): |
---|
| 53 | reader = csv.DictReader(fd) |
---|
| 54 | lists = {} |
---|
| 55 | for d in reader: |
---|
| 56 | pk = int(d['ASA_STUDENT_GROUP_KEY']) |
---|
[2a587e4] | 57 | lists[pk] = canonicalize_email(d['OFFICER_EMAIL']) |
---|
[1aaa525] | 58 | return lists |
---|
| 59 | |
---|
| 60 | def check_groups(old_groups_data): |
---|
| 61 | roles = get_roles() |
---|
| 62 | officers = officers_lists(open(old_groups_data, 'r')) |
---|
| 63 | tmpl = get_template('groups/letters/missing-transition.txt') |
---|
| 64 | emails = [] |
---|
[26fd6cf] | 65 | for group in groups.models.Group.objects.filter(group_status__slug='suspended'): |
---|
[8cf11c9] | 66 | fail, problems = check_group(group, roles, ) |
---|
| 67 | if fail: |
---|
[0dc771d] | 68 | try: |
---|
| 69 | to = [officers[group.pk]] |
---|
| 70 | except KeyError: |
---|
| 71 | print "Group %s not found in CSV" % (group, ) |
---|
[1aaa525] | 72 | if group.officer_email: |
---|
| 73 | to.append(group.officer_email) |
---|
| 74 | ctx = Context({ |
---|
| 75 | 'group': group, |
---|
| 76 | 'problems': problems, |
---|
| 77 | }) |
---|
| 78 | body = tmpl.render(ctx) |
---|
| 79 | email = mail.EmailMessage( |
---|
[0851637] | 80 | subject="[ASA] [Action Required] Missing information for %s" % (group.name, ), |
---|
[1aaa525] | 81 | body=body, |
---|
| 82 | from_email='asa-exec@mit.edu', |
---|
| 83 | to=to, |
---|
[2a587e4] | 84 | bcc=['asa-db-outgoing@mit.edu'], |
---|
[1aaa525] | 85 | ) |
---|
| 86 | emails.append(email) |
---|
| 87 | |
---|
| 88 | connection = mail.get_connection() |
---|
[2a587e4] | 89 | #connection.send_messages(emails) |
---|
[0dc771d] | 90 | for email in emails: |
---|
| 91 | print email.subject |
---|
| 92 | print email.body |
---|
[2a587e4] | 93 | print len(emails) |
---|
[1aaa525] | 94 | |
---|
| 95 | if __name__ == '__main__': |
---|
| 96 | check_groups(sys.argv[1]) |
---|