[f57cf3c] | 1 | #!/usr/bin/python |
---|
| 2 | |
---|
| 3 | import csv |
---|
| 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 | |
---|
[a7d30ef] | 13 | from django.db import connection |
---|
[f57cf3c] | 14 | from django.db.models import Q |
---|
[e9af979] | 15 | from django.utils import html |
---|
[f57cf3c] | 16 | |
---|
| 17 | import groups.models |
---|
[a7d30ef] | 18 | import space.models |
---|
[f57cf3c] | 19 | |
---|
[a7d30ef] | 20 | def space_Q(merged_acl=None, building=None, ): |
---|
| 21 | Qspace = Q() |
---|
| 22 | if merged_acl is not None: Qspace = Q(space__merged_acl=merged_acl) |
---|
| 23 | if building: Qspace = Qspace & Q(space__number__startswith=building+'-') |
---|
| 24 | return Q(spaceassignment__in=space.models.SpaceAssignment.current.filter(Qspace)) |
---|
[f57cf3c] | 25 | |
---|
[b90b977] | 26 | Qsa = Q(group_status__slug__in=('active', 'suspended', )) |
---|
[a7d30ef] | 27 | |
---|
[f57cf3c] | 28 | functions = { |
---|
| 29 | 'finboard' : { |
---|
[b90b977] | 30 | 'format' : "%(name)s;%(officer_email)s", |
---|
| 31 | 'predicate' : Qsa & Q(group_funding__slug='undergrad', group_class__slug='mit-funded', ), |
---|
[f57cf3c] | 32 | }, |
---|
| 33 | 'nolist' : { |
---|
[b90b977] | 34 | 'format' : "%(name)s <%(officer_email)s>", |
---|
| 35 | 'predicate' : Qsa & Q(officer_email=""), |
---|
[f57cf3c] | 36 | }, |
---|
| 37 | 'asa-official' : { |
---|
| 38 | 'format' : '"%(name)s" <%(officer_email)s>', |
---|
| 39 | 'predicate' : Q(group_status__slug='active'), |
---|
| 40 | }, |
---|
| 41 | 'emails-only' : { |
---|
[b90b977] | 42 | 'format' : '%(officer_email)s', |
---|
| 43 | 'predicate' : Qsa, |
---|
[f57cf3c] | 44 | }, |
---|
[e9af979] | 45 | 'midway' : { |
---|
| 46 | 'prefix': """ |
---|
| 47 | <!-- Automatically generated by %(script)s %(mode)s. |
---|
| 48 | Do not edit; instead re-run that script (or edit it as necessary). --> |
---|
| 49 | """, |
---|
| 50 | 'format' : '<option value="%(html_name)s">%(html_name)s</option>', |
---|
| 51 | 'predicate' : Q(group_status__slug__in=['active', 'suspended', 'applying', 'nge'], group_class__gets_publicity=True, ), |
---|
| 52 | }, |
---|
[a7d30ef] | 53 | 'w20-locker' : { |
---|
| 54 | 'format' : '%(officer_email)s;%(name)s', |
---|
| 55 | 'predicate' : Qsa & space_Q(merged_acl=True, building='W20'), |
---|
| 56 | }, |
---|
| 57 | 'w20-office' : { |
---|
| 58 | 'format' : '%(officer_email)s;%(name)s', |
---|
| 59 | 'predicate' : Qsa & space_Q(merged_acl=False, building='W20'), |
---|
| 60 | }, |
---|
| 61 | 'all-office' : { |
---|
| 62 | 'format' : '%(officer_email)s;%(name)s', |
---|
| 63 | 'predicate' : Qsa & space_Q(merged_acl=False), |
---|
| 64 | }, |
---|
| 65 | 'all-space' : { |
---|
| 66 | 'format' : '%(officer_email)s;%(name)s', |
---|
| 67 | 'predicate' : Qsa & space_Q(), |
---|
| 68 | }, |
---|
[f57cf3c] | 69 | } |
---|
| 70 | |
---|
| 71 | def do_output(mode): |
---|
[e9af979] | 72 | spec = functions[mode] |
---|
| 73 | format = spec['format'] |
---|
| 74 | predicate = spec['predicate'] |
---|
[a7d30ef] | 75 | gs = groups.models.Group.objects.filter(predicate).distinct() |
---|
[e9af979] | 76 | static_args = {'script': 'groups/format_groups.py', 'mode':mode, } |
---|
| 77 | if 'prefix' in spec: |
---|
| 78 | print spec['prefix'] % static_args |
---|
[f57cf3c] | 79 | for group in gs: |
---|
| 80 | print format % { |
---|
| 81 | 'name':group.name, |
---|
[e9af979] | 82 | 'html_name':html.escape(group.name), |
---|
[f57cf3c] | 83 | 'officer_email':group.officer_email, |
---|
| 84 | } |
---|
[e9af979] | 85 | if 'suffix' in spec: |
---|
| 86 | print spec['suffix'] % static_args |
---|
[a7d30ef] | 87 | #for q in connection.queries: print q |
---|
[f57cf3c] | 88 | |
---|
| 89 | if __name__ == "__main__": |
---|
| 90 | do_output(sys.argv[1]) |
---|