diff --git a/changelog.d/+qr-code-my-stuff.added.md b/changelog.d/+qr-code-my-stuff.added.md new file mode 100644 index 0000000000..12b191305f --- /dev/null +++ b/changelog.d/+qr-code-my-stuff.added.md @@ -0,0 +1 @@ +Add link to My Stuff that leads to a generated QR Code linking to the page it is called from \ No newline at end of file diff --git a/python/nav/web/templates/base.html b/python/nav/web/templates/base.html index bab92e56ec..de4bc07273 100644 --- a/python/nav/web/templates/base.html +++ b/python/nav/web/templates/base.html @@ -100,6 +100,7 @@

  • {{ link.name }}
  • {% endfor %}
  • My account
  • +
  • QR Code
  • {% endif %} diff --git a/python/nav/web/templates/webfront/qr_code.html b/python/nav/web/templates/webfront/qr_code.html new file mode 100644 index 0000000000..a0ad87f527 --- /dev/null +++ b/python/nav/web/templates/webfront/qr_code.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} + +{% load crispy_forms_tags %} + +{% block base_header_additional_head %} + + +{% endblock %} + +{% block base_content %} + {% if qr_code %} + QR Code linking to previous side + {% else %} +
    + No previous side could be found. This functionality does not work when directly opening this page. +
    + {% endif %} + +{% endblock %} diff --git a/python/nav/web/webfront/urls.py b/python/nav/web/webfront/urls.py index fb174e9436..fe4070abaa 100644 --- a/python/nav/web/webfront/urls.py +++ b/python/nav/web/webfront/urls.py @@ -97,4 +97,5 @@ views.set_account_preference, name='set-account-preference', ), + re_path(r'^qr-code/$', views.qr_code, name='webfront-qr-code'), ] diff --git a/python/nav/web/webfront/views.py b/python/nav/web/webfront/views.py index c98f0e7732..d1385f44a1 100644 --- a/python/nav/web/webfront/views.py +++ b/python/nav/web/webfront/views.py @@ -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 ( @@ -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""" diff --git a/tests/integration/web/webfront_test.py b/tests/integration/web/webfront_test.py index 49e7e45b41..a89a1d6031 100644 --- a/tests/integration/web/webfront_test.py +++ b/tests/integration/web/webfront_test.py @@ -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) + )