source: asadb/groups/export_finboard_groups.py

stable
Last change on this file was 356174b, checked in by Alex Dehnert <adehnert@…>, 11 years ago

Improve perf and correctness of export_finboard_groups.py

export_finboard_groups.py was taking about 45 seconds to run in prod, and
including former signatories.

Changes:

  • Stop using prefetch_related
    • As being used, the return value wasn't being saved as a new queryset, so it wasn't effective
    • Fixing that revealed that "officerrole" is the wrong field name
    • Finally, it requires doing filter (for role and current officers) in Python, which seemed a bit sad, and seemed like it would plausibly be appreciably slower due to needing to pull all past officers
  • Don't reuse groups, because it shadows the module name
  • Indent the JSON for improved readability
  • Property mode set to 100755
File size: 1.5 KB
Line 
1#!/usr/bin/python
2#
3# Exports data for the Finboard app as JSON: currently just group
4# id/name/account number and P/T/GA/FS
5#
6# Use as e.g.
7# ./export_finboard_groups.py > /mit/asa-db/data/finboard/groups.json
8
9import codecs
10import datetime
11import json
12import os
13import sys
14
15if __name__ == '__main__':
16    cur_file = os.path.abspath(__file__)
17    django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..'))
18    sys.path.append(django_dir)
19    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
20
21import groups.models
22
23ROLES = ['president', 'treasurer', 'financial', 'group-admin']
24
25if __name__ == '__main__':
26    gs = groups.models.Group.objects.filter(group_status__is_active=True)
27    gs = gs.filter(group_funding__slug='undergrad')
28
29    officers = groups.models.OfficeHolder.current_holders
30    officers = officers.filter(group__in=gs, role__slug__in=ROLES)
31    officers = officers.select_related('role__slug', 'group')
32
33    group_dicts = {}
34    for group in gs:
35        group_dict = {
36            'id': group.id,
37            'name': group.name,
38            'funding-account': group.funding_account_id,
39        }
40        for role in ROLES:
41            group_dict[role] = []
42        group_dicts[group.id] = group_dict
43
44    for officer in officers:
45        group_dicts[officer.group.id][officer.role.slug].append(officer.person)
46
47    out = codecs.getwriter('utf-8')(sys.stdout)
48    json.dump({
49        'groups': group_dicts.values(),
50        'date': datetime.datetime.now().isoformat(),
51    }, out, indent=2)
Note: See TracBrowser for help on using the repository browser.