Move clone() to CloningMixin

This commit is contained in:
jeremystretch 2022-09-09 16:44:58 -04:00
parent 77868a9b17
commit cd1ad452da
2 changed files with 31 additions and 23 deletions

View File

@ -2,7 +2,6 @@ from django.core.validators import ValidationError
from django.db import models from django.db import models
from mptt.models import MPTTModel, TreeForeignKey from mptt.models import MPTTModel, TreeForeignKey
from extras.utils import is_taggable
from utilities.mptt import TreeManager from utilities.mptt import TreeManager
from utilities.querysets import RestrictedQuerySet from utilities.querysets import RestrictedQuerySet
from netbox.models.features import * from netbox.models.features import *
@ -32,7 +31,7 @@ class NetBoxFeatureSet(
def get_prerequisite_models(cls): def get_prerequisite_models(cls):
""" """
Return a list of model types that are required to create this model or empty list if none. This is used for Return a list of model types that are required to create this model or empty list if none. This is used for
showing prequisite warnings in the UI on the list and detail views. showing prerequisite warnings in the UI on the list and detail views.
""" """
return [] return []
@ -52,7 +51,7 @@ class ChangeLoggedModel(ChangeLoggingMixin, CustomValidationMixin, models.Model)
abstract = True abstract = True
class NetBoxModel(NetBoxFeatureSet, models.Model): class NetBoxModel(CloningMixin, NetBoxFeatureSet, models.Model):
""" """
Primary models represent real objects within the infrastructure being modeled. Primary models represent real objects within the infrastructure being modeled.
""" """
@ -61,25 +60,6 @@ class NetBoxModel(NetBoxFeatureSet, models.Model):
class Meta: class Meta:
abstract = True abstract = True
def clone(self):
"""
Return 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.
"""
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 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()]
return attrs
class NestedGroupModel(NetBoxFeatureSet, MPTTModel): class NestedGroupModel(NetBoxFeatureSet, MPTTModel):
""" """

View File

@ -10,12 +10,13 @@ from django.db import models
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
from extras.choices import CustomFieldVisibilityChoices, ObjectChangeActionChoices from extras.choices import CustomFieldVisibilityChoices, ObjectChangeActionChoices
from extras.utils import register_features from extras.utils import is_taggable, register_features
from netbox.signals import post_clean from netbox.signals import post_clean
from utilities.utils import serialize_object from utilities.utils import serialize_object
__all__ = ( __all__ = (
'ChangeLoggingMixin', 'ChangeLoggingMixin',
'CloningMixin',
'CustomFieldsMixin', 'CustomFieldsMixin',
'CustomLinksMixin', 'CustomLinksMixin',
'CustomValidationMixin', 'CustomValidationMixin',
@ -82,6 +83,33 @@ class ChangeLoggingMixin(models.Model):
return objectchange return objectchange
class CloningMixin(models.Model):
"""
Provides the clone() method used to prepare a copy of existing objects.
"""
class Meta:
abstract = True
def clone(self):
"""
Return 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.
"""
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 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()]
return attrs
class CustomFieldsMixin(models.Model): class CustomFieldsMixin(models.Model):
""" """
Enables support for custom fields. Enables support for custom fields.