blob: cc8bbe65d4f6ec354b97965377c637c9e8f7b7ac [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Sapan Bhatia84ba65d2014-11-11 21:44:35 -05004 from core.models import Deployment,SiteDeployment,UserDeployment
Sapan Bhatia728acce2014-09-15 03:48:20 -04005 from collections import defaultdict
Sapan Bhatia3b182032014-09-03 15:38:56 -04006 deployments = Deployment.objects.all()
Sapan Bhatia84ba65d2014-11-11 21:44:35 -05007 site_deployments = SiteDeployment.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 Bhatia84ba65d2014-11-11 21:44:35 -050013 for user_deployment in UserDeployment.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 Bhatia84ba65d2014-11-11 21:44:35 -050030 ud = UserDeployment(user=user, deployment=expected_deployment)
Sapan Bhatia3b182032014-09-03 15:38:56 -040031 ud.save()
32