Skip to content

Commit cfa2a3b

Browse files
committed
37 add warning message
1 parent 81ddd51 commit cfa2a3b

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import warnings
22

33
from django.apps import apps
4+
from django.conf import settings
45

56
from netbox_custom_objects.constants import APP_LABEL
67

78
__all__ = (
89
"AppsProxy",
910
"generate_model",
1011
"get_viewname",
12+
"is_branching_plugin_installed",
13+
"is_in_branch",
14+
"get_branching_status",
1115
)
1216

1317

@@ -108,3 +112,18 @@ def generate_model(*args, **kwargs):
108112
apps.clear_cache = apps.clear_cache
109113

110114
return model
115+
116+
117+
def is_in_branch():
118+
"""
119+
Check if currently operating within a branch.
120+
121+
Returns:
122+
bool: True if currently in a branch, False otherwise.
123+
"""
124+
try:
125+
from netbox_branching.contextvars import active_branch
126+
return active_branch.get() is not None
127+
except ImportError:
128+
# Branching plugin not installed
129+
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)