From 3ea2978df467409cbc033fee662ccd9e8df5b32b Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 2 Nov 2018 11:55:52 -0400 Subject: [PATCH] Standardized naming of reverse relationships from component templates to DeviceType --- netbox/dcim/filters.py | 12 +++---- netbox/dcim/forms.py | 2 +- .../dcim/migrations/0065_front_rear_ports.py | 32 +++++++++++++++---- netbox/dcim/models.py | 30 ++++++++--------- netbox/templates/dcim/devicetype.html | 20 ++++++------ 5 files changed, 58 insertions(+), 38 deletions(-) diff --git a/netbox/dcim/filters.py b/netbox/dcim/filters.py index 6def9d4c2..5321e00b7 100644 --- a/netbox/dcim/filters.py +++ b/netbox/dcim/filters.py @@ -355,19 +355,19 @@ class DeviceTypeFilter(CustomFieldFilterSet, django_filters.FilterSet): def _console_ports(self, queryset, name, value): value = value.strip() - return queryset.exclude(console_port_templates__isnull=bool(value)) + return queryset.exclude(consoleport_templates__isnull=bool(value)) def _console_server_ports(self, queryset, name, value): value = value.strip() - return queryset.exclude(cs_port_templates__isnull=bool(value)) + return queryset.exclude(consoleserverport_templates__isnull=bool(value)) def _power_ports(self, queryset, name, value): value = value.strip() - return queryset.exclude(power_port_templates__isnull=bool(value)) + return queryset.exclude(powerport_templates__isnull=bool(value)) def _power_outlets(self, queryset, name, value): value = value.strip() - return queryset.exclude(power_outlet_templates__isnull=bool(value)) + return queryset.exclude(poweroutlet_templates__isnull=bool(value)) def _interfaces(self, queryset, name, value): value = value.strip() @@ -376,8 +376,8 @@ class DeviceTypeFilter(CustomFieldFilterSet, django_filters.FilterSet): def _pass_through_ports(self, queryset, name, value): value = value.strip() return queryset.exclude( - front_port_templates__isnull=bool(value), - rear_port_templates__isnull=bool(value) + frontport_templates__isnull=bool(value), + rearport_templates__isnull=bool(value) ) diff --git a/netbox/dcim/forms.py b/netbox/dcim/forms.py index d4f1a6c55..56407e961 100644 --- a/netbox/dcim/forms.py +++ b/netbox/dcim/forms.py @@ -807,7 +807,7 @@ class FrontPortTemplateCreateForm(ComponentForm): # Determine which rear port positions are occupied. These will be excluded from the list of available mappings. occupied_port_positions = [ (front_port.rear_port_id, front_port.rear_port_position) - for front_port in self.parent.front_port_templates.all() + for front_port in self.parent.frontport_templates.all() ] # Populate rear port choices diff --git a/netbox/dcim/migrations/0065_front_rear_ports.py b/netbox/dcim/migrations/0065_front_rear_ports.py index 2e893c4b3..d647ff8f2 100644 --- a/netbox/dcim/migrations/0065_front_rear_ports.py +++ b/netbox/dcim/migrations/0065_front_rear_ports.py @@ -1,5 +1,3 @@ -# Generated by Django 2.0.8 on 2018-10-03 17:26 - import django.core.validators from django.db import migrations, models import django.db.models.deletion @@ -68,22 +66,22 @@ class Migration(migrations.Migration): migrations.AddField( model_name='rearporttemplate', name='device_type', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rear_port_templates', to='dcim.DeviceType'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rearport_templates', to='dcim.DeviceType'), ), migrations.AddField( model_name='frontporttemplate', name='device_type', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='front_port_templates', to='dcim.DeviceType'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='frontport_templates', to='dcim.DeviceType'), ), migrations.AddField( model_name='frontporttemplate', name='rear_port', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='front_port_templates', to='dcim.RearPortTemplate'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='frontport_templates', to='dcim.RearPortTemplate'), ), migrations.AddField( model_name='frontport', name='rear_port', - field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='front_ports', to='dcim.RearPort'), + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='frontports', to='dcim.RearPort'), ), migrations.AddField( model_name='frontport', @@ -106,4 +104,26 @@ class Migration(migrations.Migration): name='frontport', unique_together={('device', 'name'), ('rear_port', 'rear_port_position')}, ), + + # Rename reverse relationships of component templates to DeviceType + migrations.AlterField( + model_name='consoleporttemplate', + name='device_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='consoleport_templates', to='dcim.DeviceType'), + ), + migrations.AlterField( + model_name='consoleserverporttemplate', + name='device_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='consoleserverport_templates', to='dcim.DeviceType'), + ), + migrations.AlterField( + model_name='poweroutlettemplate', + name='device_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='poweroutlet_templates', to='dcim.DeviceType'), + ), + migrations.AlterField( + model_name='powerporttemplate', + name='device_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='powerport_templates', to='dcim.DeviceType'), + ), ] diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 9ed94b45c..be70591a7 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -965,7 +965,7 @@ class ConsolePortTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='console_port_templates' + related_name='consoleport_templates' ) name = models.CharField( max_length=50 @@ -986,7 +986,7 @@ class ConsoleServerPortTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='cs_port_templates' + related_name='consoleserverport_templates' ) name = models.CharField( max_length=50 @@ -1007,7 +1007,7 @@ class PowerPortTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='power_port_templates' + related_name='powerport_templates' ) name = models.CharField( max_length=50 @@ -1028,7 +1028,7 @@ class PowerOutletTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='power_outlet_templates' + related_name='poweroutlet_templates' ) name = models.CharField( max_length=50 @@ -1080,7 +1080,7 @@ class FrontPortTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='front_port_templates' + related_name='frontport_templates' ) name = models.CharField( max_length=64 @@ -1091,7 +1091,7 @@ class FrontPortTemplate(ComponentTemplateModel): rear_port = models.ForeignKey( to='dcim.RearPortTemplate', on_delete=models.CASCADE, - related_name='front_port_templates' + related_name='frontport_templates' ) rear_port_position = models.PositiveSmallIntegerField( default=1, @@ -1132,7 +1132,7 @@ class RearPortTemplate(ComponentTemplateModel): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.CASCADE, - related_name='rear_port_templates' + related_name='rearport_templates' ) name = models.CharField( max_length=64 @@ -1552,19 +1552,19 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): if is_new: ConsolePort.objects.bulk_create( [ConsolePort(device=self, name=template.name) for template in - self.device_type.console_port_templates.all()] + self.device_type.consoleport_templates.all()] ) ConsoleServerPort.objects.bulk_create( [ConsoleServerPort(device=self, name=template.name) for template in - self.device_type.cs_port_templates.all()] + self.device_type.consoleserverport_templates.all()] ) PowerPort.objects.bulk_create( [PowerPort(device=self, name=template.name) for template in - self.device_type.power_port_templates.all()] + self.device_type.powerport_templates.all()] ) PowerOutlet.objects.bulk_create( [PowerOutlet(device=self, name=template.name) for template in - self.device_type.power_outlet_templates.all()] + self.device_type.poweroutlet_templates.all()] ) Interface.objects.bulk_create( [Interface(device=self, name=template.name, form_factor=template.form_factor, @@ -1576,7 +1576,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): name=template.name, type=template.type, positions=template.positions - ) for template in self.device_type.rear_port_templates.all() + ) for template in self.device_type.rearport_templates.all() ]) FrontPort.objects.bulk_create([ FrontPort( @@ -1585,7 +1585,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel): type=template.type, rear_port=RearPort.objects.get(device=self, name=template.rear_port.name), rear_port_position=template.rear_port_position, - ) for template in self.device_type.front_port_templates.all() + ) for template in self.device_type.frontport_templates.all() ]) DeviceBay.objects.bulk_create( [DeviceBay(device=self, name=template.name) for template in @@ -2120,7 +2120,7 @@ class FrontPort(CableTermination, ComponentModel): rear_port = models.ForeignKey( to='dcim.RearPort', on_delete=models.CASCADE, - related_name='front_ports' + related_name='frontports' ) rear_port_position = models.PositiveSmallIntegerField( default=1, @@ -2546,7 +2546,7 @@ class Cable(ChangeLoggedModel): if next_cable is None: return None - far_end = next_cable.termination_b if next_cable.termination_a == peer_port else next_cable.terimation_a + far_end = next_cable.termination_b if next_cable.termination_a == peer_port else next_cable.termination_a # Return the far side termination of the cable return trace_cable(far_end, position) diff --git a/netbox/templates/dcim/devicetype.html b/netbox/templates/dcim/devicetype.html index 082726ef6..89225e7e6 100644 --- a/netbox/templates/dcim/devicetype.html +++ b/netbox/templates/dcim/devicetype.html @@ -136,7 +136,7 @@ -{% if devicetype.console_port_templates.exists or devicetype.power_port_templates.exists %} +{% if devicetype.consoleport_templates.exists or devicetype.powerport_templates.exists %}
{% include 'dcim/inc/devicetype_component_table.html' with table=consoleport_table title='Console Ports' add_url='dcim:devicetype_add_consoleport' delete_url='dcim:devicetype_delete_consoleport' %} @@ -153,13 +153,20 @@
{% endif %} -{% if devicetype.cs_port_templates.exists %} +{% if devicetype.consoleserverport_templates.exists %}
{% include 'dcim/inc/devicetype_component_table.html' with table=consoleserverport_table title='Console Server Ports' add_url='dcim:devicetype_add_consoleserverport' delete_url='dcim:devicetype_delete_consoleserverport' %}
{% endif %} +{% if devicetype.poweroutlet_templates.exists %} +
+
+ {% include 'dcim/inc/devicetype_component_table.html' with table=poweroutlet_table title='Power Outlets' add_url='dcim:devicetype_add_poweroutlet' delete_url='dcim:devicetype_delete_poweroutlet' %} +
+
+{% endif %} {% if devicetype.interface_templates.exists %}
@@ -167,14 +174,7 @@
{% endif %} -{% if devicetype.power_outlet_templates.exists %} -
-
- {% include 'dcim/inc/devicetype_component_table.html' with table=poweroutlet_table title='Power Outlets' add_url='dcim:devicetype_add_poweroutlet' delete_url='dcim:devicetype_delete_poweroutlet' %} -
-
-{% endif %} -{% if devicetype.front_port_templates.exists or devicetype.rear_port_templates.exists %} +{% if devicetype.frontport_templates.exists or devicetype.rearport_templates.exists %}
{% include 'dcim/inc/devicetype_component_table.html' with table=front_port_table title='Front Ports' add_url='dcim:devicetype_add_frontport' delete_url='dcim:devicetype_delete_frontport' %}