Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix seeddb netbox-edit form so that empty function field will remove function info from netbox #2952

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
now actually removes the function for that netbox (as opposed to
silently ignoring the change).
2 changes: 2 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,8 @@ def netbox_do_save(form):
else:
func.value = function
func.save()
elif function == '':
NetboxInfo.objects.filter(netbox=netbox, variable='function').delete()

# Save the groups
netboxgroups = form.cleaned_data['groups']
Expand Down
40 changes: 39 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,41 @@ 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
Loading