blob: 6118a7ba094c7c606e584b2c0678807a80acecf0 [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Sapan Bhatiae20777d2014-11-19 15:10:51 -05004 from core.models import Deployment,SiteDeployments,UserDeployments
Sapan Bhatia0e5be032014-09-15 03:48:20 -04005 from collections import defaultdict
Sapan Bhatia3b182032014-09-03 15:38:56 -04006 deployments = Deployment.objects.all()
Sapan Bhatiab55f4a32014-11-19 15:07:36 -05007 site_deployments = SiteDeployments.objects.all()
Sapan Bhatia3b182032014-09-03 15:38:56 -04008 site_deploy_lookup = defaultdict(list)
9 for site_deployment in site_deployments:
10 site_deploy_lookup[site_deployment.site].append(site_deployment.deployment)
11
12 user_deploy_lookup = defaultdict(list)
Sapan Bhatiae20777d2014-11-19 15:10:51 -050013 for user_deployment in UserDeployments.objects.all():
Sapan Bhatia3b182032014-09-03 15:38:56 -040014 user_deploy_lookup[user_deployment.user].append(user_deployment.deployment)
15
16 all_deployments = Deployment.objects.filter()
17 if user.is_admin:
18 # admins should have an account at all deployments
19 expected_deployments = deployments
20 else:
21 # normal users should have an account at their site's deployments
22 #expected_deployments = site_deploy_lookup[user.site]
23 # users are added to all deployments for now
24 expected_deployments = deployments
25
26 for expected_deployment in expected_deployments:
27 if not user in user_deploy_lookup or \
28 expected_deployment not in user_deploy_lookup[user]:
29 # add new record
Sapan Bhatiae20777d2014-11-19 15:10:51 -050030 ud = UserDeployments(user=user, deployment=expected_deployment)
Sapan Bhatia3b182032014-09-03 15:38:56 -040031 ud.save()
32