1 | #!/usr/bin/python |
---|
2 | import sys |
---|
3 | import os |
---|
4 | import datetime |
---|
5 | |
---|
6 | from django.template import Context, Template |
---|
7 | from django.template.loader import get_template |
---|
8 | from django.core.urlresolvers import reverse |
---|
9 | from django.core.mail import EmailMessage |
---|
10 | |
---|
11 | if __name__ == '__main__': |
---|
12 | cur_file = os.path.abspath(__file__) |
---|
13 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
14 | django_dir_parent = os.path.abspath(os.path.join(os.path.dirname(cur_file), '../..')) |
---|
15 | sys.path.append(django_dir) |
---|
16 | sys.path.append(django_dir_parent) |
---|
17 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
18 | |
---|
19 | import forms.models |
---|
20 | |
---|
21 | LEVEL_HIGH = 'high' |
---|
22 | LEVEL_MEDIUM = 'medium' |
---|
23 | LEVEL_LOW = 'low' |
---|
24 | |
---|
25 | def check_display_name(fysm): |
---|
26 | if fysm.display_name[-5:] == ', MIT': |
---|
27 | return { |
---|
28 | 'heading': 'Undesirable MIT ending to display name', |
---|
29 | 'body': 'It is rarely if ever desirable to end the display name of a group with ", MIT". You may wish to move it to the beginning, or simply remove it entirely.', |
---|
30 | 'level': LEVEL_MEDIUM, |
---|
31 | } |
---|
32 | if ', ' in fysm.display_name: |
---|
33 | return { |
---|
34 | 'heading': 'Potentially undesirable comma in display name', |
---|
35 | 'body': 'You have a comma in your display name. Frequently this is to move some common word to the end of the group name. You may wish to move such a common word to the front, or leave it out of your advertising entirely.', |
---|
36 | 'level': LEVEL_LOW, |
---|
37 | } |
---|
38 | |
---|
39 | def check_image_field(fysm, label, image): |
---|
40 | good_ext = ['png', 'jpg', 'jpeg', 'gif', ] |
---|
41 | ext = image.name.rsplit('.')[-1] |
---|
42 | convert_msg = 'You should convert it to a PNG (or possibly GIF, if it is essentially all text; or JPEG, if it is mostly images).' |
---|
43 | if not image.name: |
---|
44 | return { |
---|
45 | 'heading': 'No %s provided' % (label, ), |
---|
46 | 'body': 'You did not upload a %s. We strongly recommended uploading one, since it can help new students to recognize and understand your group.' % (label, ), |
---|
47 | 'level': LEVEL_MEDIUM, |
---|
48 | } |
---|
49 | if ext.lower() in ['tif', 'tiff', ]: |
---|
50 | return { |
---|
51 | 'heading': 'Undesirable TIFF file submitted for %s' % (label, ), |
---|
52 | 'body': ('You submitted a TIFF file for your %s. TIFF files are generally large and poorly supported by browsers. ' % (label, )) + convert_msg, |
---|
53 | 'level': LEVEL_HIGH, |
---|
54 | } |
---|
55 | if ext.lower() in ['bmp', ]: |
---|
56 | return { |
---|
57 | 'heading': 'Undesirable BMP file submitted for %s' % (label, ), |
---|
58 | 'body': ('You submitted a BMP file for your %s. BMP files are generally extremely large. ' % (label, )) + convert_msg, |
---|
59 | 'level': LEVEL_HIGH, |
---|
60 | } |
---|
61 | if not ext.lower() in good_ext: |
---|
62 | print fysm.display_name, image.name, ext, label, |
---|
63 | return { |
---|
64 | 'heading': ('Unusual file extension .%s submitted for %s' % (ext, label, )), |
---|
65 | 'body': ('You submitted a .%s file for your %s. This is not one of the extensions that we see very often, which makes it seem somewhat likely to be a bad idea. ' % (ext, label, )) + convert_msg, |
---|
66 | 'level': LEVEL_MEDIUM, |
---|
67 | } |
---|
68 | |
---|
69 | checks = [ |
---|
70 | check_display_name, |
---|
71 | lambda f: check_image_field(f, 'logo', f.logo,), |
---|
72 | lambda f: check_image_field(f, 'slide', f.slide,), |
---|
73 | ] |
---|
74 | |
---|
75 | def run_checks(obj): |
---|
76 | results = [] |
---|
77 | for check in checks: |
---|
78 | result = check(obj) |
---|
79 | if result: |
---|
80 | results.append(result) |
---|
81 | return results |
---|
82 | |
---|
83 | def send_emails(objs, template, sender, bcc_recipient, subject_func, recipient_func, ): |
---|
84 | for obj in objs: |
---|
85 | results = run_checks(obj) |
---|
86 | if len(results)>0: |
---|
87 | subject = subject_func(obj) |
---|
88 | ctx = Context({ |
---|
89 | 'obj': obj, |
---|
90 | 'results': results, |
---|
91 | }) |
---|
92 | body = tmpl.render(ctx) |
---|
93 | email = EmailMessage( |
---|
94 | subject=subject, |
---|
95 | body=body, |
---|
96 | from_email=sender, |
---|
97 | to=recipient_func(obj), |
---|
98 | bcc=[bcc_recipient,] |
---|
99 | ) |
---|
100 | print subject, results |
---|
101 | email.send() |
---|
102 | |
---|
103 | if __name__ == '__main__': |
---|
104 | from optparse import OptionParser |
---|
105 | parser = OptionParser() |
---|
106 | parser.add_option("-s", "--sender", dest="sender", |
---|
107 | help="send messages from EMAIL", metavar="EMAIL") |
---|
108 | parser.add_option("-b", "--bcc-recipient", dest="bcc_recipient", |
---|
109 | help="BCC messages to EMAIL", metavar="EMAIL") |
---|
110 | parser.add_option("-o", "--override-recipient", dest="override_recipient", |
---|
111 | help="send to EMAIL instead of group emails", metavar="EMAIL") |
---|
112 | parser.add_option("-y", "--year", dest="year", |
---|
113 | help="process FYSM entries for YEAR", metavar="YEAR") |
---|
114 | parser.set_defaults( |
---|
115 | sender="asa-fysm@mit.edu", |
---|
116 | bcc_recipient='asa-fysm-submissions@mit.edu', |
---|
117 | override_recipient=None, |
---|
118 | year=datetime.date.today().year, |
---|
119 | ) |
---|
120 | (options, args) = parser.parse_args() |
---|
121 | |
---|
122 | if options.override_recipient: |
---|
123 | recipient_func = lambda fysm: [ options.override_recipient, ] |
---|
124 | else: |
---|
125 | recipient_func = lambda fysm: set([ fysm.contact_email, fysm.group.officer_email, ]) |
---|
126 | tmpl = get_template('fysm/lint_email.txt') |
---|
127 | send_emails( |
---|
128 | objs=forms.models.FYSM.objects.filter(year=int(options.year),), |
---|
129 | template=tmpl, |
---|
130 | sender=options.sender, |
---|
131 | bcc_recipient=options.bcc_recipient, |
---|
132 | subject_func=lambda f: "FYSM submission for %s" % (f.display_name, ), |
---|
133 | recipient_func=recipient_func, |
---|
134 | ) |
---|