diff options
-rw-r--r-- | accounts/templates/accounts/index.html | 4 | ||||
-rw-r--r-- | client/templates/client/approve.html | 32 | ||||
-rw-r--r-- | client/templates/client/index.html | 11 | ||||
-rw-r--r-- | client/urls.py | 1 | ||||
-rw-r--r-- | client/views.py | 216 |
5 files changed, 114 insertions, 150 deletions
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 @@ </div> <div class="input-field col l8 s12"> <input value="{{school}}" name="school" id="school" type="text" > - <label for="school">Scuola frequentata</label> + <label for="school">Scuola frequentata (o professione)</label> </div> <div class="input-field col l4 s12"> <input value="{{year}}" name="year" id="year" type="text" > - <label for="year">Classe</label> + <label for="year">Classe scolastica</label> </div> </div> <div class="fixed-action-btn"> 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 %} - <a class="breadcrumb hide-on-med-and-down">Approva</a> -{% endblock %} - -{% block content %} -{% if okay %} - <div class="row"> - <div class="col l4 offset-l4 m8 offset-m2 s12"> - <div class="card"> - <div class="card-content"> - <p>Per approvare l'utente invia il seguente codice al capo branca:</p> - <h3 class="center-align">{{code}}<h3> - </div> - </div> - </div> - </div> -{% else %} - <div class="row"> - <div class="col l4 offset-l4 m8 offset-m2 s12"> - <div class="card"> - <div class="card-content"> - <p>Per approvare l'utente inserire prima i propri dati personali e medici cliccando il proprio nome in alto a destra.</p> - </div> - </div> - </div> - </div> -{% 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 @@ <div class="card"> <div class="card-content"> <p>Il tuo utente non è ancora stato approvato.</p> - </div> - <div class="card-action"> - <a href="{% url 'approve' %}">Come farsi approvare l'utente</a> + <br> + {% if user_code %} + <p>Per approvare l'utente invia il seguente codice al capo branca:</p> + <h3 class="center-align">{{user_code}}<h3> + {% else %} + <p class="hide-on-small-only">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.</p> + <p class="hide-on-med-and-up">Per approvare l'utente inserire prima i propri dati personali e medici cliccando <i class="material-icons tiny">person</i>. Torna in seguito su questa pagina per i prossimi passaggi.</p> + {% endif %} </div> </div> </div> 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,124 +18,116 @@ 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 = {} # group name and obj |