mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
11933 saved filters clone of content-types and add m2m field cloning (#12014)
* 11933 saved filters clone of content-types and add m2m field cloning * Fix JSON rendering * Add content_types to CustomLink.clone() --------- Co-authored-by: jeremystretch <jstretch@netboxlabs.com>
This commit is contained in:
committed by
Jeremy Stretch
parent
7280dfacab
commit
6b622fd9bf
@ -1,6 +1,7 @@
|
||||
import json
|
||||
|
||||
from django import forms
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.http import QueryDict
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup
|
||||
@ -128,11 +129,10 @@ class SavedFilterForm(BootstrapMixin, forms.ModelForm):
|
||||
|
||||
def __init__(self, *args, initial=None, **kwargs):
|
||||
|
||||
# Convert any parameters delivered via initial data to a dictionary
|
||||
# Convert any parameters delivered via initial data to JSON data
|
||||
if initial and 'parameters' in initial:
|
||||
if type(initial['parameters']) is str:
|
||||
# TODO: Make a utility function for this
|
||||
initial['parameters'] = dict(QueryDict(initial['parameters']).lists())
|
||||
initial['parameters'] = json.loads(initial['parameters'])
|
||||
|
||||
super().__init__(*args, initial=initial, **kwargs)
|
||||
|
||||
|
@ -245,7 +245,7 @@ class CustomLink(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogged
|
||||
)
|
||||
|
||||
clone_fields = (
|
||||
'enabled', 'weight', 'group_name', 'button_class', 'new_window',
|
||||
'content_types', 'enabled', 'weight', 'group_name', 'button_class', 'new_window',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
@ -410,7 +410,7 @@ class SavedFilter(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge
|
||||
parameters = models.JSONField()
|
||||
|
||||
clone_fields = (
|
||||
'enabled', 'weight',
|
||||
'content_types', 'weight', 'enabled', 'parameters',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
|
@ -1,3 +1,4 @@
|
||||
import json
|
||||
from collections import defaultdict
|
||||
from functools import cached_property
|
||||
|
||||
@ -111,7 +112,11 @@ class CloningMixin(models.Model):
|
||||
for field_name in getattr(self, 'clone_fields', []):
|
||||
field = self._meta.get_field(field_name)
|
||||
field_value = field.value_from_object(self)
|
||||
if field_value not in (None, ''):
|
||||
if field_value and isinstance(field, models.ManyToManyField):
|
||||
attrs[field_name] = [v.pk for v in field_value]
|
||||
elif field_value and isinstance(field, models.JSONField):
|
||||
attrs[field_name] = json.dumps(field_value)
|
||||
elif field_value not in (None, ''):
|
||||
attrs[field_name] = field_value
|
||||
|
||||
# Include tags (if applicable)
|
||||
|
Reference in New Issue
Block a user