blob: 6118a7ba094c7c606e584b2c0678807a80acecf0 [file] [log] [blame]
Sapan Bhatia3b182032014-09-03 15:38:56 -04001from core.models import *
2
3def handle(user):
Sapan Bhatia728acce2014-09-15 03:48:20 -04004 from core.models import Deployment,SiteDeployments,UserDeployments
5 from collections import defaultdict
Sapan Bhatia3b182032014-09-03 15:38:56 -04006 deployments = Deployment.objects.all()
7 site_deployments = SiteDeployments.objects.all()
8 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)
13 for user_deployment in UserDeployments.objects.all():
14 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
30 ud = UserDeployments(user=user, deployment=expected_deployment)
31 ud.save()
32