mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Preliminary work on Cables
This commit is contained in:
178
netbox/dcim/migrations/0066_cables.py
Normal file
178
netbox/dcim/migrations/0066_cables.py
Normal file
@@ -0,0 +1,178 @@
|
||||
# Generated by Django 2.0.8 on 2018-10-18 19:41
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import utilities.fields
|
||||
|
||||
|
||||
def console_connections_to_cables(apps, schema_editor):
|
||||
"""
|
||||
Copy all existing console connections as Cables
|
||||
"""
|
||||
ConsolePort = apps.get_model('dcim', 'ConsolePort')
|
||||
Cable = apps.get_model('dcim', 'Cable')
|
||||
|
||||
# Load content types
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
consoleport_type = ContentType.objects.get(app_label='dcim', model='consoleport')
|
||||
consoleserverport_type = ContentType.objects.get(app_label='dcim', model='consoleserverport')
|
||||
|
||||
# Create a new Cable instance from each console connection
|
||||
for consoleport in ConsolePort.objects.filter(cs_port__isnull=False):
|
||||
c = Cable()
|
||||
# We have to assign GFK fields manually because we're inside a migration.
|
||||
c.endpoint_a_type = consoleport_type
|
||||
c.endpoint_a_id = consoleport.id
|
||||
c.endpoint_b_type = consoleserverport_type
|
||||
c.endpoint_b_id = consoleport.cs_port_id
|
||||
c.connection_status = consoleport.connection_status
|
||||
c.save()
|
||||
|
||||
|
||||
def power_connections_to_cables(apps, schema_editor):
|
||||
"""
|
||||
Copy all existing power connections as Cables
|
||||
"""
|
||||
PowerPort = apps.get_model('dcim', 'PowerPort')
|
||||
Cable = apps.get_model('dcim', 'Cable')
|
||||
|
||||
# Load content types
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
powerport_type = ContentType.objects.get(app_label='dcim', model='powerport')
|
||||
poweroutlet_type = ContentType.objects.get(app_label='dcim', model='poweroutlet')
|
||||
|
||||
# Create a new Cable instance from each power connection
|
||||
for powerport in PowerPort.objects.filter(power_outlet__isnull=False):
|
||||
c = Cable()
|
||||
# We have to assign GFK fields manually because we're inside a migration.
|
||||
c.endpoint_a_type = powerport_type
|
||||
c.endpoint_a_id = powerport.id
|
||||
c.endpoint_b_type = poweroutlet_type
|
||||
c.endpoint_b_id = powerport.power_outlet_id
|
||||
c.connection_status = powerport.connection_status
|
||||
c.save()
|
||||
|
||||
|
||||
def interface_connections_to_cables(apps, schema_editor):
|
||||
"""
|
||||
Copy all InterfaceConnections as Cables
|
||||
"""
|
||||
InterfaceConnection = apps.get_model('dcim', 'InterfaceConnection')
|
||||
Cable = apps.get_model('dcim', 'Cable')
|
||||
|
||||
# Load content types
|
||||
ContentType = apps.get_model('contenttypes', 'ContentType')
|
||||
interface_type = ContentType.objects.get(app_label='dcim', model='interface')
|
||||
|
||||
# Create a new Cable instance from each InterfaceConnection
|
||||
for conn in InterfaceConnection.objects.all():
|
||||
c = Cable()
|
||||
# We have to assign GFK fields manually because we're inside a migration.
|
||||
c.endpoint_a_type = interface_type
|
||||
c.endpoint_a_id = conn.interface_a_id
|
||||
c.endpoint_b_type = interface_type
|
||||
c.endpoint_b_id = conn.interface_b_id
|
||||
c.connection_status = conn.connection_status
|
||||
c.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contenttypes', '0002_remove_content_type_name'),
|
||||
('dcim', '0065_patch_panel_ports'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Cable',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
|
||||
('created', models.DateField(auto_now_add=True, null=True)),
|
||||
('last_updated', models.DateTimeField(auto_now=True, null=True)),
|
||||
('endpoint_a_id', models.PositiveIntegerField()),
|
||||
('endpoint_b_id', models.PositiveIntegerField()),
|
||||
('type', models.PositiveSmallIntegerField(blank=True, null=True)),
|
||||
('status', models.BooleanField(default=True)),
|
||||
('label', models.CharField(blank=True, max_length=100)),
|
||||
('color', utilities.fields.ColorField(blank=True, max_length=6)),
|
||||
('endpoint_a_type', models.ForeignKey(limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontpanelport', 'rearpanelport')}, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType')),
|
||||
('endpoint_b_type', models.ForeignKey(limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport', 'frontpanelport', 'rearpanelport')}, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType')),
|
||||
],
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='consoleport',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='consoleport',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport')}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='consoleserverport',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='consoleserverport',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport')}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='consoleserverport',
|
||||
name='connection_status',
|
||||
field=models.NullBooleanField(default=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport')}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='interface',
|
||||
name='connection_status',
|
||||
field=models.NullBooleanField(default=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='poweroutlet',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='poweroutlet',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport')}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='poweroutlet',
|
||||
name='connection_status',
|
||||
field=models.NullBooleanField(default=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='powerport',
|
||||
name='connected_endpoint_id',
|
||||
field=models.PositiveIntegerField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='powerport',
|
||||
name='connected_endpoint_type',
|
||||
field=models.ForeignKey(blank=True, limit_choices_to={'model__in': ('consoleport', 'consoleserverport', 'interface', 'poweroutlet', 'powerport')}, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='+', to='contenttypes.ContentType'),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='cable',
|
||||
unique_together={('endpoint_b_type', 'endpoint_b_id'), ('endpoint_a_type', 'endpoint_a_id')},
|
||||
),
|
||||
|
||||
# Copy console/power/interface connections as Cables
|
||||
migrations.RunPython(console_connections_to_cables),
|
||||
migrations.RunPython(power_connections_to_cables),
|
||||
migrations.RunPython(interface_connections_to_cables),
|
||||
|
||||
]
|
Reference in New Issue
Block a user