mirror of
https://github.com/netbox-community/netbox.git
synced 2026-02-10 19:07:46 +01:00
Compare commits
4 Commits
20911-drop
...
21240-redi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3be2b6d54 | ||
|
|
97847662ac | ||
|
|
c23b656c26 | ||
|
|
6062aa71b1 |
@@ -516,7 +516,7 @@ class RearPortViewSet(PassThroughPortMixin, NetBoxModelViewSet):
|
||||
|
||||
|
||||
class ModuleBayViewSet(NetBoxModelViewSet):
|
||||
queryset = ModuleBay.objects.order_by('device', 'module', 'name')
|
||||
queryset = ModuleBay.objects.all()
|
||||
serializer_class = serializers.ModuleBaySerializer
|
||||
filterset_class = filtersets.ModuleBayFilterSet
|
||||
|
||||
|
||||
@@ -745,8 +745,7 @@ class ModuleForm(ModuleCommonForm, PrimaryModelForm):
|
||||
label=_('Module bay'),
|
||||
queryset=ModuleBay.objects.all(),
|
||||
query_params={
|
||||
'device_id': '$device',
|
||||
'ordering': 'module,name',
|
||||
'device_id': '$device'
|
||||
},
|
||||
context={
|
||||
'disabled': 'installed_module',
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
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')
|
||||
|
||||
class MPTTMeta:
|
||||
order_insertion_by = ('module', 'name',)
|
||||
order_insertion_by = ('module',)
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
|
||||
@@ -5,7 +5,6 @@ from django.db import models
|
||||
from django.db.models.signals import post_save
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from jsonschema.exceptions import ValidationError as JSONValidationError
|
||||
from mptt.models import MPTTModel
|
||||
|
||||
from dcim.choices import *
|
||||
from dcim.utils import create_port_mappings, update_interface_bridges
|
||||
@@ -332,8 +331,7 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
|
||||
component._location = self.device.location
|
||||
component._rack = self.device.rack
|
||||
|
||||
# we handle create and update separately - this is for create
|
||||
if not issubclass(component_model, MPTTModel):
|
||||
if component_model is not ModuleBay:
|
||||
component_model.objects.bulk_create(create_instances)
|
||||
# Emit the post_save signal for each newly created object
|
||||
for component in create_instances:
|
||||
@@ -346,13 +344,11 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
|
||||
update_fields=None
|
||||
)
|
||||
else:
|
||||
# MPTT models must be saved individually to maintain tree structure
|
||||
# ModuleBays must be saved individually for MPTT
|
||||
for instance in create_instances:
|
||||
instance.save()
|
||||
|
||||
update_fields = ['module']
|
||||
|
||||
# we handle create and update separately - this is for update
|
||||
component_model.objects.bulk_update(update_instances, update_fields)
|
||||
# Emit the post_save signal for each updated object
|
||||
for component in update_instances:
|
||||
@@ -365,12 +361,7 @@ class Module(TrackingModelMixin, PrimaryModel, ConfigContextModel):
|
||||
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
|
||||
create_port_mappings(self.device, self.module_type, self)
|
||||
|
||||
# Interface bridges have to be set after interface instantiation
|
||||
update_interface_bridges(self.device, self.module_type.interfacetemplates, self)
|
||||
|
||||
@@ -43,6 +43,16 @@ REDIS = {
|
||||
# 'INSECURE_SKIP_TLS_VERIFY': False,
|
||||
# Set a path to a certificate authority, typically used with a self signed certificate.
|
||||
# 'CA_CERT_PATH': '/etc/ssl/certs/ca.crt',
|
||||
# Advanced Redis client parameters (SSL/TLS, timeouts, etc.)
|
||||
# Passed directly to redis-py. See: https://redis-py.readthedocs.io/en/stable/connections.html
|
||||
# NOTE: The CA_CERT_PATH setting above is already mapped to 'ssl_ca_certs' in KWARGS.
|
||||
# Only override these parameters in KWARGS if you have a specific reason to do so.
|
||||
# 'KWARGS': {
|
||||
# 'ssl_certfile': '/path/to/client-cert.pem',
|
||||
# 'ssl_keyfile': '/path/to/client-key.pem',
|
||||
# 'ssl_min_version': ssl.TLSVersion.TLSv1_2,
|
||||
# 'ssl_ciphers': 'HIGH:!aNULL',
|
||||
# },
|
||||
},
|
||||
'caching': {
|
||||
'HOST': 'localhost',
|
||||
@@ -59,6 +69,17 @@ REDIS = {
|
||||
# 'INSECURE_SKIP_TLS_VERIFY': False,
|
||||
# Set a path to a certificate authority, typically used with a self signed certificate.
|
||||
# 'CA_CERT_PATH': '/etc/ssl/certs/ca.crt',
|
||||
# Advanced Redis client parameters (SSL/TLS, timeouts, etc.)
|
||||
# Passed directly to Redis connection pool. See: https://github.com/jazzband/django-redis#configure-as-cache-backend
|
||||
# NOTE: The INSECURE_SKIP_TLS_VERIFY setting above is already mapped to 'ssl_cert_reqs' and
|
||||
# CA_CERT_PATH is mapped to 'ssl_ca_certs' in KWARGS. Only override these parameters
|
||||
# in KWARGS if you have a specific reason to do so.
|
||||
# 'KWARGS': {
|
||||
# 'ssl_certfile': '/path/to/client-cert.pem',
|
||||
# 'ssl_keyfile': '/path/to/client-key.pem',
|
||||
# 'ssl_min_version': ssl.TLSVersion.TLSv1_2,
|
||||
# 'ssl_ciphers': 'HIGH:!aNULL',
|
||||
# },
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -408,6 +408,12 @@ if CACHING_REDIS_CA_CERT_PATH:
|
||||
CACHES['default']['OPTIONS'].setdefault('CONNECTION_POOL_KWARGS', {})
|
||||
CACHES['default']['OPTIONS']['CONNECTION_POOL_KWARGS']['ssl_ca_certs'] = CACHING_REDIS_CA_CERT_PATH
|
||||
|
||||
# Merge in KWARGS for additional parameters
|
||||
caching_redis_kwargs = REDIS['caching'].get('KWARGS')
|
||||
if caching_redis_kwargs:
|
||||
CACHES['default']['OPTIONS'].setdefault('CONNECTION_POOL_KWARGS', {})
|
||||
CACHES['default']['OPTIONS']['CONNECTION_POOL_KWARGS'].update(caching_redis_kwargs)
|
||||
|
||||
|
||||
#
|
||||
# Sessions
|
||||
@@ -817,6 +823,12 @@ if TASKS_REDIS_CA_CERT_PATH:
|
||||
RQ_PARAMS.setdefault('REDIS_CLIENT_KWARGS', {})
|
||||
RQ_PARAMS['REDIS_CLIENT_KWARGS']['ssl_ca_certs'] = TASKS_REDIS_CA_CERT_PATH
|
||||
|
||||
# Merge in KWARGS for additional parameters
|
||||
tasks_redis_kwargs = TASKS_REDIS.get('KWARGS')
|
||||
if tasks_redis_kwargs:
|
||||
RQ_PARAMS.setdefault('REDIS_CLIENT_KWARGS', {})
|
||||
RQ_PARAMS['REDIS_CLIENT_KWARGS'].update(tasks_redis_kwargs)
|
||||
|
||||
# Define named RQ queues
|
||||
RQ_QUEUES = {
|
||||
RQ_QUEUE_HIGH: RQ_PARAMS,
|
||||
|
||||
@@ -437,12 +437,30 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
"""
|
||||
return object_form.save()
|
||||
|
||||
def _process_import_records(self, form, request, records, prefetched_objects):
|
||||
"""
|
||||
Process CSV import records and save objects.
|
||||
"""
|
||||
def create_and_update_objects(self, form, request):
|
||||
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):
|
||||
object_id = int(record.pop('id')) if record.get('id') else None
|
||||
|
||||
@@ -506,37 +524,6 @@ class BulkImportView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
|
||||
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
|
||||
#
|
||||
@@ -906,16 +893,9 @@ class BulkRenameView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
renamed_pks = self._rename_objects(form, selected_objects)
|
||||
|
||||
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()
|
||||
else:
|
||||
for obj in selected_objects:
|
||||
setattr(obj, self.field_name, obj.new_name)
|
||||
obj.save()
|
||||
for obj in selected_objects:
|
||||
setattr(obj, self.field_name, obj.new_name)
|
||||
obj.save()
|
||||
|
||||
# Enforce constrained permissions
|
||||
if self.queryset.filter(pk__in=renamed_pks).count() != len(selected_objects):
|
||||
|
||||
Reference in New Issue
Block a user