diff --git a/python/nav/web/maintenance/utils.py b/python/nav/web/maintenance/utils.py index 6b789939cd..90d7e13cd4 100644 --- a/python/nav/web/maintenance/utils.py +++ b/python/nav/web/maintenance/utils.py @@ -137,40 +137,62 @@ def get_component_keys(post): def components_for_keys(component_keys): component_data = {} - component_data['service'] = Service.objects.filter( - id__in=component_keys['service'] - ).values( - 'id', - 'handler', - 'netbox__id', - 'netbox__sysname', - 'netbox__ip', - 'netbox__room__id', - 'netbox__room__description', - 'netbox__room__location__id', - 'netbox__room__location__description', - ) - component_data['netbox'] = Netbox.objects.filter( - id__in=component_keys['netbox'] - ).values( - 'id', - 'sysname', - 'ip', - 'room__id', - 'room__description', - 'room__location__id', - 'room__location__description', - ) - component_data['room'] = Room.objects.filter(id__in=component_keys['room']).values( - 'id', 'description', 'location__id', 'location__description' - ) - component_data['location'] = Location.objects.filter( - id__in=component_keys['location'] - ).values('id', 'description') - component_data['netboxgroup'] = NetboxGroup.objects.filter( - id__in=component_keys['netboxgroup'] - ).values('id', 'description') - return component_data + component_data_errors = [] + if component_keys['service']: + component_data['service'] = Service.objects.filter( + id__in=component_keys['service'] + ).values( + 'id', + 'handler', + 'netbox__id', + 'netbox__sysname', + 'netbox__ip', + 'netbox__room__id', + 'netbox__room__description', + 'netbox__room__location__id', + 'netbox__room__location__description', + ) + if not component_data['service']: + component_data_errors.append( + "service: no elements with the given pks found" + ) + if component_keys['netbox']: + component_data['netbox'] = Netbox.objects.filter( + id__in=component_keys['netbox'] + ).values( + 'id', + 'sysname', + 'ip', + 'room__id', + 'room__description', + 'room__location__id', + 'room__location__description', + ) + if not component_data['netbox']: + component_data_errors.append("netbox: no elements with the given pks found") + if component_keys['room']: + component_data['room'] = Room.objects.filter( + id__in=component_keys['room'] + ).values('id', 'description', 'location__id', 'location__description') + if not component_data['room']: + component_data_errors.append("room: no elements with the given pks found") + if component_keys['location']: + component_data['location'] = Location.objects.filter( + id__in=component_keys['location'] + ).values('id', 'description') + if not component_data['location']: + component_data_errors.append( + "location: no elements with the given pks found" + ) + if component_keys['netboxgroup']: + component_data['netboxgroup'] = NetboxGroup.objects.filter( + id__in=component_keys['netboxgroup'] + ).values('id', 'description') + if not component_data['netboxgroup']: + component_data_errors.append( + "netboxgroup: no elements with the given pks found" + ) + return component_data, component_data_errors def structure_component_data(component_data): diff --git a/python/nav/web/maintenance/views.py b/python/nav/web/maintenance/views.py index c0b371f5ab..7d4d653e9f 100644 --- a/python/nav/web/maintenance/views.py +++ b/python/nav/web/maintenance/views.py @@ -205,7 +205,7 @@ def view(request, task_id): value = int(value) component_keys[key].append(value) - component_data = components_for_keys(component_keys) + component_data, _ = components_for_keys(component_keys) components = structure_component_data(component_data) component_trail = task_component_trails(component_keys, components) @@ -279,9 +279,11 @@ def edit(request, task_id=None, start_time=None, **_): component_keys, component_keys_errors = get_component_keys(request.GET) if component_keys: - component_data = components_for_keys(component_keys) + component_data, component_data_errors = components_for_keys(component_keys) components = structure_component_data(component_data) component_trail = task_component_trails(component_keys, components) + if component_data_errors: + new_message(request, ",".join(component_data_errors), Messages.ERROR) if component_keys_errors: new_message(request, ",".join(component_keys_errors), Messages.ERROR) diff --git a/tests/integration/web/maintenance/views_test.py b/tests/integration/web/maintenance/views_test.py index d8f6b8d921..b0af25a285 100644 --- a/tests/integration/web/maintenance/views_test.py +++ b/tests/integration/web/maintenance/views_test.py @@ -16,6 +16,7 @@ from django.urls import reverse from nav.compatibility import smart_str +from nav.models.manage import Netbox from nav.models.msgmaint import MaintenanceTask @@ -100,6 +101,20 @@ def test_with_non_int_netbox_key_in_url_should_fail(self, db, client): assert response.status_code == 200 assert "netbox: argument needs to be a number" in smart_str(response.content) + def test_with_non_existent_netbox_key_in_url_should_fail(self, db, client): + last_netbox_id = getattr(Netbox.objects.last(), "pk", 0) + url = ( + reverse('maintenance-new') + + f'?netbox={last_netbox_id+1}&netbox={last_netbox_id+2}' + ) + + response = client.get(url, follow=True) + + assert response.status_code == 200 + assert "netbox: no elements with the given pks found" in smart_str( + response.content + ) + def test_with_end_time_before_start_time_should_fail(self, db, client, localhost): url = reverse('maintenance-new') data = {