Skip to content

Commit

Permalink
Fix seeddb netbox-edit form so that empty function field will remove …
Browse files Browse the repository at this point in the history
…function info from netbox
  • Loading branch information
jorund1 committed Aug 23, 2024
1 parent ea42b94 commit a14ca71
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.d/2269.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Emptying the function field then clicking *Save* when editing a netbox
actually removes the function for that netbox (as opposed to rejecting
the change).
7 changes: 7 additions & 0 deletions python/nav/web/seeddb/page/netbox/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ def netbox_do_save(form):
else:
func.value = function
func.save()
elif function == '':
try:
func = NetboxInfo.objects.get(netbox=netbox, variable='function')
except NetboxInfo.DoesNotExist:
pass
else:
func.delete()

# Save the groups
netboxgroups = form.cleaned_data['groups']
Expand Down
34 changes: 33 additions & 1 deletion tests/integration/seeddb_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from mock import MagicMock

from django.utils.encoding import smart_str
from nav.models.manage import Netbox, Room
from nav.models.manage import Netbox, Room, NetboxInfo
from nav.web.seeddb.page.netbox.edit import netbox_edit, log_netbox_change
from nav.web.seeddb.utils.delete import dependencies

Expand Down Expand Up @@ -110,3 +110,35 @@ def test_log_netbox_change_should_not_crash(admin_account, netbox):
new.category_id = "OTHER"

assert log_netbox_change(admin_account, old, new) is None


def test_empty_function_field_in_netbox_edit_form_should_delete_respective_netboxinfo_instance(netbox, db, client):
"""
Empty function fields in the webform should cause the function's
corresponding NetboxInfo to be deleted; This is the correct thing
to do because NAV prefills user forms with previously assigned
values. Hence, if NAV receives a form with an empty function
string, this means the user has explicitly cleared the function
string.
"""
url = reverse('seeddb-netbox-edit', args=(netbox.id,))
def post(func):
return client.post(
url,
follow=True,
data={
"ip": netbox.ip,
"room": netbox.room_id,
"category": netbox.category_id,
"organization": netbox.organization_id,
"function": func,
},
)

assert len(NetboxInfo.objects.filter(netbox=netbox, variable='function')) == 0
post("")
assert len(NetboxInfo.objects.filter(netbox=netbox, variable='function')) == 0
post("foo")
assert NetboxInfo.objects.filter(netbox=netbox, variable='function').get().value == 'foo'
post("")
assert len(NetboxInfo.objects.filter(netbox=netbox, variable='function')) == 0

0 comments on commit a14ca71

Please sign in to comment.