blob: 5e2adade1a770cb909a437ac6afa38e473b4631c [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Tony Mack3066a952015-01-05 22:48:11 -05004 from core.models import Controller, ControllerSite, ControllerUser
Sapan Bhatia0e5be032014-09-15 03:48:20 -04005 from collections import defaultdict
Tony Mack3066a952015-01-05 22:48:11 -05006 ctrl_site_deployments = ControllerSite.objects.all()
Tony Mackd14d48f2014-12-05 17:13:08 -05007 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 Bhatia3b182032014-09-03 15:38:56 -040010
Tony Mack06c8e472014-11-30 15:53:08 -050011 controller_user_lookup = defaultdict(list)
Tony Mack3066a952015-01-05 22:48:11 -050012 for controller_user in ControllerUser.objects.all():
Tony Mackd14d48f2014-12-05 17:13:08 -050013 controller_user_lookup[controller_user.user].append(controller_user.controller)
Sapan Bhatia3b182032014-09-03 15:38:56 -040014
Sapan Bhatia3b182032014-09-03 15:38:56 -040015 if user.is_admin:
Tony Mack06c8e472014-11-30 15:53:08 -050016 # admins should have an account at all controllers
17 expected_controllers = controllers
Sapan Bhatia3b182032014-09-03 15:38:56 -040018 else:
Tony Mack06c8e472014-11-30 15:53:08 -050019 # 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 Bhatia3b182032014-09-03 15:38:56 -040023
Tony Mack06c8e472014-11-30 15:53:08 -050024 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 Bhatia3b182032014-09-03 15:38:56 -040027 # add new record
Tony Mack3066a952015-01-05 22:48:11 -050028 ud = ControllerUser(user=user, controller=expected_controller)
Sapan Bhatia3b182032014-09-03 15:38:56 -040029 ud.save()
30