From a4dda308340bf77ff4d13a8f2e463e286cdac0bb Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 30 Sep 2025 09:10:58 -0700 Subject: [PATCH 1/4] 37 fix merge serializer error --- netbox_custom_objects/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 41ec792..2c2111e 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -544,19 +544,20 @@ def get_model_with_serializer(self): return model def create_model(self): + from netbox_custom_objects.api.serializers import get_serializer_class # Get the model and ensure it's registered model = self.get_model() # Ensure the ContentType exists and is immediately available features = get_model_features(model) - self.object_type.features = features + ['branching'] - self.object_type.public = True self.object_type.features = features + self.object_type.public = True self.object_type.save() with connection.schema_editor() as schema_editor: schema_editor.create_model(model) + get_serializer_class(model) self.register_custom_object_search_index(model) def save(self, *args, **kwargs): From 81ddd516f2b6c8366c4b0862aedc4c87a9949356 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 1 Oct 2025 11:21:18 -0700 Subject: [PATCH 2/4] 37 remove branching from CO --- netbox_custom_objects/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 2c2111e..858a68a 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -550,6 +550,8 @@ def create_model(self): # Ensure the ContentType exists and is immediately available features = get_model_features(model) + if 'branching' in features: + features.remove('branching') self.object_type.features = features self.object_type.public = True self.object_type.save() From cfa2a3bd9aca3d389b6db4f4aa14bcbb95742a5e Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 1 Oct 2025 12:01:37 -0700 Subject: [PATCH 3/4] 37 add warning message --- .../customobject_edit.html | 4 ++++ .../inc/branch_warning.html | 12 ++++++++++++ netbox_custom_objects/utilities.py | 19 +++++++++++++++++++ netbox_custom_objects/views.py | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 netbox_custom_objects/templates/netbox_custom_objects/inc/branch_warning.html diff --git a/netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html b/netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html index daeb7ef..db2d138 100644 --- a/netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html +++ b/netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html @@ -13,6 +13,10 @@ {% endblock title %} {% block form %} + {% if branch_warning %} + {% include 'netbox_custom_objects/inc/branch_warning.html' %} + {% endif %} + {# Render hidden fields #} {% for field in form.hidden_fields %} {{ field }} diff --git a/netbox_custom_objects/templates/netbox_custom_objects/inc/branch_warning.html b/netbox_custom_objects/templates/netbox_custom_objects/inc/branch_warning.html new file mode 100644 index 0000000..98b9e68 --- /dev/null +++ b/netbox_custom_objects/templates/netbox_custom_objects/inc/branch_warning.html @@ -0,0 +1,12 @@ +{% load i18n %} + + diff --git a/netbox_custom_objects/utilities.py b/netbox_custom_objects/utilities.py index 45fdac3..6cfcdfa 100644 --- a/netbox_custom_objects/utilities.py +++ b/netbox_custom_objects/utilities.py @@ -1,6 +1,7 @@ import warnings from django.apps import apps +from django.conf import settings from netbox_custom_objects.constants import APP_LABEL @@ -8,6 +9,9 @@ "AppsProxy", "generate_model", "get_viewname", + "is_branching_plugin_installed", + "is_in_branch", + "get_branching_status", ) @@ -108,3 +112,18 @@ def generate_model(*args, **kwargs): apps.clear_cache = apps.clear_cache return model + + +def is_in_branch(): + """ + Check if currently operating within a branch. + + Returns: + bool: True if currently in a branch, False otherwise. + """ + try: + from netbox_branching.contextvars import active_branch + return active_branch.get() is not None + except ImportError: + # Branching plugin not installed + return False diff --git a/netbox_custom_objects/views.py b/netbox_custom_objects/views.py index c8dfad3..d3ccc50 100644 --- a/netbox_custom_objects/views.py +++ b/netbox_custom_objects/views.py @@ -29,6 +29,7 @@ from .models import CustomObject, CustomObjectType, CustomObjectTypeField from extras.choices import CustomFieldTypeChoices from netbox_custom_objects.constants import APP_LABEL +from netbox_custom_objects.utilities import is_in_branch logger = logging.getLogger("netbox_custom_objects.views") @@ -572,6 +573,23 @@ def custom_save(self, commit=True): return form_class + def get_extra_context(self, request, obj): + + # Check if we're in a branch and if there are external object pointers + has_external_object_pointers = False + if is_in_branch(): + # Check all fields in the custom object type + for field in self.object.custom_object_type.fields.all(): + if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]: + # Check if the related object type is not from the current app + if field.related_object_type.app_label != APP_LABEL: + has_external_object_pointers = True + break + + return { + 'branch_warning': has_external_object_pointers, + } + @register_model_view(CustomObject, "delete") class CustomObjectDeleteView(generic.ObjectDeleteView): From 91f6ba17fa2eb8525227087da052a85a4dc0c18a Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 1 Oct 2025 12:03:04 -0700 Subject: [PATCH 4/4] 37 add warning message --- netbox_custom_objects/utilities.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/netbox_custom_objects/utilities.py b/netbox_custom_objects/utilities.py index 6cfcdfa..d8d901f 100644 --- a/netbox_custom_objects/utilities.py +++ b/netbox_custom_objects/utilities.py @@ -1,7 +1,6 @@ import warnings from django.apps import apps -from django.conf import settings from netbox_custom_objects.constants import APP_LABEL @@ -9,9 +8,7 @@ "AppsProxy", "generate_model", "get_viewname", - "is_branching_plugin_installed", "is_in_branch", - "get_branching_status", ) @@ -117,7 +114,7 @@ def generate_model(*args, **kwargs): def is_in_branch(): """ Check if currently operating within a branch. - + Returns: bool: True if currently in a branch, False otherwise. """