Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | |
| 2 | def handle(controller): |
| 3 | from core.models import Controller, Site, ControllerSite, Slice, ControllerSlice, User, ControllerUser, ControllerImages, ControllerNetwork, Image, Network |
| 4 | from collections import defaultdict |
| 5 | |
| 6 | # relations for all sites |
| 7 | ctrls_by_site = defaultdict(list) |
| 8 | ctrl_sites = ControllerSite.objects.all() |
| 9 | for ctrl_site in ctrl_sites: |
| 10 | ctrls_by_site[ctrl_site.site].append(ctrl_site.controller) |
| 11 | sites = Site.objects.all() |
| 12 | for site in sites: |
| 13 | if site not in ctrls_by_site or \ |
| 14 | controller not in ctrls_by_site[site]: |
| 15 | controller_site = ControllerSite(controller=controller, site=site) |
| 16 | controller_site.save() |
| 17 | # relations for all slices |
| 18 | ctrls_by_slice = defaultdict(list) |
| 19 | ctrl_slices = ControllerSlice.objects.all() |
| 20 | for ctrl_slice in ctrl_slices: |
| 21 | ctrls_by_slice[ctrl_slice.slice].append(ctrl_slice.controller) |
| 22 | slices = Slice.objects.all() |
| 23 | for slice in slices: |
| 24 | if slice not in ctrls_by_slice or \ |
| 25 | controller not in ctrls_by_slice[slice]: |
| 26 | controller_slice = ControllerSlice(controller=controller, slice=slice) |
| 27 | controller_slice.save() |
| 28 | # relations for all users |
| 29 | ctrls_by_user = defaultdict(list) |
| 30 | ctrl_users = ControllerUser.objects.all() |
| 31 | for ctrl_user in ctrl_users: |
| 32 | ctrls_by_user[ctrl_user.user].append(ctrl_user.controller) |
| 33 | users = User.objects.all() |
| 34 | for user in users: |
| 35 | if user not in ctrls_by_user or \ |
| 36 | controller not in ctrls_by_user[user]: |
| 37 | controller_user = ControllerUser(controller=controller, user=user) |
| 38 | controller_user.save() |
| 39 | # relations for all networks |
| 40 | ctrls_by_network = defaultdict(list) |
| 41 | ctrl_networks = ControllerNetwork.objects.all() |
| 42 | for ctrl_network in ctrl_networks: |
| 43 | ctrls_by_network[ctrl_network.network].append(ctrl_network.controller) |
| 44 | networks = Network.objects.all() |
| 45 | for network in networks: |
| 46 | if network not in ctrls_by_network or \ |
| 47 | controller not in ctrls_by_network[network]: |
| 48 | controller_network = ControllerNetwork(controller=controller, network=network) |
| 49 | if network.subnet and network.subnet.strip(): |
| 50 | controller_network.subnet = network.subnet.strip() |
| 51 | controller_network.save() |
| 52 | # relations for all images |
| 53 | ctrls_by_image = defaultdict(list) |
| 54 | ctrl_images = ControllerImages.objects.all() |
| 55 | for ctrl_image in ctrl_images: |
| 56 | ctrls_by_image[ctrl_image.image].append(ctrl_image.controller) |
| 57 | images = Image.objects.all() |
| 58 | for image in images: |
| 59 | if image not in ctrls_by_image or \ |
| 60 | controller not in ctrls_by_image[image]: |
| 61 | controller_image = ControllerImages(controller=controller, image=image) |
| 62 | controller_image.save() |