From 50798f2814f397fbdd85b70dabca9dfb26d4cd02 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Sun, 9 Aug 2020 18:19:11 +0200 Subject: comment code, minor bug fixes --- client/templates/client/approve.html | 6 +++ client/templates/client/index.html | 42 ++++++++++-------- client/views.py | 83 ++++++++++++++++++++++++++++-------- 3 files changed, 95 insertions(+), 36 deletions(-) (limited to 'client') diff --git a/client/templates/client/approve.html b/client/templates/client/approve.html index ddfc30c..3da2677 100644 --- a/client/templates/client/approve.html +++ b/client/templates/client/approve.html @@ -30,4 +30,10 @@ {% endif %} +{% endblock %} + +{% block script %} +$(document).ready(function(){ + $('.tooltipped').tooltip(); +}); {% endblock %} \ No newline at end of file diff --git a/client/templates/client/index.html b/client/templates/client/index.html index 1915792..1a63c2e 100644 --- a/client/templates/client/index.html +++ b/client/templates/client/index.html @@ -373,25 +373,31 @@ {% block script %} $(document).ready(function(){ - $('.collapsible').collapsible(); - $('.tap-target').tapTarget(); - $('.modal').modal(); $('.tooltipped').tooltip(); - {% if empty %} - $('.tap-target').tapTarget('open'); + {% if user.is_authenticated %} + {% if user.is_staff or perms.client.approved %} + $('.collapsible').collapsible(); + $('.tap-target').tapTarget(); + $('.modal').modal(); + {% if empty %} + $('.tap-target').tapTarget('open'); + {% endif %} + {% endif %} {% endif %} }); - -$('*').click(function(event) { - if (this === event.target) { - $('.tap-target').tapTarget('close'); +{% if user.is_authenticated %} + {% if user.is_staff or perms.client.approved %} + $('*').click(function(event) { + if (this === event.target) { + $('.tap-target').tapTarget('close'); + } + }); + function send(id) { + var form = document.getElementById('form') + var action = document.getElementById('action') + action.setAttribute('value', id); + form.submit() } -}); - -function send(id) { - var form = document.getElementById('form') - var action = document.getElementById('action') - action.setAttribute('value', id); - form.submit() -} -{% endblock %} \ No newline at end of file + {% endif %} +{% endif %} +{% endblock %} diff --git a/client/views.py b/client/views.py index ae5f2d5..b1b8297 100644 --- a/client/views.py +++ b/client/views.py @@ -12,12 +12,12 @@ from io import BytesIO import pdfkit import base64 -# Create your views here. - def index(request): context = {} + # check if user is logged if (request.user.is_authenticated): + # generate code if user has no code users = UserCode.objects.filter(user=request.user) code = None if (len(users) == 0): @@ -30,16 +30,21 @@ def index(request): userCode = UserCode(user=request.user, code=code, medic=medic) userCode.save() + # user action if request.method == "POST": + # get document id document = Document.objects.get(id=request.POST["action"][1:]) + # check if document is valid to modify if document.user != request.user: return if document.status == "ok" or document.status == "archive": return + # execute action if request.POST["action"][0] == 'f': + # generate approve pdf template = get_template('client/approve_doc_pdf.html') context = {'doc': document} html = template.render(context) @@ -47,20 +52,22 @@ def index(request): result = BytesIO(pdf) result.seek(0) return FileResponse(result, as_attachment=True, filename=document.document_type.name+".pdf") - elif request.POST["action"][0] == 'a': + # sign autosign doc if document.status == "autosign": document.status = "ok" document.save() return HttpResponseRedirect("/") elif request.POST["action"][0] == 'd': + # delete doc document.delete() return HttpResponseRedirect("/") elif request.POST["action"][0] == 'e': + # edit doc generate context and render edit page document_type = document.document_type context = { 'doctype': document_type, - } + } context['doc'] = document context['personal_data'] = document_type.personal_data context['medical_data'] = document_type.medical_data @@ -68,15 +75,19 @@ def index(request): keys = Keys.objects.filter(container=document_type) out_keys = [] for i in keys: - out_keys.append([i, KeyVal.objects.filter(Q(container=document) & Q(key=i.key))[0].value]) + out_keys.append([i, KeyVal.objects.filter( + Q(container=document) & Q(key=i.key))[0].value]) context['keys'] = out_keys context['custom_message'] = document_type.custom_message context['custom_message_text'] = document_type.custom_message_text return edit_wrapper(request, context) - documents = Document.objects.filter(Q(user=request.user) & ~Q(status='archive')) + # show only docs of the user and non archived + documents = Document.objects.filter( + Q(user=request.user) & ~Q(status='archive')) out = [] for i in documents: + # for every document prepare images in base64 personal = None medical = None vac_file = "" @@ -92,11 +103,14 @@ def index(request): if medical.health_care_certificate.name: with open(medical.health_care_certificate.name, 'rb') as image_file: - health_file = base64.b64encode(image_file.read()).decode() + health_file = base64.b64encode( + image_file.read()).decode() doc_group = i.user.groups.values_list('name', flat=True)[0] - out.append([i, KeyVal.objects.filter(container=i), personal, medical, doc_group, vac_file, health_file]) + out.append([i, KeyVal.objects.filter(container=i), + personal, medical, doc_group, vac_file, health_file]) + context = { "docs": out, "empty": len(out) == 0, @@ -108,6 +122,7 @@ def index(request): @login_required def approve(request): context = {} + # if user not approved and has enough data then give instructions how to approve user if not (request.user.is_staff or request.user.has_perm('approved')): usercode = UserCode.objects.filter(user=request.user)[0] okay = False @@ -118,26 +133,34 @@ def approve(request): else: return render(request, 'client/index.html', context) + @login_required def create(request): context = {} + # group name and obj parent_group = request.user.groups.values_list('name', flat=True)[ 0] group = Group.objects.get(name=parent_group) + + # get available types for user doctypes = DocumentType.objects.filter( (Q(group_private=False) | Q(group=group)) & Q(enabled=True)) out = [] for doc in doctypes: + # check if user has already that document type if len(Document.objects.filter(Q(user=request.user) & Q(document_type=doc))) == 0: out.append(doc) context['docs'] = out if request.method == "POST": if request.POST["action"] == "details": + # user has to select a document type if "doctype" not in request.POST.keys(): + # if no type selected throw error context['error'] = True context['error_text'] = "Seleziona un documento" else: + # gather data to ask to the user context['next'] = True document_type = DocumentType.objects.get( id=request.POST["doctype"]) @@ -153,71 +176,90 @@ def create(request): context['custom_message'] = document_type.custom_message context['custom_message_text'] = document_type.custom_message_text elif request.POST["action"] == "save": + # after type was selected it shows details to complete + + # get selected type document_type = DocumentType.objects.get( id=request.POST["doctype"]) + # get list of docs with that type current_docs = Document.objects.filter(document_type=document_type) if len(current_docs) > 0: + # if there is already a document with that type abort (user is cheating) return + # set default values usercode = UserCode.objects.filter(user=request.user)[0] code = 0 status = "wait" personal_data = None medical_data = None + # set to auto_sign if it is the case if document_type.auto_sign: status = "autosign" keys = [] + # copy personal data and medical data if document_type.personal_data: personal_data = PersonalData(email=request.user.email, parent_name=usercode.parent_name, via=usercode.via, cap=usercode.cap, country=usercode.country, - nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone) + nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone) personal_data.save() if document_type.medical_data: medic = usercode.medic - medical_data = MedicalData(vac_certificate=medic.vac_certificate, health_care_certificate=medic.health_care_certificate, emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) + medical_data = MedicalData(vac_certificate=medic.vac_certificate, health_care_certificate=medic.health_care_certificate, emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, + rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) medical_data.save() + # generate document code while (True): code = randint(100000, 999999) if len(Document.objects.filter(code=code)) == 0: break + # save document document = Document( user=request.user, group=document_type.group, code=code, status=status, document_type=document_type, personal_data=personal_data, medical_data=medical_data) document.save() + # attach custom keys if document_type.custom_data: for i in request.POST.keys(): - if i == "doctype" or i=="csrfmiddlewaretoken" or i=="action": + if i == "doctype" or i == "csrfmiddlewaretoken" or i == "action": continue - key = KeyVal(container=document, key=Keys.objects.get(id=i).key, value=request.POST[i]) + key = KeyVal(container=document, key=Keys.objects.get( + id=i).key, value=request.POST[i]) key.save() return HttpResponseRedirect('/') return render(request, 'client/doc_create.html', context) + +# helper function to call edit_wrapper with empty context @login_required def edit(request): return edit_wrapper(request, {}) + @login_required def edit_wrapper(request, context): if request.method == "POST": if "action" not in request.POST.keys(): + # get document document = Document.objects.get(id=request.POST["doc"]) + # check if user has permission if document.user != request.user: return - + + # save again all data usercode = UserCode.objects.filter(user=document.user)[0] if document.document_type.personal_data: personal_data = PersonalData(email=request.user.email, parent_name=usercode.parent_name, via=usercode.via, cap=usercode.cap, country=usercode.country, - nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone) + nationality=usercode.nationality, born_date=usercode.born_date, home_phone=usercode.home_phone, phone=usercode.phone) personal_data.save() old_data = document.personal_data document.personal_data = personal_data @@ -226,18 +268,21 @@ def edit_wrapper(request, context): if document.document_type.medical_data: medic = usercode.medic - medical_data = MedicalData(vac_certificate=medic.vac_certificate, health_care_certificate=medic.health_care_certificate, emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) + medical_data = MedicalData(vac_certificate=medic.vac_certificate, health_care_certificate=medic.health_care_certificate, emer_name=medic.emer_name, emer_relative=medic.emer_relative, cell_phone=medic.cell_phone, address=medic.address, emer_phone=medic.emer_phone, health_care=medic.health_care, injuries=medic.injuries, + rc=medic.rc, rega=medic.rega, medic_name=medic.medic_name, medic_phone=medic.medic_phone, medic_address=medic.medic_address, sickness=medic.sickness, vaccine=medic.vaccine, tetanus_date=medic.tetanus_date, allergy=medic.allergy, drugs_bool=medic.drugs_bool, drugs=medic.drugs, misc_bool=medic.misc_bool, misc=medic.misc) medical_data.save() old_data = document.medical_data document.medical_data = medical_data document.save() old_data.delete() + # update again custom keys if document.document_type.custom_data: for i in request.POST.keys(): - if i == "doc" or i=="csrfmiddlewaretoken": + if i == "doc" or i == "csrfmiddlewaretoken": continue - key = KeyVal.objects.filter(Q(container=document) & Q(key=Keys.objects.get(id=i).key))[0] + key = KeyVal.objects.filter(Q(container=document) & Q( + key=Keys.objects.get(id=i).key))[0] key.value = request.POST[i] key.save() @@ -245,11 +290,13 @@ def edit_wrapper(request, context): return render(request, 'client/doc_edit.html', context) + def about(request): + # very simple about page, get version from text file version = "" with open("version.txt", 'r') as f: version = f.read() if version.startswith("0"): version = "Beta " + version context = {"version": version} - return render(request, 'client/about.html', context) \ No newline at end of file + return render(request, 'client/about.html', context) -- cgit v1.2.1