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 19, 2024
1 parent ea42b94 commit 65d93c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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
31 changes: 30 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,32 @@ 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):
"""
NetboxInfo.value.empty_value is not explicitly declared, but defaults to "",
so empty function fields should be saved as "" in the respective NetboxInfo
instance.
"""
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 65d93c0

Please sign in to comment.