Compare commits

...

25 Commits

Author SHA1 Message Date
Arthur
d18ef98b00 fix dropdown sorting
CI / build (20.x, 3.12) (push) Failing after 18s
CI / build (20.x, 3.13) (push) Failing after 17s
CI / build (20.x, 3.14) (push) Failing after 15s
2026-02-04 11:57:23 -08:00
Arthur
8aab80b56f merge main 2026-02-04 11:44:22 -08:00
Martin Hauser
ee6cbdcefe Fixes #21320: Prevent Rack validation errors when site or optional fields are missing during import (#21321)
CI / build (20.x, 3.12) (push) Failing after 18s
CI / build (20.x, 3.13) (push) Failing after 14s
CI / build (20.x, 3.14) (push) Failing after 12s
CodeQL / Analyze (actions) (push) Failing after 24s
CodeQL / Analyze (javascript-typescript) (push) Failing after 30s
CodeQL / Analyze (python) (push) Failing after 27s
2026-02-03 09:32:07 -06:00
Arthur
905d17294a Review feedback
CI / build (20.x, 3.12) (push) Failing after 10s
CI / build (20.x, 3.13) (push) Failing after 11s
CI / build (20.x, 3.14) (push) Failing after 11s
2026-01-28 17:27:01 -08:00
Arthur
44e5a4c177 Review feedback 2026-01-28 17:12:13 -08:00
Arthur
de19447317 Merge branch 'main' into 20911-dropdown
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CI / build (20.x, 3.14) (push) Has been cancelled
2026-01-23 15:59:08 -08:00
Arthur
f195af206b fix csv import 2026-01-23 15:46:26 -08:00
Arthur
b0ac55ed6a cleanup
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CI / build (20.x, 3.14) (push) Has been cancelled
2026-01-21 16:44:48 -08:00
Arthur
91ab818411 use bulk_update and rebuild 2026-01-21 16:23:24 -08:00
Arthur
62b9367ae3 use bulk_update and rebuild 2026-01-21 16:14:14 -08:00
Arthur
0c091aa80e cleanup 2026-01-21 13:18:34 -08:00
Arthur
94836e5a37 fix migration 2026-01-21 12:55:34 -08:00
Arthur
c92912ff03 fix migration 2026-01-21 12:52:41 -08:00
Arthur
ef0bc18095 fix migration 2026-01-21 12:47:16 -08:00
Arthur
99f727e685 fix migration 2026-01-21 12:41:59 -08:00
Arthur
6a5aced4bc fix migration 2026-01-21 12:28:01 -08:00
Arthur
46f9a12a87 add migration 2026-01-21 11:59:03 -08:00
Arthur
be1a008216 rebuild tree after rename
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CI / build (20.x, 3.14) (push) Has been cancelled
2026-01-20 15:28:49 -08:00
Arthur
c4c3518bb4 change ordering field, remove front-end changes 2026-01-20 13:45:17 -08:00
Arthur
5a1282e326 Merge branch 'main' into 20911-dropdown
CI / build (20.x, 3.12) (push) Has been cancelled
CI / build (20.x, 3.13) (push) Has been cancelled
CI / build (20.x, 3.14) (push) Has been cancelled
2026-01-14 13:39:45 -08:00
Arthur
cb13eb277f use correct node version 2026-01-14 13:36:33 -08:00
Arthur
24642be351 cleanup 2026-01-08 17:08:10 -08:00
Arthur
89af9efd85 cleanup 2026-01-08 17:04:00 -08:00
Arthur
99d678502f cleanup 2026-01-08 16:23:47 -08:00
Arthur
e6300ee06d Fix TomSelect dropdown ordering 2026-01-08 16:17:40 -08:00
6 changed files with 92 additions and 30 deletions
+2 -1
View File
@@ -745,7 +745,8 @@ class ModuleForm(ModuleCommonForm, PrimaryModelForm):
label=_('Module bay'), label=_('Module bay'),
queryset=ModuleBay.objects.all(), queryset=ModuleBay.objects.all(),
query_params={ query_params={
'device_id': '$device' 'device_id': '$device',
'ordering': 'name',
}, },
context={ context={
'disabled': 'installed_module', 'disabled': 'installed_module',
@@ -0,0 +1,32 @@
from django.db import migrations
import mptt.managers
import mptt.models
def rebuild_mptt(apps, schema_editor):
"""
Rebuild the MPTT tree for ModuleBay to apply new ordering.
"""
ModuleBay = apps.get_model('dcim', 'ModuleBay')
# Set MPTTMeta with the correct order_insertion_by
class MPTTMeta:
order_insertion_by = ('module', 'name',)
ModuleBay.MPTTMeta = MPTTMeta
ModuleBay._mptt_meta = mptt.models.MPTTOptions(MPTTMeta)
manager = mptt.managers.TreeManager()
manager.model = ModuleBay
manager.contribute_to_class(ModuleBay, 'objects')
manager.rebuild()
class Migration(migrations.Migration):
dependencies = [
('dcim', '0225_gfk_indexes'),
]
operations = [
migrations.RunPython(code=rebuild_mptt, reverse_code=migrations.RunPython.noop),
]
+1 -1
View File
@@ -1273,7 +1273,7 @@ class ModuleBay(ModularComponentModel, TrackingModelMixin, MPTTModel):
verbose_name_plural = _('module bays') verbose_name_plural = _('module bays')
class MPTTMeta: class MPTTMeta:
order_insertion_by = ('module',) order_insertion_by = ('module', 'name',)
def clean(self): def clean(self):
super().clean() super().clean()
+11 -2
View File
@@ -5,6 +5,7 @@ from django.db import models
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from jsonschema.exceptions import ValidationError as JSONValidationError from jsonschema.exceptions import ValidationError as JSONValidationError
from mptt.models import MPTTModel
from dcim.choices import * from dcim.choices import *
from dcim.utils import create_port_mappings, update_interface_bridges from dcim.utils import create_port_mappings, update_interface_bridges
@@ -331,7 +332,8 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
component._location = self.device.location component._location = self.device.location
component._rack = self.device.rack component._rack = self.device.rack
if component_model is not ModuleBay: # we handle create and update separately - this is for create
if not issubclass(component_model, MPTTModel):
component_model.objects.bulk_create(create_instances) component_model.objects.bulk_create(create_instances)
# Emit the post_save signal for each newly created object # Emit the post_save signal for each newly created object
for component in create_instances: for component in create_instances:
@@ -344,11 +346,13 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
update_fields=None update_fields=None
) )
else: else:
# ModuleBays must be saved individually for MPTT # MPTT models must be saved individually to maintain tree structure
for instance in create_instances: for instance in create_instances:
instance.save() instance.save()
update_fields = ['module'] update_fields = ['module']
# we handle create and update separately - this is for update
component_model.objects.bulk_update(update_instances, update_fields) component_model.objects.bulk_update(update_instances, update_fields)
# Emit the post_save signal for each updated object # Emit the post_save signal for each updated object
for component in update_instances: for component in update_instances:
@@ -361,7 +365,12 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
update_fields=update_fields update_fields=update_fields
) )
# Rebuild MPTT tree if needed (bulk_update bypasses model save)
if issubclass(component_model, MPTTModel) and update_instances:
component_model.objects.rebuild()
# Replicate any front/rear port mappings from the ModuleType # Replicate any front/rear port mappings from the ModuleType
create_port_mappings(self.device, self.module_type, self) create_port_mappings(self.device, self.module_type, self)
# Interface bridges have to be set after interface instantiation # Interface bridges have to be set after interface instantiation
update_interface_bridges(self.device, self.module_type.interfacetemplates, self) update_interface_bridges(self.device, self.module_type.interfacetemplates, self)
+1 -1
View File
@@ -373,7 +373,7 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, TrackingModelMixin, RackBase):
super().clean() super().clean()
# Validate location/site assignment # Validate location/site assignment
if self.site and self.location and self.location.site != self.site: if self.site_id and self.location_id and self.location.site_id != self.site_id:
raise ValidationError(_("Assigned location must belong to parent site ({site}).").format(site=self.site)) raise ValidationError(_("Assigned location must belong to parent site ({site}).").format(site=self.site))
# Validate outer dimensions and unit # Validate outer dimensions and unit
+45 -25
View File
@@ -437,30 +437,12 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
""" """
return object_form.save() return object_form.save()
def create_and_update_objects(self, form, request): def _process_import_records(self, form, request, records, prefetched_objects):
"""
Process CSV import records and save objects.
"""
saved_objects = [] saved_objects = []
records = list(form.cleaned_data['data'])
# Prefetch objects to be updated, if any
prefetch_ids = [int(record['id']) for record in records if record.get('id')]
# check for duplicate IDs
duplicate_pks = [pk for pk, count in Counter(prefetch_ids).items() if count > 1]
if duplicate_pks:
error_msg = _(
"Duplicate objects found: {model} with ID(s) {ids} appears multiple times"
).format(
model=title(self.queryset.model._meta.verbose_name),
ids=', '.join(str(pk) for pk in sorted(duplicate_pks))
)
raise ValidationError(error_msg)
prefetched_objects = {
obj.pk: obj
for obj in self.queryset.model.objects.filter(id__in=prefetch_ids)
} if prefetch_ids else {}
for i, record in enumerate(records, start=1): for i, record in enumerate(records, start=1):
object_id = int(record.pop('id')) if record.get('id') else None object_id = int(record.pop('id')) if record.get('id') else None
@@ -524,6 +506,37 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
return saved_objects return saved_objects
def create_and_update_objects(self, form, request):
records = list(form.cleaned_data['data'])
# Prefetch objects to be updated, if any
prefetch_ids = [int(record['id']) for record in records if record.get('id')]
# check for duplicate IDs
duplicate_pks = [pk for pk, count in Counter(prefetch_ids).items() if count > 1]
if duplicate_pks:
error_msg = _(
"Duplicate objects found: {model} with ID(s) {ids} appears multiple times"
).format(
model=title(self.queryset.model._meta.verbose_name),
ids=', '.join(str(pk) for pk in sorted(duplicate_pks))
)
raise ValidationError(error_msg)
prefetched_objects = {
obj.pk: obj
for obj in self.queryset.model.objects.filter(id__in=prefetch_ids)
} if prefetch_ids else {}
# For MPTT models, delay tree updates until all saves are complete
if issubclass(self.queryset.model, MPTTModel):
with self.queryset.model.objects.delay_mptt_updates():
saved_objects = self._process_import_records(form, request, records, prefetched_objects)
else:
saved_objects = self._process_import_records(form, request, records, prefetched_objects)
return saved_objects
# #
# Request handlers # Request handlers
# #
@@ -893,9 +906,16 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView):
renamed_pks = self._rename_objects(form, selected_objects) renamed_pks = self._rename_objects(form, selected_objects)
if '_apply' in request.POST: if '_apply' in request.POST:
for obj in selected_objects: # For MPTT models, delay tree updates until all saves are complete
setattr(obj, self.field_name, obj.new_name) if issubclass(self.queryset.model, MPTTModel):
obj.save() with self.queryset.model.objects.delay_mptt_updates():
for obj in selected_objects:
setattr(obj, self.field_name, obj.new_name)
obj.save()
else:
for obj in selected_objects:
setattr(obj, self.field_name, obj.new_name)
obj.save()
# Enforce constrained permissions # Enforce constrained permissions
if self.queryset.filter(pk__in=renamed_pks).count() != len(selected_objects): if self.queryset.filter(pk__in=renamed_pks).count() != len(selected_objects):