From 4b974407b3ffe0c55e2d7a359346d2963b0d0fd1 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Sun, 7 Aug 2022 14:38:20 +0200 Subject: bar code scanner inside direct approve page --- client/templates/client/approve_doc_pdf.html | 2 +- server/templates/server/approve_doc_direct.html | 67 ++++++++++++++++++++++++- server/views.py | 2 + templates/registration/base_admin.html | 2 + templates/registration/base_simple.html | 2 + version.txt | 2 +- 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/client/templates/client/approve_doc_pdf.html b/client/templates/client/approve_doc_pdf.html index 0cf4944..d2ba29b 100644 --- a/client/templates/client/approve_doc_pdf.html +++ b/client/templates/client/approve_doc_pdf.html @@ -63,7 +63,7 @@ +{% endblock %} + {% block script %} +document.addEventListener('DOMContentLoaded', function() { + {% if error and error != -1 %} + M.toast({html: '{{ error }}', classes: 'orange'}) + {% endif %} +}); + function confirm() { var button = document.getElementById('send_button') var button_small = document.getElementById('send_button_small') @@ -90,4 +104,53 @@ function send() { action.setAttribute('value', {{doc.code}}); form.submit() } + +{% if error %} +function loadCode(code) { + if (code.substring(0, 4) == 'http') { + window.location.replace(code); + return; + } + + var currentUrl = window.location.href; + var currentUrl = currentUrl.split('?')[0]; + if (currentUrl[currentUrl.length - 1] == '/') { + currentUrl = currentUrl.substring(0, currentUrl.length - 1); + } + var newUrl = currentUrl + "?code=" + code; + window.location.replace(newUrl); + return; +} + +let qrboxFunction = function(viewfinderWidth, viewfinderHeight) { + let minEdgePercentage = 0.7; // 70% + let minEdgeSize = Math.min(viewfinderWidth, viewfinderHeight); + let qrboxSize = Math.floor(minEdgeSize * minEdgePercentage); + let size = viewfinderWidth * minEdgePercentage; + return { + width: size, + height: size*0.25, + }; +} + +const html5QrCode = new Html5Qrcode("reader"); +html5QrCode.start( + { facingMode: "environment" }, + { + fps: 10, + qrbox: qrboxFunction, + }, + (decodedText, decodedResult) => { + loadCode(decodedText); + html5QrCode.stop(); + }, + (errorMessage) => {} + ) +.catch((err) => { + // start failed + console.log(`Error starting. Reason: ${err}`) + M.toast({html: 'Collegamento alla telecamera non riuscito. Riprovare.', classes: 'orange'}) +}); +{% endif %} + {% endblock %} \ No newline at end of file diff --git a/server/views.py b/server/views.py index 2b45caa..56a05ec 100644 --- a/server/views.py +++ b/server/views.py @@ -337,6 +337,8 @@ def approve_direct(request): doc_code = int(doc_code) else: doc_code = -1 + else: + return render(request, 'server/approve_doc_direct.html', {"error": -1}) document = Document.objects.filter(code=doc_code) diff --git a/templates/registration/base_admin.html b/templates/registration/base_admin.html index 0fdaffb..e4ecdf3 100644 --- a/templates/registration/base_admin.html +++ b/templates/registration/base_admin.html @@ -142,6 +142,8 @@ + {% block script_import %} + {% endblock %} + {% block script_import %} + {% endblock %}