diff options
-rw-r--r-- | accounts/views.py | 8 | ||||
-rw-r--r-- | client/migrations/0026_document_signed_doc.py | 18 | ||||
-rw-r--r-- | client/models.py | 2 | ||||
-rw-r--r-- | client/templates/client/index.html | 4 | ||||
-rw-r--r-- | server/templates/server/approve_doc.html | 2 | ||||
-rw-r--r-- | server/templates/server/doc_list.html | 18 | ||||
-rw-r--r-- | server/templates/server/download_doc.html | 13 | ||||
-rw-r--r-- | server/templates/server/index.html | 8 | ||||
-rw-r--r-- | server/templates/server/upload_doc.html | 86 | ||||
-rw-r--r-- | server/templates/server/user_list.html | 4 | ||||
-rw-r--r-- | server/urls.py | 2 | ||||
-rw-r--r-- | server/views.py | 129 |
12 files changed, 277 insertions, 17 deletions
diff --git a/accounts/views.py b/accounts/views.py index 31786b1..a31d12b 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -123,11 +123,10 @@ def personal(request): if "vac_certificate" in request.FILES: myfile = request.FILES['vac_certificate'] - medic.save() try: im = Image.open(myfile) im_io = BytesIO() - im.save(im_io, 'WEBP', quality=70) + im.save(im_io, 'WEBP', quality=50) medic.vac_certificate.save(request.user.username+"_"+myfile.name, im_io) medic.save() except UnidentifiedImageError: @@ -139,7 +138,7 @@ def personal(request): try: im = Image.open(myfile) im_io = BytesIO() - im.save(im_io, 'WEBP', quality=70) + im.save(im_io, 'WEBP', quality=50) medic.health_care_certificate.save(request.user.username+"_"+myfile.name, im_io) medic.save() except UnidentifiedImageError: @@ -155,6 +154,9 @@ def personal(request): medic.health_care_certificate = None medic.save() + if not error: + return HttpResponseRedirect("") + if len(request.user.groups.values_list('name', flat=True)) == 0: branca_default = "selected" else: diff --git a/client/migrations/0026_document_signed_doc.py b/client/migrations/0026_document_signed_doc.py new file mode 100644 index 0000000..f3b6747 --- /dev/null +++ b/client/migrations/0026_document_signed_doc.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-07-29 19:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0025_documenttype_custom_group'), + ] + + operations = [ + migrations.AddField( + model_name='document', + name='signed_doc', + field=models.FileField(default=None, null=True, upload_to='documents/'), + ), + ] diff --git a/client/models.py b/client/models.py index 4d8ae83..7c47cee 100644 --- a/client/models.py +++ b/client/models.py @@ -71,6 +71,8 @@ class Document(models.Model): medical_data = models.ForeignKey( MedicalData, default=None, on_delete=models.PROTECT, null=True) + signed_doc = models.FileField(default=None, upload_to='documents/', null=True) + class Meta: permissions = [ ("approved", "The user is approved") diff --git a/client/templates/client/index.html b/client/templates/client/index.html index 45a7e90..fd8316b 100644 --- a/client/templates/client/index.html +++ b/client/templates/client/index.html @@ -310,7 +310,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ doc.5 }}"> + {% if doc.5 %} <img src="data:;base64,{{ doc.5 }}"> {% endif %} </div> </div> </div> @@ -319,7 +319,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ doc.6 }}"> + {% if doc.6 %}<img src="data:;base64,{{ doc.6 }}">{% endif %} </div> </div> </div> diff --git a/server/templates/server/approve_doc.html b/server/templates/server/approve_doc.html index 3657f8f..c4bdca4 100644 --- a/server/templates/server/approve_doc.html +++ b/server/templates/server/approve_doc.html @@ -1,6 +1,6 @@ {% extends 'registration/base.html' %} -{% block title %}Admin - Approva Utente{% endblock %} +{% block title %}Admin - Approva Documento{% endblock %} {% block nav %} <a style="margin-left: 10px;" href="{% url 'index' %}" class="breadcrumb">Home</a> diff --git a/server/templates/server/doc_list.html b/server/templates/server/doc_list.html index 3953905..12b64cd 100644 --- a/server/templates/server/doc_list.html +++ b/server/templates/server/doc_list.html @@ -86,6 +86,16 @@ <ul class="collapsible"><li> <div class="collapsible-header">Avanzati</div> <div class="collapsible-body"><span> + <div class="row"> + <div class="input-field col s12"> + <label> + <input name="filter_signdoc" type="checkbox" class="filled-in" {{signdoc_check}}/> + <span style="color:black"><i class="material-icons left">check_circle</i>Visualizza solo approvati con firma allegata</span> + </label> + </div> + </div> + <br> + <br> <div class = "row"> <div class="input-field col l6 s12"> <label for="newer">Più recenti di</label> @@ -130,7 +140,11 @@ {% if doc.0.status == "wait" %} <i class="material-icons">timelapse</i> {% elif doc.0.status == "ok" %} + {% if doc.0.signed_doc %} + <i class="material-icons">check_circle</i> + {% else %} <i class="material-icons">check</i> + {% endif %} {% elif doc.0.status == "archive" %} <i class="material-icons">archive</i> {% elif doc.0.status == "autosign" %} @@ -386,7 +400,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ doc.5 }}"> + {% if doc.5 %}<img src="data:;base64,{{ doc.5 }}">{% endif %} </div> </div> </div> @@ -395,7 +409,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ doc.6 }}"> + {% if doc.6 %}<img src="data:;base64,{{ doc.6 }}">{% endif %} </div> </div> </div> diff --git a/server/templates/server/download_doc.html b/server/templates/server/download_doc.html index 41ce00b..4f4f12d 100644 --- a/server/templates/server/download_doc.html +++ b/server/templates/server/download_doc.html @@ -262,7 +262,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ vac }}"> + {% if vac %}<img src="data:;base64,{{ vac }}">{% endif %} </div> </div> </div> @@ -271,7 +271,16 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ health }}"> + {% if health %}<img src="data:;base64,{{ health }}">{% endif %} + </div> + </div> + </div> + </div> + <div class="row"> + <div class="col s12"> + <div class="card"> + <div class="card-image"> + {% if sign_doc_file %}<img src="data:;base64,{{ sign_doc_file }}">{% endif %} </div> </div> </div> diff --git a/server/templates/server/index.html b/server/templates/server/index.html index 57cfc83..4c82618 100644 --- a/server/templates/server/index.html +++ b/server/templates/server/index.html @@ -9,7 +9,7 @@ {% block content %} <div class="row"> - <div class="col l6 s12"> + <div class="col l4 s12"> <div class="card large"> <div class="card-content"> <p> @@ -35,7 +35,7 @@ </div> </div> </div> - <div class="col l6 s12"> + <div class="col l8 s12"> <div class="card large"> <div class="card-content"> <ul class="collection"> @@ -60,6 +60,7 @@ <a class="waves-effect waves-light btn red lighten-1" href="{% url 'doctype' %}">Lista tipo doc</a> <a class="waves-effect waves-light btn red lighten-1" href="{% url 'doclist' %}">Lista documenti</a> <a class="waves-effect waves-light btn red lighten-1" href="{% url 'docapprove' %}">Approva documento</a> + <a class="waves-effect waves-light btn red lighten-1" href="{% url 'docupload' %}">Carica firma</a> </div> <div class="hide-on-med-and-up"> @@ -70,6 +71,9 @@ <br> <br> <a class="col s12 waves-effect waves-light btn red lighten-1" href="{% url 'docapprove' %}">Approva documento</a> + <br> + <br> + <a class="col s12 waves-effect waves-light btn red lighten-1" href="{% url 'docupload' %}">Carica firma</a> </div> </div> </div> diff --git a/server/templates/server/upload_doc.html b/server/templates/server/upload_doc.html new file mode 100644 index 0000000..334a819 --- /dev/null +++ b/server/templates/server/upload_doc.html @@ -0,0 +1,86 @@ +{% extends 'registration/base.html' %} + +{% block title %}Admin - Carica documento{% endblock %} + +{% block nav %} + <a style="margin-left: 10px;" href="{% url 'index' %}" class="breadcrumb">Home</a> + <a href="{% url 'server'%}" class="breadcrumb hide-on-med-and-down">Admin</a> + <a href="{% url 'docupload'%}" class="breadcrumb hide-on-med-and-down">Carica Documento</a> +{% endblock %} + +{% block content %} +<form target="_blank" action="{% url 'docpreview' %}" method="post" id="preview_form"> + {% csrf_token %} + <input type="hidden" name="preview" id="code_submit"> +</form> + +<div class="row"> + <div class="col l4 offset-l4 m8 offset-m2 s12"> + <div class="card"> + <form id="form" action="{% url 'docupload'%}" method="post" enctype="multipart/form-data"> + {% csrf_token %} + <div class="card-content"> + <div class="row"> + <div class="input-field col s12"> + <input name="code" id="code" type="text"> + <label for="code">Codice documento</label> + </div> + <a class="waves-effect waves-light btn red lighten-1" onclick="send()">Anteprima documento</a> + </div> + <br> + <div class="row"> + <div class="file-field input-field col s12"> + <div class="btn red lighten-1"> + <span><i class="material-icons left">file_upload</i>File</span> + <input type="file" name="doc_sign" id="doc_sign" onchange="loadFile(event)"> + </div> + <div class="file-path-wrapper"> + <input id="doc_sign_name" class="file-path" type="text" placeholder="Documento firmato"> + </div> + </div> + </div> + </div> + <div class="card-action"> + <a class="waves-effect waves-light btn red lighten-1" href="#" onclick="document.getElementById('form').submit()">Invia</a> + </div> + </form> + </div> + </div> +</div> + +<div class="row"> + <div class="col l4 offset-l4 m8 offset-m2 s12"> + <div class="card"> + <div class="card-image"> + <img id="preview"/> + </div> + </div> + </div> +</div> + +{% endblock %} + +{% block script %} +function send() { + var form = document.getElementById('preview_form') + var action = document.getElementById('code_submit') + var text = document.getElementById('code').value + action.setAttribute('value', text); + form.submit() +} + +var loadFile = function(event) { + var output = document.getElementById('preview'); + output.src = URL.createObjectURL(event.target.files[0]); + output.onload = function() { + URL.revokeObjectURL(output.src) // free memory + } +}; +$(document).ready(function(){ + {% if error %} + M.toast({html: '{{ error_text }}', classes: 'orange'}) + {% elif success %} + M.toast({html: '{{ success_text }}', classes: 'green'}) + {% endif %} +}); +{% endblock %}
\ No newline at end of file diff --git a/server/templates/server/user_list.html b/server/templates/server/user_list.html index 26e4b11..653e33d 100644 --- a/server/templates/server/user_list.html +++ b/server/templates/server/user_list.html @@ -274,7 +274,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ user.4 }}"> + {% if user.4 %}<img src="data:;base64,{{ user.4 }}">{% endif %} </div> </div> </div> @@ -283,7 +283,7 @@ <div class="col s12"> <div class="card"> <div class="card-image"> - <img src="data:;base64,{{ user.5 }}"> + {% if user.5 %}<img src="data:;base64,{{ user.5 }}">{% endif %} </div> </div> </div> diff --git a/server/urls.py b/server/urls.py index 773d910..0be590c 100644 --- a/server/urls.py +++ b/server/urls.py @@ -10,4 +10,6 @@ urlpatterns = [ path('doccreate', views.doccreate, name='doccreate'), path('doclist', views.doclist, name='doclist'), path('docapprove', views.docapprove, name='docapprove'), + path('docupload', views.upload_doc, name='docupload'), + path('docpreview', views.docpreview, name='docpreview'), ] diff --git a/server/views.py b/server/views.py index c634e6b..b230d51 100644 --- a/server/views.py +++ b/server/views.py @@ -16,6 +16,7 @@ import pytz import pdfkit from io import BytesIO import os, base64 +from PIL import Image, UnidentifiedImageError @staff_member_required @@ -97,6 +98,9 @@ def uapprove(request): def docapprove(request): context = {} data = [] + parent_group = request.user.groups.values_list('name', flat=True)[ + 0] + group = Group.objects.get(name=parent_group) if request.method == "POST": data = request.POST["codes"] data.replace("\r", "") @@ -108,6 +112,8 @@ def docapprove(request): data[i] = data[i] + " - Formato errato" elif len(Document.objects.filter(code=data[i])) == 0: data[i] = data[i] + " - Invalido" + elif Document.objects.filter(code=data[i])[0].group != group: + data[i] = data[i] + " - Invalido" else: document = Document.objects.filter(code=data[i])[0] if document.status == 'ok': @@ -136,6 +142,7 @@ def ulist(request): if document.group == group: vac_file = "" health_file = "" + sign_doc_file = "" if document.medical_data: if document.medical_data.vac_certificate.name: with open(document.medical_data.vac_certificate.name, 'rb') as image_file: @@ -144,10 +151,13 @@ def ulist(request): if document.medical_data.health_care_certificate.name: with open(document.medical_data.health_care_certificate.name, 'rb') as image_file: health_file = base64.b64encode(image_file.read()).decode() + if document.signed_doc: + with open(document.signed_doc.name, 'rb') as image_file: + sign_doc_file = base64.b64encode(image_file.read()).decode() template = get_template('server/download_doc.html') doc = [document, KeyVal.objects.filter(container=document), document.personal_data, document.medical_data, parent_group] - context = {'doc': doc, 'vac': vac_file, 'health': health_file} + context = {'doc': doc, 'vac': vac_file, 'health': health_file, 'sign_doc_file': sign_doc_file} html = template.render(context) pdf = pdfkit.from_string(html, False) result = BytesIO(pdf) @@ -390,11 +400,13 @@ def doclist(request): wait = True selfsign = True ok = True + signdoc = False hidden_check = 'checked="checked"' wait_check = 'checked="checked"' selfsign_check = 'checked="checked"' ok_check = 'checked="checked"' + signdoc_check = 'checked="checked"' newer = zurich.localize(dateparser.parse("1970-01-01")) older = zurich.localize(datetime.now()) owner = [] @@ -410,6 +422,7 @@ def doclist(request): if document.group == group: vac_file = "" health_file = "" + sign_doc_file = "" if document.medical_data: if document.medical_data.vac_certificate.name: with open(document.medical_data.vac_certificate.name, 'rb') as image_file: @@ -418,10 +431,14 @@ def doclist(request): if document.medical_data.health_care_certificate.name: with open(document.medical_data.health_care_certificate.name, 'rb') as image_file: health_file = base64.b64encode(image_file.read()).decode() + + if document.signed_doc: + with open(document.signed_doc.name, 'rb') as image_file: + sign_doc_file = base64.b64encode(image_file.read()).decode() template = get_template('server/download_doc.html') doc = [document, KeyVal.objects.filter(container=document), document.personal_data, document.medical_data, parent_group] - context = {'doc': doc, 'vac': vac_file, 'health': health_file} + context = {'doc': doc, 'vac': vac_file, 'health': health_file, 'sign_doc_file': sign_doc_file} html = template.render(context) pdf = pdfkit.from_string(html, False) result = BytesIO(pdf) @@ -462,6 +479,7 @@ def doclist(request): wait = "filter_wait" in request.POST selfsign = "filter_selfsign" in request.POST ok = "filter_ok" in request.POST + signdoc = "filter_signdoc" in request.POST newer = zurich.localize(dateparser.parse(request.POST["newer"])) older = zurich.localize(dateparser.parse(request.POST["older"]) + timedelta(days=1)) owner = request.POST["owner"].split("^|") @@ -498,6 +516,8 @@ def doclist(request): if not ok: documents = documents.filter(~Q(status="ok")) ok_check = "" + if not signdoc: + signdoc_check = "" documents = documents.filter(compilation_date__range=[newer, older]) @@ -531,6 +551,10 @@ def doclist(request): out = [] for i in documents: + if signdoc: + if i.status == "ok" and not i.signed_doc: + continue + personal = None medical = None vac_file = "" @@ -562,6 +586,7 @@ def doclist(request): "wait_check": wait_check, "selfsign_check": selfsign_check, "ok_check": ok_check, + "signdoc_check": signdoc_check, "newer": newer, "older": older, "chips_owner": chips_owner, @@ -571,4 +596,102 @@ def doclist(request): 'error_text': error_text, 'settings': settings, } - return render(request, 'server/doc_list.html', context)
\ No newline at end of file + return render(request, 'server/doc_list.html', context) + +@staff_member_required +def upload_doc(request): + parent_group = request.user.groups.values_list('name', flat=True)[ + 0] + group = Group.objects.get(name=parent_group) + message = "" + error = False + success = False + error_text = "" + success_text = "" + document = None + if request.method == "POST": + data = request.POST["code"] + if not data.isdigit(): + error_text = "Formato codice errato" + error = True + elif int(data) < 100000 or int(data) > 999999: + error_text = "Formato codice errato" + error = True + elif len(Document.objects.filter(code=data)) == 0: + error_text = "Codice invalido" + error = True + elif Document.objects.filter(code=data)[0].group != group: + error_text = "Codice invalido" + error = True + else: + document = Document.objects.filter(code=data)[0] + if document.status == 'ok': + success_text = "File caricato" + success = True + else: + document.status = 'ok' + document.save() + success_text = "Documento approvato e file caricato" + success = True + + if "doc_sign" in request.FILES and not error: + myfile = request.FILES['doc_sign'] + try: + im = Image.open(myfile) + im_io = BytesIO() + im.save(im_io, 'WEBP', quality=50) + document.signed_doc.save(data+"_"+myfile.name, im_io) + document.save() + except UnidentifiedImageError: + error = True + error_text = "Il file non รจ un immagine valida" + else: + error = True + error_text = "Prego caricare un file" + + context = { + "message": message, + "error": error, + "error_text": error_text, + "success": success, + "success_text": success_text, + + } + return render(request, 'server/upload_doc.html', context) + +def docpreview(request): + context = {} + parent_group = request.user.groups.values_list('name', flat=True)[ + 0] + group = Group.objects.get(name=parent_group) + if request.method == "POST": + print(request.POST) + code = request.POST["preview"] + if not code.isdigit(): + return render(request, 'server/download_doc.html', context) + if len(Document.objects.filter(code=code)) == 0: + return render(request, 'server/download_doc.html', context) + if Document.objects.filter(code=code)[0].group != group: + return render(request, 'server/download_doc.html', context) + + document = Document.objects.filter(code=code)[0] + vac_file = "" + health_file = "" + sign_doc_file = "" + if document.medical_data: + if document.medical_data.vac_certificate.name: + with open(document.medical_data.vac_certificate.name, 'rb') as image_file: + vac_file = base64.b64encode(image_file.read()).decode() + + if document.medical_data.health_care_certificate.name: + with open(document.medical_data.health_care_certificate.name, 'rb') as image_file: + health_file = base64.b64encode(image_file.read()).decode() + if document.signed_doc: + with open(document.signed_doc.name, 'rb') as image_file: + sign_doc_file = base64.b64encode(image_file.read()).decode() + + template = get_template('server/download_doc.html') + doc = [document, KeyVal.objects.filter(container=document), document.personal_data, document.medical_data, parent_group] + context = {'doc': doc, 'vac': vac_file, 'health': health_file, 'sign_doc_file': sign_doc_file} + + return render(request, 'server/download_doc.html', context)
\ No newline at end of file |