Sapan Bhatia | 3b18203 | 2014-09-03 15:38:56 -0400 | [diff] [blame^] | 1 | from core.models import * |
| 2 | |
| 3 | def handle(user): |
| 4 | deployments = Deployment.objects.all() |
| 5 | site_deployments = SiteDeployments.objects.all() |
| 6 | site_deploy_lookup = defaultdict(list) |
| 7 | for site_deployment in site_deployments: |
| 8 | site_deploy_lookup[site_deployment.site].append(site_deployment.deployment) |
| 9 | |
| 10 | user_deploy_lookup = defaultdict(list) |
| 11 | for user_deployment in UserDeployments.objects.all(): |
| 12 | user_deploy_lookup[user_deployment.user].append(user_deployment.deployment) |
| 13 | |
| 14 | all_deployments = Deployment.objects.filter() |
| 15 | if user.is_admin: |
| 16 | # admins should have an account at all deployments |
| 17 | expected_deployments = deployments |
| 18 | else: |
| 19 | # normal users should have an account at their site's deployments |
| 20 | #expected_deployments = site_deploy_lookup[user.site] |
| 21 | # users are added to all deployments for now |
| 22 | expected_deployments = deployments |
| 23 | |
| 24 | for expected_deployment in expected_deployments: |
| 25 | if not user in user_deploy_lookup or \ |
| 26 | expected_deployment not in user_deploy_lookup[user]: |
| 27 | # add new record |
| 28 | ud = UserDeployments(user=user, deployment=expected_deployment) |
| 29 | ud.save() |
| 30 | |