Skip to content

Commit 17b5433

Browse files
authored
Merge pull request #241 from netboxlabs/37-branching-2
37 branching 2
2 parents 0db00de + 91f6ba1 commit 17b5433

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

netbox_custom_objects/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,19 +544,22 @@ def get_model_with_serializer(self):
544544
return model
545545

546546
def create_model(self):
547+
from netbox_custom_objects.api.serializers import get_serializer_class
547548
# Get the model and ensure it's registered
548549
model = self.get_model()
549550

550551
# Ensure the ContentType exists and is immediately available
551552
features = get_model_features(model)
552-
self.object_type.features = features + ['branching']
553-
self.object_type.public = True
553+
if 'branching' in features:
554+
features.remove('branching')
554555
self.object_type.features = features
556+
self.object_type.public = True
555557
self.object_type.save()
556558

557559
with connection.schema_editor() as schema_editor:
558560
schema_editor.create_model(model)
559561

562+
get_serializer_class(model)
560563
self.register_custom_object_search_index(model)
561564

562565
def save(self, *args, **kwargs):

netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
{% endblock title %}
1414

1515
{% block form %}
16+
{% if branch_warning %}
17+
{% include 'netbox_custom_objects/inc/branch_warning.html' %}
18+
{% endif %}
19+
1620
{# Render hidden fields #}
1721
{% for field in form.hidden_fields %}
1822
{{ field }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% load i18n %}
2+
3+
<div class="alert alert-warning d-flex align-items-center mb-4" role="alert">
4+
<span class="text-warning fs-1">
5+
<i class="mdi mdi-alert"></i>
6+
</span>
7+
<span class="flex-fill">
8+
{% blocktrans trimmed %}
9+
This object has fields that reference objects in other apps and you have a branch active. Care must be taken to not reference an object that only exists in another branch.
10+
{% endblocktrans %}
11+
</span>
12+
</div>

netbox_custom_objects/utilities.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"AppsProxy",
99
"generate_model",
1010
"get_viewname",
11+
"is_in_branch",
1112
)
1213

1314

@@ -108,3 +109,18 @@ def generate_model(*args, **kwargs):
108109
apps.clear_cache = apps.clear_cache
109110

110111
return model
112+
113+
114+
def is_in_branch():
115+
"""
116+
Check if currently operating within a branch.
117+
118+
Returns:
119+
bool: True if currently in a branch, False otherwise.
120+
"""
121+
try:
122+
from netbox_branching.contextvars import active_branch
123+
return active_branch.get() is not None
124+
except ImportError:
125+
# Branching plugin not installed
126+
return False

netbox_custom_objects/views.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .models import CustomObject, CustomObjectType, CustomObjectTypeField
3030
from extras.choices import CustomFieldTypeChoices
3131
from netbox_custom_objects.constants import APP_LABEL
32+
from netbox_custom_objects.utilities import is_in_branch
3233

3334
logger = logging.getLogger("netbox_custom_objects.views")
3435

@@ -572,6 +573,23 @@ def custom_save(self, commit=True):
572573

573574
return form_class
574575

576+
def get_extra_context(self, request, obj):
577+
578+
# Check if we're in a branch and if there are external object pointers
579+
has_external_object_pointers = False
580+
if is_in_branch():
581+
# Check all fields in the custom object type
582+
for field in self.object.custom_object_type.fields.all():
583+
if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]:
584+
# Check if the related object type is not from the current app
585+
if field.related_object_type.app_label != APP_LABEL:
586+
has_external_object_pointers = True
587+
break
588+
589+
return {
590+
'branch_warning': has_external_object_pointers,
591+
}
592+
575593

576594
@register_model_view(CustomObject, "delete")
577595
class CustomObjectDeleteView(generic.ObjectDeleteView):

0 commit comments

Comments
 (0)