diff --git a/python/nav/web/seeddb/page/__init__.py b/python/nav/web/seeddb/page/__init__.py index fe236c7853..58c95a1645 100644 --- a/python/nav/web/seeddb/page/__init__.py +++ b/python/nav/web/seeddb/page/__init__.py @@ -42,11 +42,19 @@ def not_implemented(*_args, **_kwargs): raise NotImplementedError() -def view_switcher(request, list_view=None, move_view=None, delete_view=None): +def view_switcher( + request, + list_view=None, + move_view=None, + delete_view=None, + generate_qr_codes_view=None, +): """Selects appropriate view depending on POST data.""" if request.method == 'POST': if 'move' in request.POST: return move_view(request) elif 'delete' in request.POST: return delete_view(request) + elif 'qr_code' in request.POST: + return generate_qr_codes_view(request) return list_view(request) diff --git a/python/nav/web/seeddb/page/netbox/__init__.py b/python/nav/web/seeddb/page/netbox/__init__.py index 19bb38e561..d4788fc59e 100644 --- a/python/nav/web/seeddb/page/netbox/__init__.py +++ b/python/nav/web/seeddb/page/netbox/__init__.py @@ -28,6 +28,7 @@ from nav.web.seeddb.page import view_switcher from nav.web.seeddb.utils.list import render_list from nav.web.seeddb.utils.delete import render_delete +from nav.web.seeddb.utils.generate_qr_codes import generate_qr_codes from nav.web.seeddb.utils.move import move from nav.web.seeddb.utils.bulk import render_bulkimport from nav.web.seeddb.page.netbox.forms import NetboxFilterForm, NetboxMoveForm @@ -53,7 +54,11 @@ class NetboxInfo(SeeddbInfo): def netbox(request): """Controller for landing page for netboxes""" return view_switcher( - request, list_view=netbox_list, move_view=netbox_move, delete_view=netbox_delete + request, + list_view=netbox_list, + move_view=netbox_move, + delete_view=netbox_delete, + generate_qr_codes_view=netbox_generate_qr_codes, ) @@ -111,6 +116,50 @@ def netbox_pre_deletion_mark(queryset): queryset.update(deleted_at=datetime.datetime.now(), up_to_date=False) +def netbox_generate_qr_codes(request): + """Controller for generating qr codes for netboxes""" + url_dict = dict() + netboxes = Netbox.objects.filter(id__in=request.POST.getlist('object')) + + for netbox in netboxes: + name = str(netbox) + url = request.build_absolute_uri("/ipdevinfo/" + name) + url_dict[name] = url + + qr_codes = generate_qr_codes( + request=request, redirect='seeddb-netbox', url_dict=url_dict + ) + + info = NetboxInfo() + query = ( + Netbox.objects.select_related("room", "category", "type", "organization") + .prefetch_related("profiles") + .annotate(profile=ArrayAgg("profiles__name")) + ) + filter_form = NetboxFilterForm(request.GET) + value_list = ( + 'sysname', + 'room', + 'ip', + 'category', + 'organization', + 'profile', + 'type__name', + ) + extra_context = info.template_context + extra_context["qr_codes"] = qr_codes + return render_list( + request, + query, + value_list, + 'seeddb-netbox-edit', + edit_url_attr='pk', + filter_form=filter_form, + template='seeddb/list_netbox.html', + extra_context=extra_context, + ) + + def netbox_move(request): """Controller for handling a move request""" info = NetboxInfo() diff --git a/python/nav/web/seeddb/page/room.py b/python/nav/web/seeddb/page/room.py index 03abe94cac..0d82d82db8 100644 --- a/python/nav/web/seeddb/page/room.py +++ b/python/nav/web/seeddb/page/room.py @@ -28,6 +28,7 @@ from nav.web.seeddb.page import view_switcher from nav.web.seeddb.utils.list import render_list from nav.web.seeddb.utils.edit import render_edit +from nav.web.seeddb.utils.generate_qr_codes import generate_qr_codes from nav.web.seeddb.utils.delete import render_delete from nav.web.seeddb.utils.move import move from nav.web.seeddb.utils.bulk import render_bulkimport @@ -55,7 +56,11 @@ class RoomInfo(SeeddbInfo): def room(request): """Controller for listing, moving and deleting rooms""" return view_switcher( - request, list_view=room_list, move_view=room_move, delete_view=room_delete + request, + list_view=room_list, + move_view=room_move, + delete_view=room_delete, + generate_qr_codes_view=room_generate_qr_codes, ) @@ -83,6 +88,42 @@ def room_move(request): ) +def room_generate_qr_codes(request): + """Controller for generating qr codes for rooms""" + url_dict = dict() + ids = request.POST.getlist('object') + + for id in ids: + url = request.build_absolute_uri(reverse('room-info', kwargs={'roomid': id})) + url_dict[id] = url + + qr_codes = generate_qr_codes( + request=request, redirect='seeddb-room', url_dict=url_dict + ) + + info = RoomInfo() + value_list = ( + 'id', + 'location', + 'description', + 'position', + 'data', + ) + query = Room.objects.select_related("location").all() + filter_form = RoomFilterForm(request.GET) + extra_context = info.template_context + extra_context["qr_codes"] = qr_codes + return render_list( + request, + query, + value_list, + 'seeddb-room-edit', + edit_url_attr='pk', + filter_form=filter_form, + extra_context=extra_context, + ) + + def room_delete(request, object_id=None): """Controller for deleting rooms. Used in room()""" info = RoomInfo() diff --git a/python/nav/web/templates/seeddb/list.html b/python/nav/web/templates/seeddb/list.html index ff97b96d55..1ee86e878e 100644 --- a/python/nav/web/templates/seeddb/list.html +++ b/python/nav/web/templates/seeddb/list.html @@ -30,6 +30,12 @@ {% endif %} + {% if qr_codes %} + {%for qr_code in qr_codes %} + + {% endfor %} + {% endif %} +