Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 1 | from core.models import * |
| 2 | |
| 3 | def handle(user): |
Sapan Bhatia | 84ba65d | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 4 | from core.models import Deployment,SiteDeployment,UserDeployment |
Sapan Bhatia | 728acce | 2014-09-15 03:48:20 -0400 | [diff] [blame] | 5 | from collections import defaultdict |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 6 | deployments = Deployment.objects.all() |
Sapan Bhatia | 84ba65d | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 7 | site_deployments = SiteDeployment.objects.all() |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 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) |
Sapan Bhatia | 84ba65d | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 13 | for user_deployment in UserDeployment.objects.all(): |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 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 |
Sapan Bhatia | 84ba65d | 2014-11-11 21:44:35 -0500 | [diff] [blame] | 30 | ud = UserDeployment(user=user, deployment=expected_deployment) |
Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame] | 31 | ud.save() |
| 32 | |