diff --git a/netbox/dcim/views.py b/netbox/dcim/views.py index ce4bb5750..ddbbb7642 100644 --- a/netbox/dcim/views.py +++ b/netbox/dcim/views.py @@ -23,6 +23,7 @@ from netbox.constants import DEFAULT_ACTION_PERMISSIONS from netbox.views import generic from tenancy.views import ObjectContactsView from utilities.forms import ConfirmationForm +from utilities.htmx import is_htmx from utilities.paginator import EnhancedPaginator, get_paginate_count from utilities.permissions import get_permission_for_model from utilities.query_functions import CollateAsChar @@ -3183,6 +3184,7 @@ class CableView(generic.ObjectView): class CableEditView(generic.ObjectEditView): queryset = Cable.objects.all() template_name = 'dcim/cable_edit.html' + htmx_template_name = 'dcim/cable_edit.html' def dispatch(self, request, *args, **kwargs): @@ -3195,22 +3197,29 @@ class CableEditView(generic.ObjectEditView): return super().dispatch(request, *args, **kwargs) - def get_object(self, **kwargs): + def alter_object(self, obj, request, url_args, url_kwargs): """ Hack into get_object() to set the form class when editing an existing Cable, since ObjectEditView doesn't currently provide a hook for dynamic class resolution. """ - obj = super().get_object(**kwargs) - + a_terminations_type = request.GET.get('a_terminations_type') + b_terminations_type = request.GET.get('b_terminations_type') if obj.pk: # TODO: Optimize this logic termination_a = obj.terminations.filter(cable_end='A').first() - a_type = termination_a.termination._meta.model if termination_a else None + a_type = termination_a.termination._meta.model if termination_a else ( + CABLE_TERMINATION_TYPES.get(a_terminations_type) + ) termination_b = obj.terminations.filter(cable_end='B').first() - b_type = termination_b.termination._meta.model if termination_b else None + b_type = termination_b.termination._meta.model if termination_b else ( + CABLE_TERMINATION_TYPES.get(b_terminations_type) + ) + + self.skip_htmx = True + self.form = forms.get_cable_form(a_type, b_type) - return obj + return super().alter_object(obj, request, url_args, url_kwargs) def get_extra_addanother_params(self, request): diff --git a/netbox/netbox/views/generic/object_views.py b/netbox/netbox/views/generic/object_views.py index 6277e1c5b..e352f5f2a 100644 --- a/netbox/netbox/views/generic/object_views.py +++ b/netbox/netbox/views/generic/object_views.py @@ -167,6 +167,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView): """ template_name = 'generic/object_edit.html' form = None + skip_htmx = False def dispatch(self, request, *args, **kwargs): # Determine required permission based on whether we are editing an existing object @@ -227,7 +228,7 @@ class ObjectEditView(GetReturnURLMixin, BaseObjectView): restrict_form_fields(form, request.user) # If this is an HTMX request, return only the rendered form HTML - if is_htmx(request): + if is_htmx(request) and not self.skip_htmx: return render(request, 'htmx/form.html', { 'form': form, }) diff --git a/netbox/templates/dcim/cable_edit.html b/netbox/templates/dcim/cable_edit.html index 14d763dc3..f7058e47d 100644 --- a/netbox/templates/dcim/cable_edit.html +++ b/netbox/templates/dcim/cable_edit.html @@ -5,9 +5,8 @@ {% load i18n %} {% block form %} - {# A side termination #} -