Skip to content

Commit fb4d98a

Browse files
Fixes #150: Fix recalculation of cable paths when merging a branch with cable changes (#329)
* Extend update_object() to support non-field attribute assignment * Call a model's custom serialize_object() & deserialize_object() methods if defined * Bump minimum NetBox version to 4.4.1
1 parent d48b08a commit fb4d98a

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

netbox_branching/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AppConfig(PluginConfig):
1414
description = 'A git-like branching implementation for NetBox'
1515
version = '0.7.0'
1616
base_url = 'branching'
17-
min_version = '4.4.0'
17+
min_version = '4.4.1'
1818
max_version = '4.4.99'
1919
middleware = [
2020
'netbox_branching.middleware.BranchMiddleware'

netbox_branching/models/changes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ def apply(self, branch, using=DEFAULT_DB_ALIAS, logger=None):
4848

4949
# Creating a new object
5050
if self.action == ObjectChangeActionChoices.ACTION_CREATE:
51-
instance = deserialize_object(model, self.postchange_data, pk=self.changed_object_id)
51+
if hasattr(model, 'deserialize_object'):
52+
instance = model.deserialize_object(self.postchange_data, pk=self.changed_object_id)
53+
else:
54+
instance = deserialize_object(model, self.postchange_data, pk=self.changed_object_id)
5255
logger.debug(f'Creating {model._meta.verbose_name} {instance}')
5356
instance.object.full_clean()
5457
instance.save(using=using)
5558

5659
# Modifying an object
5760
elif self.action == ObjectChangeActionChoices.ACTION_UPDATE:
5861
instance = model.objects.using(using).get(pk=self.changed_object_id)
62+
logger.debug(f'Updating {model._meta.verbose_name} {instance}')
5963
update_object(instance, self.diff()['post'], using=using)
6064

6165
# Deleting an object

netbox_branching/signal_receivers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def record_change_diff(instance, **kwargs):
8181
model = instance.changed_object_type.model_class()
8282
with deactivate_branch():
8383
obj = model.objects.get(pk=instance.changed_object_id)
84-
current_data = serialize_object(obj, exclude=['created', 'last_updated'])
84+
if hasattr(obj, 'serialize_object'):
85+
current_data = obj.serialize_object(exclude=['created', 'last_updated'])
86+
else:
87+
current_data = serialize_object(obj, exclude=['created', 'last_updated'])
8588
diff = ChangeDiff(
8689
branch=branch,
8790
object=instance.changed_object,

netbox_branching/utilities.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from functools import cached_property
77

88
from django.contrib import messages
9-
from django.core.exceptions import ObjectDoesNotExist
9+
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
1010
from django.db.models import ForeignKey, ManyToManyField
1111
from django.http import HttpResponseBadRequest
1212
from django.urls import reverse
@@ -188,13 +188,16 @@ def update_object(instance, data, using):
188188
if attr == 'custom_fields':
189189
attr = 'custom_field_data'
190190

191-
model_field = instance._meta.get_field(attr)
192-
field_cls = model_field.__class__
191+
try:
192+
model_field = instance._meta.get_field(attr)
193+
field_cls = model_field.__class__
194+
except FieldDoesNotExist:
195+
field_cls = None
193196

194-
if issubclass(field_cls, ForeignKey):
197+
if field_cls and issubclass(field_cls, ForeignKey):
195198
# Direct value assignment for ForeignKeys must be done by the field's concrete name
196199
setattr(instance, f'{attr}_id', value)
197-
elif issubclass(field_cls, (ManyToManyField, TaggableManager)):
200+
elif field_cls and issubclass(field_cls, (ManyToManyField, TaggableManager)):
198201
# Use M2M manager for ManyToMany assignments
199202
m2m_manager = getattr(instance, attr)
200203
m2m_assignments[m2m_manager] = value

0 commit comments

Comments
 (0)