[b6c7a44] | 1 | #!/usr/bin/python |
---|
| 2 | import collections |
---|
| 3 | import datetime |
---|
| 4 | import os |
---|
| 5 | import sys |
---|
| 6 | |
---|
| 7 | if __name__ == '__main__': |
---|
| 8 | cur_file = os.path.abspath(__file__) |
---|
| 9 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
| 10 | sys.path.append(django_dir) |
---|
| 11 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
| 12 | |
---|
| 13 | from django.contrib.contenttypes.models import ContentType |
---|
| 14 | from django.core.mail import EmailMessage, mail_admins |
---|
| 15 | from django.db import connection |
---|
| 16 | from django.db.models import Q |
---|
| 17 | from django.template import Context, Template |
---|
| 18 | from django.template.loader import get_template |
---|
| 19 | |
---|
| 20 | import reversion.models |
---|
| 21 | |
---|
| 22 | import groups.models |
---|
| 23 | import settings |
---|
[4098256] | 24 | import util.emails |
---|
| 25 | import util.mailman |
---|
[b6c7a44] | 26 | |
---|
| 27 | update_asa_exec = 'asa-exec@mit.edu' |
---|
| 28 | update_funding_board = 'asa-db@mit.edu' |
---|
| 29 | update_constitution_archive = 'asa-db@mit.edu' |
---|
| 30 | |
---|
[ea217bd] | 31 | if settings.PRODUCTION_DEPLOYMENT: |
---|
| 32 | asa_all_groups_list = util.mailman.MailmanList('asa-official') |
---|
| 33 | else: |
---|
| 34 | asa_all_groups_list = util.mailman.MailmanList('asa-test-mailman') |
---|
[4098256] | 35 | |
---|
[b6c7a44] | 36 | class DiffCallback(object): |
---|
| 37 | def start_run(self, since, now, ): |
---|
| 38 | pass |
---|
| 39 | def end_run(self, ): |
---|
| 40 | pass |
---|
| 41 | def handle_group(self, before, after, before_fields, after_fields, ): |
---|
| 42 | pass |
---|
[73f0faf] | 43 | def handle_signatories(self, signatories, ): |
---|
| 44 | pass |
---|
[b6c7a44] | 45 | def new_group(self, after, after_fields, ): |
---|
| 46 | pass |
---|
| 47 | |
---|
| 48 | class StaticMailCallback(DiffCallback): |
---|
| 49 | def __init__(self, fields, address, template, signatories=[]): |
---|
| 50 | self.fields = fields |
---|
| 51 | self.address = address |
---|
| 52 | self.template = template |
---|
| 53 | self.interesting_signatories = signatories |
---|
[435ceab] | 54 | self.care_about_groups = True |
---|
| 55 | self.care_about_signatories = True |
---|
[b6c7a44] | 56 | |
---|
| 57 | def start_run(self, since, now, ): |
---|
| 58 | self.updates = [] |
---|
| 59 | self.signatory_updates = [] |
---|
| 60 | self.signatory_type_counts = { |
---|
[9a30b56] | 61 | 'Added': collections.defaultdict(lambda: 0), |
---|
| 62 | 'Expired': collections.defaultdict(lambda: 0), |
---|
[b6c7a44] | 63 | } |
---|
[8254020] | 64 | self.signatory_types_seen = set() |
---|
[b6c7a44] | 65 | self.since = since |
---|
| 66 | self.now = now |
---|
| 67 | |
---|
| 68 | def handle_group(self, before, after, before_fields, after_fields, ): |
---|
| 69 | after_revision = after.revision |
---|
| 70 | update = "Group: %s (ID #%d)\n" % (after_fields['name'], after_fields['id'], ) |
---|
| 71 | update += "At %s by %s (and possibly other people or times)\n" % ( |
---|
| 72 | after_revision.date_created, after_revision.user, ) |
---|
| 73 | for field in self.fields: |
---|
| 74 | if before_fields[field] != after_fields[field]: |
---|
| 75 | update += "%s: '%s' -> '%s'\n" % ( |
---|
| 76 | field, before_fields[field], after_fields[field], ) |
---|
| 77 | self.updates.append(update) |
---|
| 78 | |
---|
| 79 | def handle_signatories(self, signatories, ): |
---|
[7654a6d] | 80 | prev_group = None |
---|
[b6c7a44] | 81 | for signatory in signatories: |
---|
| 82 | if signatory.end_time > self.now: |
---|
| 83 | change_type = "Added" |
---|
| 84 | else: |
---|
| 85 | change_type = "Expired" |
---|
| 86 | counter = self.signatory_type_counts[change_type] |
---|
[9a30b56] | 87 | counter[signatory.role.slug] += 1 |
---|
[8254020] | 88 | self.signatory_types_seen.add(signatory.role.slug) |
---|
[b6c7a44] | 89 | if signatory.role.slug in self.interesting_signatories: |
---|
[7654a6d] | 90 | if signatory.group != prev_group: |
---|
| 91 | self.signatory_updates.append("") |
---|
[b6c7a44] | 92 | self.signatory_updates.append( |
---|
| 93 | "%s: %s: %s: %s:\n\trange %s to %s" % ( |
---|
| 94 | change_type, |
---|
| 95 | signatory.group, |
---|
| 96 | signatory.role, |
---|
| 97 | signatory.person, |
---|
| 98 | signatory.start_time.strftime(settings.DATETIME_FORMAT_PYTHON), |
---|
| 99 | signatory.end_time.strftime(settings.DATETIME_FORMAT_PYTHON), |
---|
| 100 | )) |
---|
[7654a6d] | 101 | prev_group = signatory.group |
---|
[b6c7a44] | 102 | else: |
---|
[8254020] | 103 | pass |
---|
[4091199] | 104 | #print "Ignoring role %s (signatory %s)" % (signatory.role.slug, signatory, ) |
---|
[b6c7a44] | 105 | |
---|
[8254020] | 106 | def build_change_stats(self, ): |
---|
| 107 | lines = [] |
---|
| 108 | care_about = 0 |
---|
| 109 | |
---|
| 110 | line = "%20s" % ("", ) |
---|
| 111 | change_types = self.signatory_type_counts.keys() |
---|
| 112 | for change_type in change_types: |
---|
| 113 | line += "\t%s" % (change_type, ) |
---|
| 114 | lines.append(line); line = "" |
---|
| 115 | |
---|
| 116 | for sig_type in self.signatory_types_seen: |
---|
[b2cee30] | 117 | anno_sig = sig_type |
---|
| 118 | if sig_type in self.interesting_signatories: |
---|
| 119 | anno_sig += " " |
---|
| 120 | else: |
---|
| 121 | anno_sig += "*" |
---|
| 122 | line += "%20s" % (anno_sig, ) |
---|
[8254020] | 123 | for change_type in change_types: |
---|
| 124 | if sig_type in self.signatory_type_counts[change_type]: |
---|
| 125 | count = self.signatory_type_counts[change_type][sig_type] |
---|
| 126 | else: |
---|
| 127 | count = 0 |
---|
| 128 | if sig_type in self.interesting_signatories: |
---|
| 129 | care_about += count |
---|
| 130 | out = "\t%4d" % (count, ) |
---|
| 131 | line += out |
---|
| 132 | lines.append(line); line = "" |
---|
| 133 | |
---|
[b2cee30] | 134 | line = "* Details for this signatory type not included in email." |
---|
| 135 | lines.append(line) |
---|
| 136 | |
---|
[8254020] | 137 | return "\n".join(lines), care_about |
---|
| 138 | |
---|
[b6c7a44] | 139 | def end_run(self, ): |
---|
[8254020] | 140 | change_stats, care_about = self.build_change_stats() |
---|
[4091199] | 141 | print "\nChange stats for email to %s:" % (self.address, ) |
---|
[8254020] | 142 | print change_stats |
---|
[4091199] | 143 | |
---|
[b6c7a44] | 144 | message = "\n\n".join(self.updates) |
---|
| 145 | signatories_message = "\n".join(self.signatory_updates) |
---|
[435ceab] | 146 | if (self.care_about_groups and self.updates) or (self.care_about_signatories and self.signatory_updates): |
---|
[b6c7a44] | 147 | pass |
---|
| 148 | else: |
---|
| 149 | return |
---|
| 150 | tmpl = get_template(self.template) |
---|
| 151 | ctx = Context({ |
---|
| 152 | 'num_groups': len(self.updates), |
---|
| 153 | 'groups_message': message, |
---|
[8254020] | 154 | 'care_about': care_about, |
---|
| 155 | 'change_stats': change_stats, |
---|
[b6c7a44] | 156 | 'signatory_types': self.interesting_signatories, |
---|
| 157 | 'signatories_message': signatories_message, |
---|
| 158 | }) |
---|
| 159 | body = tmpl.render(ctx) |
---|
| 160 | email = EmailMessage( |
---|
| 161 | subject='ASA Database Updates', |
---|
| 162 | body=body, |
---|
| 163 | from_email='asa-db@mit.edu', |
---|
| 164 | to=[self.address, ], |
---|
| 165 | bcc=['asa-db-outgoing@mit.edu', ] |
---|
| 166 | ) |
---|
| 167 | email.send() |
---|
| 168 | self.updates = [] |
---|
| 169 | self.signatory_updates = [] |
---|
| 170 | |
---|
[4098256] | 171 | |
---|
| 172 | class UpdateOfficerListCallback(DiffCallback): |
---|
| 173 | def start_run(self, since, now, ): |
---|
| 174 | self.add = [] |
---|
| 175 | self.delete = [] |
---|
[74e9f09] | 176 | self.notes = [] |
---|
[4098256] | 177 | |
---|
| 178 | def end_run(self, ): |
---|
| 179 | if self.add or self.delete: |
---|
| 180 | errors = asa_all_groups_list.change_members(self.add, self.delete) |
---|
| 181 | subject = "asa-official updater" |
---|
| 182 | if errors: |
---|
| 183 | subject = "ERROR: " + subject |
---|
| 184 | context = { |
---|
[ea217bd] | 185 | 'listname': asa_all_groups_list.name, |
---|
[4098256] | 186 | 'add': self.add, |
---|
| 187 | 'delete': self.delete, |
---|
| 188 | 'errors': errors, |
---|
[74e9f09] | 189 | 'notes': self.notes, |
---|
[4098256] | 190 | } |
---|
| 191 | util.emails.email_from_template( |
---|
| 192 | tmpl='groups/diffs/asa-official-update.txt', |
---|
| 193 | context=context, subject=subject, |
---|
[281891e] | 194 | to=['asa-db@mit.edu'], |
---|
[4098256] | 195 | ).send() |
---|
| 196 | |
---|
| 197 | def handle_group(self, before, after, before_fields, after_fields, ): |
---|
[74e9f09] | 198 | before_addr = before_fields['officer_email'] |
---|
| 199 | after_addr = after_fields['officer_email'] |
---|
| 200 | if before_addr != after_addr: |
---|
[fb76571] | 201 | name = after_fields['name'] |
---|
[74e9f09] | 202 | if after_addr: |
---|
| 203 | self.add.append((name, after_addr, )) |
---|
| 204 | else: |
---|
| 205 | self.notes.append("%s: Not adding because address is blank." % (name, )) |
---|
| 206 | if before_addr and after_addr: |
---|
| 207 | # Don't remove an address unless there's a replacement |
---|
| 208 | self.delete.append(before_fields['officer_email']) |
---|
| 209 | else: |
---|
| 210 | self.notes.append("%s: Not removing '%s' (to add '%s') because at least one is blank." % (name, before_addr, after_addr, )) |
---|
[4098256] | 211 | |
---|
| 212 | def new_group(self, after, after_fields, ): |
---|
| 213 | self.add.append(after_fields['officer_email']) |
---|
| 214 | |
---|
| 215 | |
---|
[b6c7a44] | 216 | diff_fields = { |
---|
| 217 | 'name' : [ update_asa_exec, ], |
---|
| 218 | 'abbreviation' : [ update_asa_exec, ], |
---|
| 219 | 'officer_email' : [ update_asa_exec, update_funding_board ], |
---|
| 220 | 'constitution_url': [ update_asa_exec, update_constitution_archive ], |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | def build_callbacks(): |
---|
| 224 | callbacks = [] |
---|
| 225 | callbacks.append(StaticMailCallback( |
---|
| 226 | fields=['name', 'abbreviation', 'officer_email', 'constitution_url', ], |
---|
[7654a6d] | 227 | address='asa-admin@mit.edu', |
---|
[b6c7a44] | 228 | template='groups/diffs/asa-update-mail.txt', |
---|
[99747bd] | 229 | signatories=['president', 'treasurer', 'financial', 'group-admin', 'temp-admin', ] |
---|
[b6c7a44] | 230 | )) |
---|
[435ceab] | 231 | sao_callback = StaticMailCallback( |
---|
| 232 | fields=['name', 'abbreviation', 'officer_email', ], |
---|
| 233 | address='funds@mit.edu', |
---|
| 234 | template='groups/diffs/sao-update-mail.txt', |
---|
| 235 | signatories=['president', 'treasurer', 'financial', ] |
---|
| 236 | ) |
---|
| 237 | sao_callback.care_about_groups = False |
---|
| 238 | callbacks.append(sao_callback) |
---|
[4098256] | 239 | callbacks.append(UpdateOfficerListCallback()) |
---|
[b6c7a44] | 240 | return callbacks |
---|
| 241 | |
---|
| 242 | def recent_groups(since): |
---|
| 243 | group_type = ContentType.objects.get_by_natural_key(app_label='groups', model='group') |
---|
| 244 | revisions = reversion.models.Revision.objects.filter(date_created__gte=since) |
---|
| 245 | versions = reversion.models.Version.objects.filter(revision__in=revisions, content_type=group_type) |
---|
| 246 | objs = versions.values("content_type", "object_id").distinct() |
---|
| 247 | return objs |
---|
| 248 | |
---|
[4091199] | 249 | def diff_objects(objs, since, callbacks, stats, ): |
---|
[b6c7a44] | 250 | revs = reversion.models.Revision.objects.all() |
---|
| 251 | old_revs = revs.filter(date_created__lte=since) |
---|
| 252 | new_revs = revs.filter(date_created__gte=since) |
---|
| 253 | for obj in objs: |
---|
| 254 | all_versions = reversion.models.Version.objects.filter(content_type=obj['content_type'], object_id=obj['object_id']).order_by('-revision__date_created') |
---|
| 255 | before_versions = all_versions.filter(revision__in=old_revs)[:1] |
---|
| 256 | # This object being passed in means that some version changed it. |
---|
[b04e142] | 257 | after_versions = all_versions.filter(revision__in=new_revs).select_related('revision__user') |
---|
| 258 | after = after_versions[0] |
---|
[b6c7a44] | 259 | |
---|
[b04e142] | 260 | if len(before_versions) > 0 or len(after_versions) > 1: |
---|
| 261 | if len(before_versions) > 0: |
---|
| 262 | before = before_versions[0] |
---|
[4091199] | 263 | stats['change_old'] += 1 |
---|
[b04e142] | 264 | else: |
---|
| 265 | # New group that's been edited since. Diff against the creation |
---|
| 266 | # (since creation sent mail, but later changes haven't) |
---|
[80a8145] | 267 | before = after_versions.reverse()[0] |
---|
[4091199] | 268 | stats['change_new'] += 1 |
---|
| 269 | stats['change_total'] += 1 |
---|
| 270 | #print "Change?: before=%s (%d), after=%s (%d), type=%s, new=%s" % ( |
---|
| 271 | # before, before.pk, |
---|
| 272 | # after, after.pk, |
---|
| 273 | # after.type, after.field_dict, |
---|
| 274 | #) |
---|
[b83db50] | 275 | before_fields = before.field_dict |
---|
| 276 | after_fields = after.field_dict |
---|
[b6c7a44] | 277 | for callback in callbacks: |
---|
| 278 | callback.handle_group(before, after, before_fields, after_fields) |
---|
| 279 | else: |
---|
[b04e142] | 280 | # New group that's only been edited once |
---|
[80a8145] | 281 | # Note that "normal" new groups will have their startup form |
---|
| 282 | # (which creates the Group object) and the approval (which makes |
---|
| 283 | # more changes, so this is group startups + NGEs, not actually |
---|
| 284 | # normal new groups) |
---|
[4091199] | 285 | stats['new_group'] += 1 |
---|
[b6c7a44] | 286 | |
---|
| 287 | def diff_signatories(since, now, callbacks): |
---|
| 288 | # First: still around; then added recently |
---|
| 289 | qobj_added = Q(end_time__gte=now, start_time__gte=since) |
---|
| 290 | # First: already gone; then it existed for a while; finally expired recently |
---|
| 291 | qobj_expired = Q(end_time__lte=now, start_time__lte=since, end_time__gte=since) |
---|
| 292 | changed_signatories = groups.models.OfficeHolder.objects.filter(qobj_added|qobj_expired) |
---|
[080bf9a] | 293 | changed_signatories = changed_signatories.order_by('group__name', 'role__display_name', 'person', ) |
---|
[b6c7a44] | 294 | changed_signatories = changed_signatories.select_related('role', 'group') |
---|
| 295 | for callback in callbacks: callback.handle_signatories(changed_signatories) |
---|
| 296 | |
---|
| 297 | def generate_diffs(): |
---|
| 298 | now = datetime.datetime.now() |
---|
[a34597e] | 299 | recent = now - datetime.timedelta(hours=24, minutes=15) |
---|
[b6c7a44] | 300 | objs = recent_groups(since=recent) |
---|
| 301 | callbacks = build_callbacks() |
---|
[4091199] | 302 | stats = collections.defaultdict(lambda: 0) |
---|
[b6c7a44] | 303 | for callback in callbacks: callback.start_run(since=recent, now=now, ) |
---|
[4091199] | 304 | diff_objects(objs, since=recent, callbacks=callbacks, stats=stats) |
---|
[b6c7a44] | 305 | diff_signatories(recent, now, callbacks=callbacks, ) |
---|
| 306 | for callback in callbacks: callback.end_run() |
---|
| 307 | |
---|
[4091199] | 308 | print "\nOverall change stats:" |
---|
| 309 | for stat_key, stat_val in stats.items(): |
---|
| 310 | print "%20s:\t%6d" % (stat_key, stat_val, ) |
---|
| 311 | print "" |
---|
| 312 | |
---|
[b6c7a44] | 313 | if __name__ == '__main__': |
---|
| 314 | generate_diffs() |
---|