Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 1 | from core.models import * |
| 2 | |
| 3 | def handle(user): |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 4 | from core.models import Controller, ControllerSite, ControllerUser |
Sapan Bhatia | 0e5be03 | 2014-09-15 03:48:20 -0400 | [diff] [blame] | 5 | from collections import defaultdict |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 6 | ctrl_site_deployments = ControllerSite.objects.all() |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 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 | 06c8e47 | 2014-11-30 15:53:08 -0500 | [diff] [blame] | 11 | controller_user_lookup = defaultdict(list) |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 12 | for controller_user in ControllerUser.objects.all(): |
Tony Mack | d14d48f | 2014-12-05 17:13:08 -0500 | [diff] [blame] | 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 | 06c8e47 | 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 | 06c8e47 | 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 | 06c8e47 | 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 | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 28 | ud = ControllerUser(user=user, controller=expected_controller) |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 29 | ud.save() |
| 30 | |