Skip to content

Commit c6a2705

Browse files
committed
Address PR feedback, ensure that parent GFK underlying fields are not nullable
This changes an existing (within the branch) migration, so if you're testing this you'll need to back up migrations and re-run.
1 parent 5ffc048 commit c6a2705

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

netbox/ipam/forms/bulk_import.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ class ServiceImportForm(NetBoxModelImportForm):
555555
to_field_name='name',
556556
help_text=_('Parent object name')
557557
)
558+
parent_object_id = forms.IntegerField(
559+
required=False,
560+
help_text=_('Parent object ID'),
561+
)
558562
protocol = CSVChoiceField(
559563
label=_('Protocol'),
560564
choices=ServiceProtocolChoices,
@@ -570,8 +574,7 @@ class ServiceImportForm(NetBoxModelImportForm):
570574
class Meta:
571575
model = Service
572576
fields = (
573-
'parent_object_type', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments',
574-
'tags', 'parent_object_id',
577+
'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags',
575578
)
576579

577580
def __init__(self, data=None, *args, **kwargs):
@@ -603,16 +606,20 @@ def clean(self):
603606
parent = parent_ct.model_class().objects.filter(id=parent_id).first()
604607
self.cleaned_data['parent'] = parent
605608
else:
606-
# If a parent object type is passed and we've made it to here, then raise a validation
607-
# error since an associated parent object or parent object id has nto be passed
609+
# If a parent object type is passed and we've made it here, then raise a validation
610+
# error since an associated parent object or parent object id has not been passed
608611
raise forms.ValidationError(
609-
_("One of parent or parent_object_id needs to be included with parent_object_type")
612+
_("One of parent or parent_object_id must be included with parent_object_type")
610613
)
611614

615+
# making sure parent is defined. In cases where an import is resulting in an update, the
616+
# import data might not include the parent object and so the above logic might not be
617+
# triggered
618+
parent = self.cleaned_data.get('parent')
612619
for ip_address in self.cleaned_data.get('ipaddresses', []):
613620
if not ip_address.assigned_object or getattr(ip_address.assigned_object, 'parent_object') != parent:
614621
raise forms.ValidationError(
615-
_("{ip} is not assigned to this device/VM.").format(ip=ip_address)
622+
_("{ip} is not assigned to this parent.").format(ip=ip_address)
616623
)
617624

618625
return self.cleaned_data

netbox/ipam/migrations/0080_remove_service_device_virtual_machine_add_parent_gfk_index.py

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ class Migration(migrations.Migration):
1818
model_name='service',
1919
name='virtual_machine',
2020
),
21+
migrations.AlterField(
22+
model_name='service',
23+
name='parent_object_id',
24+
field=models.PositiveBigIntegerField(),
25+
),
26+
migrations.AlterField(
27+
model_name='service',
28+
name='parent_object_type',
29+
field=models.ForeignKey(
30+
on_delete=models.deletion.PROTECT, related_name='+', to='contenttypes.contenttype'
31+
),
32+
),
2133
migrations.AddIndex(
2234
model_name='service',
2335
index=models.Index(

netbox/ipam/models/services.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,8 @@ class Service(ContactsMixin, ServiceBase, PrimaryModel):
6868
to='contenttypes.ContentType',
6969
on_delete=models.PROTECT,
7070
related_name='+',
71-
blank=True,
72-
null=True
73-
)
74-
parent_object_id = models.PositiveBigIntegerField(
75-
blank=True,
76-
null=True
7771
)
72+
parent_object_id = models.PositiveBigIntegerField()
7873
parent = GenericForeignKey(
7974
ct_field='parent_object_type',
8075
fk_field='parent_object_id'

netbox/ipam/tests/test_filtersets.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1243,9 +1243,24 @@ def setUpTestData(cls):
12431243
IPAddress.objects.bulk_create(ipaddresses)
12441244

12451245
services = (
1246-
Service(name='Service 1', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[1]),
1247-
Service(name='Service 2', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[1]),
1248-
Service(name='Service 3', protocol=ServiceProtocolChoices.PROTOCOL_TCP, ports=[1]),
1246+
Service(
1247+
parent=devices[0],
1248+
name='Service 1',
1249+
protocol=ServiceProtocolChoices.PROTOCOL_TCP,
1250+
ports=[1],
1251+
),
1252+
Service(
1253+
parent=devices[1],
1254+
name='Service 2',
1255+
protocol=ServiceProtocolChoices.PROTOCOL_TCP,
1256+
ports=[1],
1257+
),
1258+
Service(
1259+
parent=devices[2],
1260+
name='Service 3',
1261+
protocol=ServiceProtocolChoices.PROTOCOL_TCP,
1262+
ports=[1],
1263+
),
12491264
)
12501265
Service.objects.bulk_create(services)
12511266
services[0].ipaddresses.add(ipaddresses[0])

0 commit comments

Comments
 (0)