From f4d03e6016950c41836100e97b206a823f7cb090 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Thu, 3 Sep 2020 18:18:03 +0200 Subject: better wording, simpler approve page --- accounts/templates/accounts/index.html | 4 +- client/templates/client/approve.html | 32 ----- client/templates/client/index.html | 11 +- client/urls.py | 1 - client/views.py | 216 ++++++++++++++++----------------- 5 files changed, 114 insertions(+), 150 deletions(-) delete mode 100644 client/templates/client/approve.html diff --git a/accounts/templates/accounts/index.html b/accounts/templates/accounts/index.html index 8769a61..847e44c 100644 --- a/accounts/templates/accounts/index.html +++ b/accounts/templates/accounts/index.html @@ -83,11 +83,11 @@
- +
- +
diff --git a/client/templates/client/approve.html b/client/templates/client/approve.html deleted file mode 100644 index 785e719..0000000 --- a/client/templates/client/approve.html +++ /dev/null @@ -1,32 +0,0 @@ -{% extends 'registration/base_client.html' %} - -{% block title %}Approva{% endblock %} - -{% block breadcrumb %} - Approva -{% endblock %} - -{% block content %} -{% if okay %} -
-
-
-
-

Per approvare l'utente invia il seguente codice al capo branca:

-

{{code}}

-

-
-
-
-{% else %} -
-
-
-
-

Per approvare l'utente inserire prima i propri dati personali e medici cliccando il proprio nome in alto a destra.

-
-
-
-
-{% endif %} -{% endblock %} \ No newline at end of file diff --git a/client/templates/client/index.html b/client/templates/client/index.html index e0f09db..a62e907 100644 --- a/client/templates/client/index.html +++ b/client/templates/client/index.html @@ -351,9 +351,14 @@

Il tuo utente non è ancora stato approvato.

-
-
- Come farsi approvare l'utente +
+ {% if user_code %} +

Per approvare l'utente invia il seguente codice al capo branca:

+

{{user_code}}

+ {% else %} +

Per approvare l'utente inserire prima i propri dati personali e medici cliccando il proprio nome in alto a destra. Torna in seguito su questa pagina per i prossimi passaggi.

+

Per approvare l'utente inserire prima i propri dati personali e medici cliccando person. Torna in seguito su questa pagina per i prossimi passaggi.

+ {% endif %}

diff --git a/client/urls.py b/client/urls.py index b0477d1..4da5169 100644 --- a/client/urls.py +++ b/client/urls.py @@ -4,7 +4,6 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('approve', views.approve, name='approve'), path('create', views.create, name='create'), path('edit', views.edit, name='edit'), path('about', views.about, name='about'), diff --git a/client/views.py b/client/views.py index eb2586a..a0143a2 100644 --- a/client/views.py +++ b/client/views.py @@ -18,123 +18,115 @@ 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): - while (True): - code = randint(100000, 999999) - if len(UserCode.objects.filter(code=code)) == 0: - break - medic = MedicalData() - medic.save() - 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) - pdf = pdfkit.from_string(html, False) - result = BytesIO(pdf) - result.seek(0) - return FileResponse(result, filename=document.document_type.name+".pdf") - elif request.POST["action"][0] == 'a': - # sign autosign doc - if document.status == "autosign": - document.status = "ok" - document.save() + if not (request.user.is_staff or request.user.has_perm("client.approved")): + # generate code if user has no code + users = UserCode.objects.filter(user=request.user) + code = None + if (len(users) == 0): + while (True): + code = randint(100000, 999999) + if len(UserCode.objects.filter(code=code)) == 0: + break + medic = MedicalData() + medic.save() + userCode = UserCode(user=request.user, code=code, medic=medic) + userCode.save() + + user_code = None + usercode = UserCode.objects.filter(user=request.user)[0] + if request.user.first_name != "" and request.user.last_name != "" and request.user.email != "" and usercode.phone != "": + user_code = "U" + str(usercode.code) + context = {"user_code": user_code} + else: + # 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) + pdf = pdfkit.from_string(html, False) + result = BytesIO(pdf) + result.seek(0) + return FileResponse(result, 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] == '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 - context['custom_data'] = document_type.custom_data - 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]) - 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) - - # 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 = "" - health_file = "" - if i.personal_data: - personal = i.personal_data - if i.medical_data: - medical = i.medical_data - - if medical.vac_certificate.name: - with open(medical.vac_certificate.name, 'rb') as image_file: - vac_file = base64.b64encode(image_file.read()).decode() - - 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() - - 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]) - - context = { - "docs": out, - "empty": len(out) == 0, - } + 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 + context['custom_data'] = document_type.custom_data + 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]) + 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) + + # 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 = "" + health_file = "" + if i.personal_data: + personal = i.personal_data + if i.medical_data: + medical = i.medical_data + + if medical.vac_certificate.name: + with open(medical.vac_certificate.name, 'rb') as image_file: + vac_file = base64.b64encode(image_file.read()).decode() + + 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() + + 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]) + + context = { + "docs": out, + "empty": len(out) == 0, + } return render(request, 'client/index.html', context) -@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 - if request.user.first_name != "" and request.user.last_name != "" and request.user.email != "" and usercode.phone != "": - okay = True - context = {'code': 'U' + str(usercode.code), 'okay': okay} - return render(request, 'client/approve.html', context) - else: - return render(request, 'client/index.html', context) - - @login_required def create(request): context = {} -- cgit v1.2.1