From fc45a267170eaa36c7b39e207fd8b85ab36944f8 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Wed, 8 May 2024 10:23:46 -0300 Subject: [PATCH] Suggestion to addressed PR comment --- netbox/users/models/features.py | 53 ------------------------------ netbox/users/models/permissions.py | 21 ++++++++++-- 2 files changed, 19 insertions(+), 55 deletions(-) delete mode 100644 netbox/users/models/features.py diff --git a/netbox/users/models/features.py b/netbox/users/models/features.py deleted file mode 100644 index b635a08c7..000000000 --- a/netbox/users/models/features.py +++ /dev/null @@ -1,53 +0,0 @@ -import json - -from django.db import models - -from extras.utils import is_taggable - - -class CloningUserMixin(models.Model): - """ - Provides the clone() method used to prepare a copy of existing objects. - The same code from netbox/users/models/features.py (CloningMixin) is used here. - It was necessary to avoid circular imports. - """ - class Meta: - abstract = True - - def clone(self): - """ - Returns a dictionary of attributes suitable for creating a copy of the current instance. This is used for pre- - populating an object creation form in the UI. By default, this method will replicate any fields listed in the - model's `clone_fields` list (if defined), but it can be overridden to apply custom logic. - - ```python - class MyModel(NetBoxModel): - def clone(self): - attrs = super().clone() - attrs['extra-value'] = 123 - return attrs - ``` - """ - attrs = {} - - 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 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) - if is_taggable(self): - attrs['tags'] = [tag.pk for tag in self.tags.all()] - - # Include any cloneable custom fields - if hasattr(self, 'custom_fields'): - for field in self.custom_fields: - if field.is_cloneable: - attrs[f'cf_{field.name}'] = self.custom_field_data.get(field.name) - - return attrs diff --git a/netbox/users/models/permissions.py b/netbox/users/models/permissions.py index 662d1a817..11fe6c042 100644 --- a/netbox/users/models/permissions.py +++ b/netbox/users/models/permissions.py @@ -1,10 +1,12 @@ +import json + from django.contrib.postgres.fields import ArrayField from django.db import models from django.urls import reverse from django.utils.translation import gettext_lazy as _ from users.constants import OBJECTPERMISSION_OBJECT_TYPES -from users.models.features import CloningUserMixin + from utilities.querysets import RestrictedQuerySet __all__ = ( @@ -12,7 +14,7 @@ __all__ = ( ) -class ObjectPermission(CloningUserMixin, models.Model): +class ObjectPermission(models.Model): """ A mapping of view, add, change, and/or delete permission for users and/or groups to an arbitrary set of objects identified by ORM query parameters. @@ -84,3 +86,18 @@ class ObjectPermission(CloningUserMixin, models.Model): def get_absolute_url(self): return reverse('users:objectpermission', args=[self.pk]) + + def clone(self): + attrs = {} + + 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 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 + + return attrs