mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-13 02:58:17 -06:00
Created CloningUserMixin;
Added CloningUserMixin to ObjectPermission to enable clone_fields.
This commit is contained in:
parent
b91741dd75
commit
d9aeeb8050
53
netbox/users/models/features.py
Normal file
53
netbox/users/models/features.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
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
|
@ -4,6 +4,7 @@ from django.urls import reverse
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from users.constants import OBJECTPERMISSION_OBJECT_TYPES
|
from users.constants import OBJECTPERMISSION_OBJECT_TYPES
|
||||||
|
from users.models.features import CloningUserMixin
|
||||||
from utilities.querysets import RestrictedQuerySet
|
from utilities.querysets import RestrictedQuerySet
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -11,7 +12,7 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ObjectPermission(models.Model):
|
class ObjectPermission(CloningUserMixin, models.Model):
|
||||||
"""
|
"""
|
||||||
A mapping of view, add, change, and/or delete permission for users and/or groups to an arbitrary set of objects
|
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.
|
identified by ORM query parameters.
|
||||||
@ -47,6 +48,8 @@ class ObjectPermission(models.Model):
|
|||||||
|
|
||||||
objects = RestrictedQuerySet.as_manager()
|
objects = RestrictedQuerySet.as_manager()
|
||||||
|
|
||||||
|
clone_fields = ['name', 'description', 'enabled', 'object_types', 'actions', 'constraints']
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['name']
|
ordering = ['name']
|
||||||
verbose_name = _('permission')
|
verbose_name = _('permission')
|
||||||
|
Loading…
Reference in New Issue
Block a user