2022-05-03 11:32:52 -04:00
|
|
|
from django.db import migrations
|
|
|
|
|
|
|
|
from dcim.utils import compile_path_node
|
|
|
|
|
|
|
|
|
|
|
|
def populate_cable_paths(apps, schema_editor):
|
|
|
|
"""
|
|
|
|
Replicate terminations from the Cable model into CableTermination instances.
|
|
|
|
"""
|
|
|
|
CablePath = apps.get_model('dcim', 'CablePath')
|
|
|
|
|
|
|
|
# Construct the new two-dimensional path, and add the origin & destination objects to the nodes list
|
|
|
|
cable_paths = []
|
|
|
|
for cablepath in CablePath.objects.all():
|
|
|
|
|
|
|
|
# Origin
|
|
|
|
origin = compile_path_node(cablepath.origin_type_id, cablepath.origin_id)
|
|
|
|
cablepath.path.append([origin])
|
|
|
|
cablepath._nodes.insert(0, origin)
|
|
|
|
|
|
|
|
# Transit nodes
|
|
|
|
cablepath.path.extend([
|
|
|
|
[node] for node in cablepath._nodes[1:]
|
|
|
|
])
|
|
|
|
|
|
|
|
# Destination
|
|
|
|
if cablepath.destination_id:
|
|
|
|
destination = compile_path_node(cablepath.destination_type_id, cablepath.destination_id)
|
|
|
|
cablepath.path.append([destination])
|
|
|
|
cablepath._nodes.append(destination)
|
|
|
|
cablepath.is_complete = True
|
|
|
|
|
|
|
|
cable_paths.append(cablepath)
|
|
|
|
|
2022-05-13 16:46:55 -04:00
|
|
|
# Bulk update all CableTerminations
|
|
|
|
CablePath.objects.bulk_update(cable_paths, fields=('path', '_nodes', 'is_complete'), batch_size=100)
|
2022-05-03 11:32:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
2022-06-27 12:12:34 -04:00
|
|
|
('dcim', '0158_populate_cable_terminations'),
|
2022-05-03 11:32:52 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(
|
|
|
|
code=populate_cable_paths,
|
|
|
|
reverse_code=migrations.RunPython.noop
|
|
|
|
),
|
|
|
|
]
|