File tree Expand file tree Collapse file tree 4 files changed +53
-0
lines changed
templates/netbox_custom_objects Expand file tree Collapse file tree 4 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 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 }}
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11import warnings
22
33from django .apps import apps
4+ from django .conf import settings
45
56from 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
Original file line number Diff line number Diff line change 2929from .models import CustomObject , CustomObjectType , CustomObjectTypeField
3030from extras .choices import CustomFieldTypeChoices
3131from netbox_custom_objects .constants import APP_LABEL
32+ from netbox_custom_objects .utilities import is_in_branch
3233
3334logger = 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" )
577595class CustomObjectDeleteView (generic .ObjectDeleteView ):
You can’t perform that action at this time.
0 commit comments