Skip to content

Commit 9d2db85

Browse files
(fix): Correct add inventory/device bug in the forms, views, templates... (#33)
(fix): Correct add inventory/device bug in the forms, views, templates of Inventory module. Add device should now function properly. Swapping between both add and edit in same window should not return old data any longer.
1 parent 0a89d27 commit 9d2db85

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

src/network_ops_dashboard/inventory/forms.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ class Meta:
1111
fields = ('status', 'name', 'name_lookup', 'site', 'platform', 'serial_number', \
1212
'ipaddress_mgmt', 'ipaddress_rest', 'ipaddress_gnmi', 'port_rest', 'port_netc', \
1313
'port_gnmi', 'device_tag', 'creds_ssh', 'creds_rest')
14-
DEVICE_STATUS_CHOICES = (
15-
("ACTIVE", "Active"),
16-
("STAGING", "Staging"),
17-
("MAINT", "Maintenance"),
18-
("RETIRED", "Retired"),
19-
)
20-
status = forms.ChoiceField(label="Status:", choices=DEVICE_STATUS_CHOICES, required=True)
14+
status = forms.ChoiceField(label="Status:", choices=Inventory.Status.choices, required=True)
2115
name = forms.CharField(label="Hostname:", help_text="<br>Hostname of device", required=True)
2216
name_lookup = forms.CharField(label="FQDN:", help_text="<br>ie: devicename.companyname.com", required=False)
2317
site = forms.ModelChoiceField(label="Site:", queryset=Site.objects.all(), required=False)

src/network_ops_dashboard/inventory/views.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,18 @@ def inventory_add_modal(request):
130130
form = InventoryForm(request.POST)
131131
if form.is_valid():
132132
form.save()
133-
return HttpResponse('<script>window.location.reload()</script>')
134-
else:
135-
form = InventoryForm()
136-
context = {"form": form}
137-
context["device"] = None
133+
# Reload the full inventory page
134+
resp = HttpResponse()
135+
resp["HX-Redirect"] = reverse("inventory_home")
136+
return resp
137+
else:
138+
# Re-render form with errors inside modal
139+
context = {"form": form, "device": None}
140+
return render(request, "network_ops_dashboard/inventory/_inventory_form.html", context)
141+
142+
# blank form
143+
form = InventoryForm()
144+
context = {"form": form, "device": None}
138145
return render(request, "network_ops_dashboard/inventory/_inventory_form.html", context)
139146

140147
@login_required(login_url='/accounts/login/')

src/network_ops_dashboard/templates/network_ops_dashboard/inventory/_interfaces_list.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ <h2 class="accordion-header" id="heading{{ forloop.counter }}">
3535
name="action"
3636
value="add_interface"
3737
class="btn btn-sm btn-primary"
38-
hx-post="{% url 'inventory_edit_modal' device.pk %}"
38+
{% if device and device.pk %}
39+
hx-post="{% url 'inventory_edit_modal' device.pk %}"
40+
{% else %}
41+
hx-post="{% url 'inventory_add_modal' %}"
42+
{% endif %}
3943
hx-target="#interfaces-list"
4044
hx-swap="outerHTML">
4145
Add

src/network_ops_dashboard/templates/network_ops_dashboard/inventory/_inventory_form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h5 class="modal-title">
66
</div>
77

88
<form method="post"
9-
hx-post="{% if device %}{% url 'inventory_edit_modal' device.pk %}{% else %}{% url 'inventory_add_modal' %}{% endif %}"
9+
hx-post="{% if device and device.pk %}{% url 'inventory_edit_modal' device.pk %}{% else %}{% url 'inventory_add_modal' %}{% endif %}"
1010
hx-target="#inventoryModal .modal-content"
1111
hx-swap="outerHTML">
1212
{% csrf_token %}

0 commit comments

Comments
 (0)