source: asadb/groups/format_groups.py @ a7d30ef

space-accessstablestage
Last change on this file since a7d30ef was a7d30ef, checked in by Alex Dehnert <adehnert@…>, 14 years ago

Script to dump groups with space (ASA Trac #95)

Add support to groups/format_groups.py to dump email addresses of groups with
various kinds of assigned space (W20 office, W20 locker, etc.)

  • Property mode set to 100755
File size: 2.9 KB
RevLine 
[f57cf3c]1#!/usr/bin/python
2
3import csv
4import os
5import sys
6
7if __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]13from django.db import connection
[f57cf3c]14from django.db.models import Q
[e9af979]15from django.utils import html
[f57cf3c]16
17import groups.models
[a7d30ef]18import space.models
[f57cf3c]19
[a7d30ef]20def 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]26Qsa = Q(group_status__slug__in=('active', 'suspended', ))
[a7d30ef]27
[f57cf3c]28functions = {
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
71def 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
89if __name__ == "__main__":
90    do_output(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.