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 | |
---|
13 | from django.db import connection |
---|
14 | from django.db.models import Q |
---|
15 | from django.utils import html |
---|
16 | |
---|
17 | import groups.models |
---|
18 | import space.models |
---|
19 | |
---|
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)) |
---|
25 | |
---|
26 | Qsa = Q(group_status__slug__in=('active', 'suspended', )) |
---|
27 | |
---|
28 | functions = { |
---|
29 | 'finboard' : { |
---|
30 | 'format' : "%(name)s;%(officer_email)s", |
---|
31 | 'predicate' : Qsa & Q(group_funding__slug='undergrad', group_class__slug='mit-funded', ), |
---|
32 | }, |
---|
33 | 'nolist' : { |
---|
34 | 'format' : "%(name)s <%(officer_email)s>", |
---|
35 | 'predicate' : Qsa & Q(officer_email=""), |
---|
36 | }, |
---|
37 | 'asa-official' : { |
---|
38 | 'format' : '"%(name)s" <%(officer_email)s>', |
---|
39 | 'predicate' : Q(group_status__slug='active'), |
---|
40 | }, |
---|
41 | 'emails-only' : { |
---|
42 | 'format' : '%(officer_email)s', |
---|
43 | 'predicate' : Qsa, |
---|
44 | }, |
---|
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 | }, |
---|
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 | }, |
---|
69 | } |
---|
70 | |
---|
71 | def do_output(mode): |
---|
72 | spec = functions[mode] |
---|
73 | format = spec['format'] |
---|
74 | predicate = spec['predicate'] |
---|
75 | gs = groups.models.Group.objects.filter(predicate).distinct() |
---|
76 | static_args = {'script': 'groups/format_groups.py', 'mode':mode, } |
---|
77 | if 'prefix' in spec: |
---|
78 | print spec['prefix'] % static_args |
---|
79 | for group in gs: |
---|
80 | print format % { |
---|
81 | 'name':group.name, |
---|
82 | 'html_name':html.escape(group.name), |
---|
83 | 'officer_email':group.officer_email, |
---|
84 | } |
---|
85 | if 'suffix' in spec: |
---|
86 | print spec['suffix'] % static_args |
---|
87 | #for q in connection.queries: print q |
---|
88 | |
---|
89 | if __name__ == "__main__": |
---|
90 | do_output(sys.argv[1]) |
---|