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 gather_users(): |
---|
18 | spaces_groups = collections.defaultdict(set) |
---|
19 | spaces_list = space.models.SpaceAssignment.current.filter() |
---|
20 | for assignment in spaces_list: |
---|
21 | if assignment.is_locker(): |
---|
22 | spaces_groups[assignment.space.pk].add(assignment.group) |
---|
23 | space_users = {} |
---|
24 | role = groups.models.OfficerRole.objects.get(slug='locker-access') |
---|
25 | for space_id, space_groups in spaces_groups.items(): |
---|
26 | users = set() |
---|
27 | for group in space_groups: |
---|
28 | holders = group.officers(role=role) |
---|
29 | for holder in holders: users.add(holder.person) |
---|
30 | space_users[space_id] = users |
---|
31 | return space_users |
---|
32 | |
---|
33 | def print_info(space_users, stream=None, ): |
---|
34 | if not stream: stream = sys.stdout |
---|
35 | writer = csv.writer(stream) |
---|
36 | writer.writerow(("space", "last_name", "first_name", "username", "mit_id", )) |
---|
37 | for space_id, users in space_users.items(): |
---|
38 | writer.writerow(()) |
---|
39 | cur_space = space.models.Space.objects.get(pk=space_id) |
---|
40 | writer.writerow((cur_space, )) |
---|
41 | user_objs = groups.models.AthenaMoiraAccount.objects.filter(username__in=users).order_by('last_name', 'first_name', ) |
---|
42 | for user in user_objs: |
---|
43 | writer.writerow((cur_space, user.last_name, user.first_name, user.username, user.mit_id)) |
---|
44 | |
---|
45 | if __name__ == '__main__': |
---|
46 | space_users = gather_users() |
---|
47 | print_info(space_users) |
---|