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