From 0b5be72725ffd6cd10490fbd202388e22c7f8522 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Tue, 12 Nov 2024 15:27:12 +0100 Subject: commit edits for new feature --- server/templates/server/data_download.html | 102 +++++++++++++++++++++++++++++ server/templates/server/doc_type.html | 2 +- server/urls.py | 2 + server/views.py | 84 +++++++++++++++++++++++- 4 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 server/templates/server/data_download.html (limited to 'server') diff --git a/server/templates/server/data_download.html b/server/templates/server/data_download.html new file mode 100644 index 0000000..59b180d --- /dev/null +++ b/server/templates/server/data_download.html @@ -0,0 +1,102 @@ +{% extends 'registration/base_admin.html' %} + +{% block title %}Admin - Richiesta dati{% endblock %} + +{% block breadcrumb %} + Admin + Richiesta dati +{% endblock %} + +{% block content %} + +
+
+
+
+
+
+ +
+
+
+
+

+ +

+

+ +

+

+ +

+
+
+
+ +
+ +
+
+
+
+{% endblock %} + +{% block script %} +$(document).ready(function(){ + {% if error %} + M.toast({html: '{{ error }}', classes: 'orange'}) + {% endif %} +}); + +document.addEventListener('DOMContentLoaded', function() { + var options = { + placeholder: 'Documento', + autocompleteOptions: { + data: { + {% for doc in docs %} + '{{ doc.name }}': null, + {% endfor %} + }, + }, + limit: 1, + }; + + var elems = document.querySelectorAll('.chips'); + var instances = M.Chips.init(elems, options); +}); + +function send() { + var chips = M.Chips.getInstance(document.getElementById('doc_search')); + var personal = document.getElementsByName('personal')[0].checked; + var medic = document.getElementsByName('medic')[0].checked; + var attachments = document.getElementsByName('attachments')[0].checked; + + var loadbox = document.getElementById('loading'); + var progress = document.getElementById('progress_bar'); + var progress_text = document.getElementById('progress_text'); + var under_text = document.getElementById('under_text'); + + loadbox.hidden = false; + +} +{% endblock%} \ No newline at end of file diff --git a/server/templates/server/doc_type.html b/server/templates/server/doc_type.html index 35dd9f3..023ea5e 100644 --- a/server/templates/server/doc_type.html +++ b/server/templates/server/doc_type.html @@ -147,7 +147,7 @@
editModifica tipo - file_downloadScarica CSV + file_downloadScarica CSV {% if doctype.medical_data %} download_for_offlineScarica CSV (con dati medici) {% endif %} diff --git a/server/urls.py b/server/urls.py index 5937707..7906dfa 100644 --- a/server/urls.py +++ b/server/urls.py @@ -22,5 +22,7 @@ urlpatterns = [ path('progress', views.get_progress, name='progress'), path('request', views.data_request, name='request'), path('debug-uc', views.debug_uc, name='debug-uc'), + path('download-data', views.download_data, name='download-data'), + path('data-progress', views.download_data_progress, name='data-progress'), path('media///', views.media_request, name='media'), ] diff --git a/server/views.py b/server/views.py index ef13e45..b9e2dc7 100644 --- a/server/views.py +++ b/server/views.py @@ -28,6 +28,7 @@ from PIL import Image, UnidentifiedImageError import zipfile import json import threading +from pypdf import PdfMerger # custom staff check function for non primary group staff members def isStaff(user): @@ -416,7 +417,7 @@ def ulist(request): # get template and build context template = get_template('server/download_doc.html') doc = [document, KeyVal.objects.filter( - container=document), document.personal_data, document.medical_data, document.user.groups.values_list('name', flat=True)[0]] + container=document), document.personal_data, document.medical_data] context = {'doc': doc, 'vac': vac_file, 'health': health_file, 'sign_doc_file': sign_doc_file} # render context @@ -1898,6 +1899,87 @@ def debug_uc(request): } return render(request, 'server/debug_usercode.html', context) +@user_passes_test(isStaff) +def download_data(request): + groups = getGroups(request) + + # get all doctypes the user has access to + doctypes = DocumentType.objects.filter(group__in=groups) + # sort by name + doctypes = doctypes.order_by("name") + + context = { + "docs": doctypes + } + + return render(request, 'server/data_download.html', context) + +def download_data_progress(request): + # if user wants to download result + if 'download' in request.GET: + # if job is completed + if request.session['status']: + data = BytesIO(base64.b64decode(request.session['result'])) + data.seek(0) + return FileResponse(data, as_attachment=True, filename="documents_" + datetime.now().strftime("%H_%M-%d_%m_%y") + ".pdf") + + # otherwise return status + data = [request.session['progress'], request.session['total'], request.session['status']] + return HttpResponse(json.dumps(data)) + +def prepare_documents(docs, session_key): + pdf_list = [] + + # get session + session = SessionStore(session_key=session_key) + for i in docs: + vac_file = "" + health_file = "" + sign_doc_file = "" + + # prepare pictures in base64 + if i.medical_data: + if i.medical_data.vac_certificate.name: + with open(i.medical_data.vac_certificate.name, 'rb') as image_file: + vac_file = base64.b64encode( + image_file.read()).decode() + + if i.medical_data.health_care_certificate.name: + with open(i.medical_data.health_care_certificate.name, 'rb') as image_file: + health_file = base64.b64encode( + image_file.read()).decode() + if i.signed_doc: + with open(i.signed_doc.name, 'rb') as image_file: + sign_doc_file = base64.b64encode( + image_file.read()).decode() + + template = get_template('server/download_doc.html') + doc = [i, KeyVal.objects.filter( + container=i), i.personal_data, i.medical_data] + context = {'doc': doc, 'vac': vac_file, + 'health': health_file, 'sign_doc_file': sign_doc_file} + # render context + html = template.render(context) + # render pdf using wkhtmltopdf + pdf = pdfkit.from_string(html, False) + # append file + pdf_list.append(pdf) + + session['progress'] += 1 + session.save() + + merger = PdfMerger() + for pdf in pdf_list: + merger.append(BytesIO(pdf)) + + mem = BytesIO() + merger.write(mem) + mem.seek(0) + # save result + session['result'] = base64.b64encode(mem.getvalue()).decode() + session['status'] = True + session.save() + @user_passes_test(isStaff) def data_request(request): context = {} -- cgit v1.2.1