Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 1 | from core.models import * |
| 2 | |
| 3 | def handle(user): |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 4 | from core.models import Controller,ControllerSites,ControllerUsers |
Sapan Bhatia | 0e5be03 | 2014-09-15 03:48:20 -0400 | [diff] [blame] | 5 | from collections import defaultdict |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 6 | controllers = Controller.objects.all() |
| 7 | controller_sitements = ControllerSites.objects.all() |
| 8 | controller_site_lookup = defaultdict(list) |
| 9 | for controller_sitement in controller_sitements: |
| 10 | controller_site_lookup[controller_sitement.site].append(controller_sitement.controller) |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 11 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 12 | controller_user_lookup = defaultdict(list) |
| 13 | for controller_userment in ControllerUsers.objects.all(): |
| 14 | controller_user_lookup[controller_userment.user].append(controller_userment.controller) |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 15 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 16 | all_controllers = Controller.objects.filter() |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 17 | if user.is_admin: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 18 | # admins should have an account at all controllers |
| 19 | expected_controllers = controllers |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 20 | else: |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 21 | # normal users should have an account at their site's controllers |
| 22 | #expected_controllers = controller_site_lookup[user.site] |
| 23 | # users are added to all controllers for now |
| 24 | expected_controllers = controllers |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 25 | |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 26 | for expected_controller in expected_controllers: |
| 27 | if not user in controller_user_lookup or \ |
| 28 | expected_controller not in controller_user_lookup[user]: |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 29 | # add new record |
Tony Mack | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame^] | 30 | ud = ControllerUsers(user=user, controller=expected_controller) |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 31 | ud.save() |
| 32 | |