diff --git a/changelog.d/2269.fixed.md b/changelog.d/2269.fixed.md new file mode 100644 index 0000000000..650739a276 --- /dev/null +++ b/changelog.d/2269.fixed.md @@ -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). diff --git a/python/nav/web/seeddb/page/netbox/edit.py b/python/nav/web/seeddb/page/netbox/edit.py index 209ac090cf..1fab8d6e13 100644 --- a/python/nav/web/seeddb/page/netbox/edit.py +++ b/python/nav/web/seeddb/page/netbox/edit.py @@ -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'] diff --git a/tests/integration/seeddb_test.py b/tests/integration/seeddb_test.py index bfd6b6bb97..4ca9a2d9ae 100644 --- a/tests/integration/seeddb_test.py +++ b/tests/integration/seeddb_test.py @@ -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 @@ -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