mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-12 10:38:16 -06:00
Suggestion to addressed PR comment
This commit is contained in:
parent
d9aeeb8050
commit
fc45a26717
@ -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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user