diff options
author | Andrea Lepori <alepori@student.ethz.ch> | 2022-08-07 14:38:20 +0200 |
---|---|---|
committer | Andrea Lepori <alepori@student.ethz.ch> | 2022-08-07 14:38:20 +0200 |
commit | 4b974407b3ffe0c55e2d7a359346d2963b0d0fd1 (patch) | |
tree | d49b5ca4f16f765e8c0ca68f0c25c9c443971c87 /server | |
parent | add qr code to pdf approve, direct approval page (diff) | |
download | scout-subs-4b974407b3ffe0c55e2d7a359346d2963b0d0fd1.tar.gz scout-subs-4b974407b3ffe0c55e2d7a359346d2963b0d0fd1.zip |
bar code scanner inside direct approve page
Diffstat (limited to '')
-rw-r--r-- | server/templates/server/approve_doc_direct.html | 67 | ||||
-rw-r--r-- | server/views.py | 2 |
2 files changed, 67 insertions, 2 deletions
diff --git a/server/templates/server/approve_doc_direct.html b/server/templates/server/approve_doc_direct.html index 2a6710b..a435b5f 100644 --- a/server/templates/server/approve_doc_direct.html +++ b/server/templates/server/approve_doc_direct.html @@ -36,6 +36,10 @@ </div> </div> </div> + <br> + <div class="row" style="margin-bottom: 0;"> + <a class="col s12 waves-effect waves-light btn {{color}}" href="{% url 'approve_direct' %}">Scansiona un nuovo documento</a> + </div> </div> {% elif not error%} <form id="form" action="{% url 'approve_direct'%}" method="post" enctype="multipart/form-data"> @@ -51,7 +55,7 @@ <div class="row"> <a id="send_button_small" class="col s12 waves-effect waves-light btn {{color}}" href="#" onclick="confirm()">Approva</a> </div> - <div class="row" style="margin-bottom: 0"> + <div class="row" style="margin-bottom: 0;"> <a class="col s12 waves-effect waves-light btn {{color}}" onclick="send()">Anteprima</a> </div> </div> @@ -63,7 +67,7 @@ </form> {% else %} <div class="card-content"> - {{error}} + <div id="reader"></div> </div> {% endif %} </div> @@ -72,7 +76,17 @@ {% endblock %} +{% block script_import %} +<script type="text/javascript" src="https://unpkg.com/html5-qrcode"></script> +{% 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)
|