1 | #!/usr/bin/python |
---|
2 | import collections |
---|
3 | import csv |
---|
4 | import datetime |
---|
5 | import os |
---|
6 | import sys |
---|
7 | |
---|
8 | if __name__ == '__main__': |
---|
9 | cur_file = os.path.abspath(__file__) |
---|
10 | django_dir = os.path.abspath(os.path.join(os.path.dirname(cur_file), '..')) |
---|
11 | sys.path.append(django_dir) |
---|
12 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' |
---|
13 | |
---|
14 | import groups.models |
---|
15 | import space.models |
---|
16 | |
---|
17 | def current_groups(): |
---|
18 | return space.models.SpaceAssignment.current.values_list('group', flat=True) |
---|
19 | |
---|
20 | def people_lookup(): |
---|
21 | office_access = groups.models.OfficeHolder.current_holders.filter(role__slug='office-access', group__in=current_groups()).values_list('person', flat=True) |
---|
22 | current_users = groups.models.AthenaMoiraAccount.objects.filter(username__in=office_access) |
---|
23 | user_map = {} |
---|
24 | for user in current_users: |
---|
25 | user_map[user.username] = user |
---|
26 | return user_map |
---|
27 | |
---|
28 | def space_lookup(): |
---|
29 | space_list = space.models.Space.objects.all() |
---|
30 | space_map = {} |
---|
31 | for space_obj in space_list: |
---|
32 | space_map[space_obj.pk] = space_obj |
---|
33 | return space_map |
---|
34 | |
---|
35 | def group_lookup(): |
---|
36 | group_list = groups.models.Group.objects.filter(id__in=current_groups()) |
---|
37 | group_map = {} |
---|
38 | for group_obj in group_list: |
---|
39 | group_map[group_obj.pk] = group_obj |
---|
40 | return group_map |
---|
41 | |
---|
42 | def gather_users(): |
---|
43 | spaces_list = space.models.SpaceAssignment.current.filter() |
---|
44 | space_aces = collections.defaultdict(dict) |
---|
45 | for assignment in spaces_list: |
---|
46 | if not assignment.is_locker(): |
---|
47 | space_aces[assignment.space_id][assignment.group_id] = set() |
---|
48 | for ace in space.models.SpaceAccessListEntry.current.filter(group__in=current_groups()): |
---|
49 | if ace.group_id in space_aces[ace.space_id]: |
---|
50 | space_aces[ace.space_id][ace.group_id].add(ace) |
---|
51 | return space_aces |
---|
52 | |
---|
53 | |
---|
54 | holders = groups.models.OfficerRole.current_holders.filter(role__slug='office-access') |
---|
55 | |
---|
56 | def print_info(stream=None, ): |
---|
57 | if not stream: stream=sys.stdout |
---|
58 | |
---|
59 | people_map = people_lookup() |
---|
60 | space_map = space_lookup() |
---|
61 | group_map = group_lookup() |
---|
62 | |
---|
63 | # Load office-access folks |
---|
64 | group_holders = collections.defaultdict(set) |
---|
65 | for holder in groups.models.OfficeHolder.current_holders.filter(role__slug='office-access', group__in=current_groups()): |
---|
66 | group_holders[holder.group_id].add((holder, people_map[holder.person])) |
---|
67 | |
---|
68 | # Load room-specific stuff |
---|
69 | space_aces = gather_users() |
---|
70 | |
---|
71 | writer = csv.writer(stream) |
---|
72 | writer.writerow(("space", "group", "last_name", "first_name", "username", "mit_id", )) |
---|
73 | for space_id, space_groups in space_aces.items(): |
---|
74 | space_obj = space_map[space_id] |
---|
75 | writer.writerow(()) |
---|
76 | for group_id, aces in space_groups.items(): |
---|
77 | group_obj = group_map[group_id] |
---|
78 | writer.writerow(()) |
---|
79 | for ace in aces: |
---|
80 | writer.writerow((space_obj, group_obj, "", ace.name, "", ace.card_number, )) |
---|
81 | for holder, person in group_holders[group_id]: |
---|
82 | writer.writerow((space_obj, group_obj, person.last_name, person.first_name, person.mit_id, )) |
---|
83 | |
---|
84 | if __name__ == '__main__': |
---|
85 | print_info() |
---|