blob: 593ba9dd974f7b2ccb814e296030ad28a413643f [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Tony Mack06c8e472014-11-30 15:53:08 -05004 from core.models import Controller,ControllerSites,ControllerUsers
Sapan Bhatia0e5be032014-09-15 03:48:20 -04005 from collections import defaultdict
Tony Mack06c8e472014-11-30 15:53:08 -05006 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 Bhatia3b182032014-09-03 15:38:56 -040011
Tony Mack06c8e472014-11-30 15:53:08 -050012 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 Bhatia3b182032014-09-03 15:38:56 -040015
Tony Mack06c8e472014-11-30 15:53:08 -050016 all_controllers = Controller.objects.filter()
Sapan Bhatia3b182032014-09-03 15:38:56 -040017 if user.is_admin:
Tony Mack06c8e472014-11-30 15:53:08 -050018 # admins should have an account at all controllers
19 expected_controllers = controllers
Sapan Bhatia3b182032014-09-03 15:38:56 -040020 else:
Tony Mack06c8e472014-11-30 15:53:08 -050021 # 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 Bhatia3b182032014-09-03 15:38:56 -040025
Tony Mack06c8e472014-11-30 15:53:08 -050026 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 Bhatia3b182032014-09-03 15:38:56 -040029 # add new record
Tony Mack06c8e472014-11-30 15:53:08 -050030 ud = ControllerUsers(user=user, controller=expected_controller)
Sapan Bhatia3b182032014-09-03 15:38:56 -040031 ud.save()
32