Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 1 | from collections import defaultdict |
| 2 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 3 | from synchronizers.new_base.policy import Policy |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 4 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 5 | class ControllerPolicy(Policy): |
| 6 | model_name = "Controller" |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 7 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 8 | def handle_create(self, controller): |
| 9 | return self.handle_update(controller) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 10 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 11 | def handle_update(self, controller): |
| 12 | # relations for all sites |
| 13 | ctrls_by_site = defaultdict(list) |
| 14 | ctrl_sites = ControllerSite.objects.all() |
| 15 | for ctrl_site in ctrl_sites: |
| 16 | ctrls_by_site[ctrl_site.site.id].append(ctrl_site.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 17 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 18 | sites = Site.objects.all() |
| 19 | for site in sites: |
| 20 | if site.id not in ctrls_by_site or controller.id not in ctrls_by_site[site.id]: |
| 21 | controller_site = ControllerSite(controller=controller, site=site) |
| 22 | controller_site.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 23 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 24 | # relations for all slices |
| 25 | ctrls_by_slice = defaultdict(list) |
| 26 | ctrl_slices = ControllerSlice.objects.all() |
| 27 | for ctrl_slice in ctrl_slices: |
| 28 | ctrls_by_slice[ctrl_slice.slice.id].append(ctrl_slice.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 29 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 30 | slices = Slice.objects.all() |
| 31 | for slice in slices: |
| 32 | if slice.id not in ctrls_by_slice or controller.id not in ctrls_by_slice[slice.id]: |
| 33 | controller_slice = ControllerSlice(controller=controller, slice=slice) |
| 34 | controller_slice.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 35 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 36 | # relations for all users |
| 37 | ctrls_by_user = defaultdict(list) |
| 38 | ctrl_users = ControllerUser.objects.all() |
| 39 | for ctrl_user in ctrl_users: |
| 40 | ctrls_by_user[ctrl_user.user.id].append(ctrl_user.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 41 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 42 | users = User.objects.all() |
| 43 | for user in users: |
| 44 | if user.id not in ctrls_by_user or controller.id not in ctrls_by_user[user.id]: |
| 45 | controller_user = ControllerUser(controller=controller, user=user) |
| 46 | controller_user.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 47 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 48 | # relations for all networks |
| 49 | ctrls_by_network = defaultdict(list) |
| 50 | ctrl_networks = ControllerNetwork.objects.all() |
| 51 | for ctrl_network in ctrl_networks: |
| 52 | ctrls_by_network[ctrl_network.network.id].append(ctrl_network.controller.id) |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 53 | |
Scott Baker | 46a3ee9 | 2017-05-30 16:44:56 -0700 | [diff] [blame^] | 54 | networks = Network.objects.all() |
| 55 | for network in networks: |
| 56 | if network.id not in ctrls_by_network or controller.id not in ctrls_by_network[network.id]: |
| 57 | controller_network = ControllerNetwork(controller=controller, network=network) |
| 58 | if network.subnet and network.subnet.strip(): |
| 59 | controller_network.subnet = network.subnet.strip() |
| 60 | controller_network.save() |
| 61 | |
| 62 | # relations for all images |
| 63 | ctrls_by_image = defaultdict(list) |
| 64 | ctrl_images = ControllerImages.objects.all() |
| 65 | for ctrl_image in ctrl_images: |
| 66 | ctrls_by_image[ctrl_image.image.id].append(ctrl_image.controller.id) |
| 67 | |
| 68 | images = Image.objects.all() |
| 69 | for image in images: |
| 70 | if image.id not in ctrls_by_image or controller.id not in ctrls_by_image[image.id]: |
| 71 | controller_image = ControllerImages(controller=controller, image=image) |
| 72 | controller_image.save() |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 73 | |