diff options
author | Andrea Lepori <aleporia@gmail.com> | 2024-11-12 15:27:12 +0100 |
---|---|---|
committer | Andrea Lepori <aleporia@gmail.com> | 2024-11-12 15:27:23 +0100 |
commit | 0b5be72725ffd6cd10490fbd202388e22c7f8522 (patch) | |
tree | aa3738e82a99372468084885aa7cbbbf1ef83fcd /server/views.py | |
parent | add new materialize v2 (diff) | |
download | scout-subs-0b5be72725ffd6cd10490fbd202388e22c7f8522.tar.gz scout-subs-0b5be72725ffd6cd10490fbd202388e22c7f8522.zip |
commit edits for new featuredata_download
Diffstat (limited to 'server/views.py')
-rw-r--r-- | server/views.py | 84 |
1 files changed, 83 insertions, 1 deletions
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
@@ -1899,6 +1900,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 = {}
parent_group = getGroups(request)[0]
|