Skip to content

Commit

Permalink
Generate QR codes in SeedDB and show in page
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Apr 29, 2024
1 parent 376e88d commit 34d1bda
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
10 changes: 9 additions & 1 deletion python/nav/web/seeddb/page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
51 changes: 50 additions & 1 deletion python/nav/web/seeddb/page/netbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)


Expand Down Expand Up @@ -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()
Expand Down
43 changes: 42 additions & 1 deletion python/nav/web/seeddb/page/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)


Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions python/nav/web/templates/seeddb/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
</div>
{% endif %}

{% if qr_codes %}
{%for qr_code in qr_codes %}
<img src="data:image/png;base64,{{qr_code}}">
{% endfor %}
{% endif %}

<div id="tablewrapper" class="notvisible" data-forpage="{{ request.path }}" data-page="{{ active_page }}">
<table id="seeddb-content" class="listtable" width="100%">
<caption>
Expand Down

0 comments on commit 34d1bda

Please sign in to comment.