Tony Mack | 4c1c57c | 2015-01-03 16:58:18 -0500 | [diff] [blame^] | 1 | from core.models import * |
| 2 | |
| 3 | def handle(site): |
| 4 | """ |
| 5 | ensure ControllerSiteDeployment object exists for each of the site's SiteDeployment objects |
| 6 | """ |
| 7 | from core.models import SiteDeployments, Controller, ControllerSiteDeployments |
| 8 | from collections import defaultdict |
| 9 | |
| 10 | |
| 11 | # get current controller site deployments |
| 12 | ctrl_site_deployments = ControllerSiteDeployments.objects.filter(site_deployment__site = site) |
| 13 | ctrl_site_deployments_dict = {} |
| 14 | for ctrl_site_depl in controller_site_deployments: |
| 15 | ctrl_site_deployments_dict[ctrl_site_depl.site_deployment] = ctrl_cite_depl |
| 16 | |
| 17 | # get current site deployments |
| 18 | site_deployments = SiteDeployments.objects.filter(site=site) |
| 19 | |
| 20 | # for each site deployment, if there is no controller site deployment create one |
| 21 | for site_deployment in site_deployments: |
| 22 | if site_deployment not in ctrl_site_deployments_dict: |
| 23 | ctrl_site_deployment = ControllerSiteDeployment( |
| 24 | site_deployment=site_deployment, |
| 25 | controller=site_deployment.controller, |
| 26 | ) |
| 27 | ctrl_site_deployment.save() |