blob: 703b28c2eb16b575128e4e381a0824c40d052fd0 [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Tony Mack528d4222014-12-05 17:13:08 -05004 from core.models import Controller, ControllerSiteDeployments, ControllerUsers
Sapan Bhatia728acce2014-09-15 03:48:20 -04005 from collections import defaultdict
Tony Mack528d4222014-12-05 17:13:08 -05006 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 Bhatia3b182032014-09-03 15:38:56 -040010
Tony Mack336e0f92014-11-30 15:53:08 -050011 controller_user_lookup = defaultdict(list)
Tony Mack528d4222014-12-05 17:13:08 -050012 for controller_user in ControllerUsers.objects.all():
13 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 Mack336e0f92014-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 Mack336e0f92014-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 Mack336e0f92014-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 Mack336e0f92014-11-30 15:53:08 -050028 ud = ControllerUsers(user=user, controller=expected_controller)
Sapan Bhatia3b182032014-09-03 15:38:56 -040029 ud.save()
30