mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-22 03:28:45 -06:00
Compare commits
11 Commits
20911-drop
...
20911-drop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0ac55ed6a | ||
|
|
91ab818411 | ||
|
|
62b9367ae3 | ||
|
|
0c091aa80e | ||
|
|
94836e5a37 | ||
|
|
c92912ff03 | ||
|
|
ef0bc18095 | ||
|
|
99f727e685 | ||
|
|
6a5aced4bc | ||
|
|
46f9a12a87 | ||
|
|
be1a008216 |
32
netbox/dcim/migrations/0226_modulebay_rebuild_tree.py
Normal file
32
netbox/dcim/migrations/0226_modulebay_rebuild_tree.py
Normal file
@@ -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),
|
||||||
|
]
|
||||||
@@ -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 = ('name',)
|
order_insertion_by = ('module', 'name',)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
|||||||
@@ -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 update_interface_bridges
|
from dcim.utils import update_interface_bridges
|
||||||
@@ -329,7 +330,7 @@ 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:
|
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:
|
||||||
@@ -342,11 +343,12 @@ 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']
|
||||||
|
|
||||||
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:
|
||||||
@@ -359,5 +361,9 @@ 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()
|
||||||
|
|
||||||
# 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)
|
||||||
|
|||||||
@@ -895,6 +895,15 @@ 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 MPTT models, delay tree updates until all saves are complete
|
||||||
|
if issubclass(self.queryset.model, MPTTModel):
|
||||||
|
with self.queryset.model.objects.delay_mptt_updates():
|
||||||
|
for obj in selected_objects:
|
||||||
|
setattr(obj, self.field_name, obj.new_name)
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
self.queryset.model.objects.rebuild()
|
||||||
|
else:
|
||||||
for obj in selected_objects:
|
for obj in selected_objects:
|
||||||
setattr(obj, self.field_name, obj.new_name)
|
setattr(obj, self.field_name, obj.new_name)
|
||||||
obj.save()
|
obj.save()
|
||||||
|
|||||||
Reference in New Issue
Block a user