Skip to content

Commit bc553af

Browse files
committed
Disable submit if on a branch
1 parent 82f2118 commit bc553af

File tree

5 files changed

+27
-45
lines changed

5 files changed

+27
-45
lines changed

netbox_custom_objects/templates/netbox_custom_objects/custom_object_bulk_edit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ <h2 class="col-9 offset-3">{% trans "Comments" %}</h2>
8585

8686
<div class="btn-float-group-right">
8787
<a href="{{ return_url }}" class="btn btn-outline-secondary btn-float">{% trans "Cancel" %}</a>
88-
<button type="submit" name="_apply" class="btn btn-primary">{% trans "Apply" %}</button>
88+
<button type="submit" name="_apply" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>{% trans "Apply" %}</button>
8989
</div>
9090
</div>
9191
</form>

netbox_custom_objects/templates/netbox_custom_objects/custom_object_bulk_import.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
{% if return_url %}
3737
<a href="{{ return_url }}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
3838
{% endif %}
39-
<button type="submit" name="data_submit" class="btn btn-primary">{% trans "Submit" %}</button>
39+
<button type="submit" name="data_submit" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>{% trans "Submit" %}</button>
4040
</div>
4141
</div>
4242
</form>
@@ -68,7 +68,7 @@
6868
{% if return_url %}
6969
<a href="{{ return_url }}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
7070
{% endif %}
71-
<button type="submit" name="file_submit" class="btn btn-primary">{% trans "Submit" %}</button>
71+
<button type="submit" name="file_submit" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>{% trans "Submit" %}</button>
7272
</div>
7373
</div>
7474
</form>
@@ -101,7 +101,7 @@
101101
{% if return_url %}
102102
<a href="{{ return_url }}" class="btn btn-outline-secondary">{% trans "Cancel" %}</a>
103103
{% endif %}
104-
<button type="submit" name="file_submit" class="btn btn-primary">{% trans "Submit" %}</button>
104+
<button type="submit" name="file_submit" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>{% trans "Submit" %}</button>
105105
</div>
106106
</div>
107107
</form>

netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,22 @@ <h3 class="col-9 offset-3 mb-3 h4">{{ group }}</h3>
4545
{% endif %}
4646
{% endfor %}
4747
</div>
48-
{% endblock form %}
48+
{% endblock form %}
49+
50+
{% block buttons %}
51+
<a href="{{ return_url }}" class="btn btn-outline-secondary btn-float">{% trans "Cancel" %}</a>
52+
{% if object.pk %}
53+
<button type="submit" name="_update" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>
54+
{% trans "Save" %}
55+
</button>
56+
{% else %}
57+
<div class="btn-group" role="group" aria-label="{% trans "Actions" %}">
58+
<button type="submit" name="_create" class="btn btn-primary"{% if branch_warning %} disabled{% endif %}>
59+
{% trans "Create" %}
60+
</button>
61+
<button type="submit" name="_addanother" class="btn btn-outline-primary btn-float"{% if branch_warning %} disabled{% endif %}>
62+
{% trans "Create & Add Another" %}
63+
</button>
64+
</div>
65+
{% endif %}
66+
{% endblock buttons %}

netbox_custom_objects/templates/netbox_custom_objects/inc/branch_warning.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</span>
77
<span class="flex-fill">
88
{% 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.
9+
Please switch to the main branch to perform this operation.
1010
{% endblocktrans %}
1111
</span>
1212
</div>

netbox_custom_objects/views.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,8 @@ def custom_save(self, commit=True):
574574
return form_class
575575

576576
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-
589577
return {
590-
'branch_warning': has_external_object_pointers,
578+
'branch_warning': is_in_branch(),
591579
}
592580

593581

@@ -692,20 +680,8 @@ def get_form(self, queryset):
692680
return form
693681

694682
def get_extra_context(self, request):
695-
696-
# Check if we're in a branch and if there are external object pointers
697-
has_external_object_pointers = False
698-
if is_in_branch():
699-
# Check all fields in the custom object type
700-
for field in self.custom_object_type.fields.all():
701-
if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]:
702-
# Check if the related object type is not from the current app
703-
if field.related_object_type.app_label != APP_LABEL:
704-
has_external_object_pointers = True
705-
break
706-
707683
return {
708-
'branch_warning': has_external_object_pointers,
684+
'branch_warning': is_in_branch(),
709685
}
710686

711687

@@ -795,20 +771,8 @@ def get_model_form(self, queryset):
795771
return form
796772

797773
def get_extra_context(self, request):
798-
799-
# Check if we're in a branch and if there are external object pointers
800-
has_external_object_pointers = False
801-
if is_in_branch():
802-
# Check all fields in the custom object type
803-
for field in self.custom_object_type.fields.all():
804-
if field.type in [CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT]:
805-
# Check if the related object type is not from the current app
806-
if field.related_object_type.app_label != APP_LABEL:
807-
has_external_object_pointers = True
808-
break
809-
810774
return {
811-
'branch_warning': has_external_object_pointers,
775+
'branch_warning': is_in_branch(),
812776
}
813777

814778

0 commit comments

Comments
 (0)