Skip to content

Commit

Permalink
Add QR Code link to My Stuff
Browse files Browse the repository at this point in the history
Clicking the link will show a page with a QR Code linking to the previous page
  • Loading branch information
johannaengland committed May 7, 2024
1 parent 68a368d commit f7aedce
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/+qr-code-my-stuff.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add link to My Stuff that leads to a generated QR Code linking to the page it is called from
1 change: 1 addition & 0 deletions python/nav/web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ <h1>
<li><a href="{{ link.uri }}">{{ link.name }}</a></li>
{% endfor %}
<li class="divider"><a href="{% url 'webfront-preferences' %}">My account <i class="fa fa-gear"></i></a></li>
<li class="divider"><a href="{% url 'webfront-qr-code' %}">QR Code <i class="fa fa-qrcode"></i></a></li>
</ul>
</li>
{% endif %}
Expand Down
19 changes: 19 additions & 0 deletions python/nav/web/templates/webfront/qr_code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "base.html" %}

{% load crispy_forms_tags %}

{% block base_header_additional_head %}
<link rel="stylesheet" href="{{ STATIC_URL }}css/nav.css" />
<script>require(['src/navigation_preferences']);</script>
{% endblock %}

{% block base_content %}
{% if qr_code %}
<img src="data:image/png;base64,{{qr_code}}" alt="QR Code linking to previous side">
{% else %}
<div class="alert-box warning with-icon">
No previous side could be found. This functionality does not work when directly opening this page.
</div>
{% endif %}

{% endblock %}
1 change: 1 addition & 0 deletions python/nav/web/webfront/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@
views.set_account_preference,
name='set-account-preference',
),
re_path(r'^qr-code/$', views.qr_code, name='webfront-qr-code'),
]
18 changes: 18 additions & 0 deletions python/nav/web/webfront/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from nav.web import auth
from nav.web.auth import ldap
from nav.web.auth.utils import set_account
from nav.web.utils import generate_qr_codes_as_byte_strings
from nav.web.utils import require_param
from nav.web.webfront.utils import quick_read, tool_list
from nav.web.webfront.forms import (
Expand Down Expand Up @@ -325,6 +326,23 @@ def preferences(request):
return render(request, 'webfront/preferences.html', context)


def qr_code(request):
"""Show qr code linking to previous page"""
qr_code = None
previous_url = request.headers.get("referer")
if previous_url:
qr_codes = generate_qr_codes_as_byte_strings({previous_url: previous_url})
qr_code = qr_codes[0]

context = {
'navpath': [('Home', '/'), ('QR Code', None)],
'title': 'QR Code',
'qr_code': qr_code,
}

return render(request, 'webfront/qr_code.html', context)


@sensitive_post_parameters('old_password', 'new_password1', 'new_password2')
def change_password(request):
"""Handles POST requests to change a users password"""
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/web/webfront_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,31 @@ def test_non_expired_session_id_should_not_be_changed_on_request_unrelated_to_lo
client.get(index_url)
session_id_post_login = client.session.session_key
assert session_id_post_login == session_id_pre_login


def test_show_qr_code_with_referred_in_header_should_show_qr_code(client):
"""
Tests that when showing the qr_code view after being referred to from another side
will show a generated QR code
"""
url = reverse("webfront-qr-code")
header = {'HTTP_REFERER': 'www.example.com'}
response = client.get(url, follow=True, **header)

assert response.status_code == 200
assert "QR Code linking to previous side" in smart_str(response.content)


def test_show_qr_code_with_no_referred_in_header_should_show_alert(client):
"""
Tests that when showing the qr_code view when directly accessing the side, for
example by directly opening the url it will show an alert
"""
url = reverse("webfront-qr-code")
response = client.get(url, follow=True)

assert response.status_code == 200
assert (
"No previous side could be found. This functionality does not work when directly opening this page."
in smart_str(response.content)
)

0 comments on commit f7aedce

Please sign in to comment.