From 684c019daf76b499417c85e4ea5877a96b0ddb0e Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Tue, 23 Jun 2020 19:05:09 +0200 Subject: Nicer user list, multi group support --- accounts/views.py | 9 +- .../migrations/0025_documenttype_custom_group.py | 18 ++ client/models.py | 1 + client/templates/client/approve_doc_pdf.html | 113 +++------ client/views.py | 4 +- server/templates/server/doc_create.html | 22 +- server/templates/server/doc_list.html | 87 +++++-- server/templates/server/doc_type.html | 30 ++- server/templates/server/download_doc.html | 2 +- server/templates/server/index.html | 10 +- server/templates/server/user_list.html | 263 +++++++++++++++++++-- server/views.py | 136 ++++++----- 12 files changed, 490 insertions(+), 205 deletions(-) create mode 100644 client/migrations/0025_documenttype_custom_group.py diff --git a/accounts/views.py b/accounts/views.py index 9c2efeb..7b40c05 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -81,10 +81,11 @@ def personal(request): medic.misc = request.POST["misc"] medic.save() - if request.POST["branca"] != "": - request.user.groups.clear() - request.user.groups.add( - Group.objects.get(name=request.POST["branca"])) + if "branca" in request.POST: + if request.POST["branca"] != "": + request.user.groups.clear() + request.user.groups.add( + Group.objects.get(name=request.POST["branca"])) if "vac_certificate" in request.FILES: myfile = request.FILES['vac_certificate'] diff --git a/client/migrations/0025_documenttype_custom_group.py b/client/migrations/0025_documenttype_custom_group.py new file mode 100644 index 0000000..56c77f9 --- /dev/null +++ b/client/migrations/0025_documenttype_custom_group.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-06-23 14:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0024_auto_20200622_1930'), + ] + + operations = [ + migrations.AddField( + model_name='documenttype', + name='custom_group', + field=models.BooleanField(default=False), + ), + ] diff --git a/client/models.py b/client/models.py index 105165b..4d8ae83 100644 --- a/client/models.py +++ b/client/models.py @@ -10,6 +10,7 @@ class DocumentType(models.Model): auto_sign = models.BooleanField(default=False) group_private = models.BooleanField(default=False) group = models.ForeignKey(Group, default=None, on_delete=models.CASCADE) + custom_group = models.BooleanField(default=False) personal_data = models.BooleanField(default=False) medical_data = models.BooleanField(default=False) custom_data = models.BooleanField(default=False) diff --git a/client/templates/client/approve_doc_pdf.html b/client/templates/client/approve_doc_pdf.html index 4b7df8b..deabd99 100644 --- a/client/templates/client/approve_doc_pdf.html +++ b/client/templates/client/approve_doc_pdf.html @@ -2,98 +2,47 @@ + + -
-

{{doc.code}}

-

{{doc.document_type.name}}

-
-

Con la presente firma approvo il documento con codice {{doc.code}} accettando - tutte le clausole apposte su di esso. - Inoltre comprovo come veritieri e completi tutti i dati apposti sul documento. -

+



+



+
+

{{doc.code}}

+
+
+

{{doc.document_type.name}}

+
+
+
+

Con la presente firma approvo il documento con codice {{doc.code}} accettando + tutte le clausole apposte su di esso. + Inoltre comprovo come veritieri e completi tutti i dati apposti sul documento. +

+
















-
- - - - - -

Data

Firma

+
+
+
+ + Data e luogo +
+
+
+
+ + Firma +
+
+ \ No newline at end of file diff --git a/client/views.py b/client/views.py index b43a3c9..17d9e0a 100644 --- a/client/views.py +++ b/client/views.py @@ -92,7 +92,7 @@ def approve(request): 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 len(request.user.groups.values_list('name', flat=True)) != 0: + 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) @@ -162,7 +162,7 @@ def create(request): break document = Document( - user=request.user, group=group, code=code, status=status, document_type=document_type, personal_data=personal_data, medical_data=medical_data) + 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() if document_type.custom_data: diff --git a/server/templates/server/doc_create.html b/server/templates/server/doc_create.html index 6454bee..1c469ae 100644 --- a/server/templates/server/doc_create.html +++ b/server/templates/server/doc_create.html @@ -80,14 +80,20 @@
- - + +
- - + + +
+
+
+
+ +
@@ -100,4 +106,12 @@
+{% endblock %} + +{% block script%} +$(document).ready(function(){ + {% if error %} + M.toast({html: '{{ error_text}}', classes: 'orange'}) + {% endif %} +}); {% endblock %} \ No newline at end of file diff --git a/server/templates/server/doc_list.html b/server/templates/server/doc_list.html index 961b9ab..5206d2f 100644 --- a/server/templates/server/doc_list.html +++ b/server/templates/server/doc_list.html @@ -36,8 +36,10 @@
  • archive
  • unarchive
  • -
  • checkDEBUG
  • -
  • deleteDEBUG
  • + {% if settings.DEBUG %} +
  • checkDEBUG
  • +
  • deleteDEBUG
  • + {% endif %}
    - Lista tipo doc - Lista documenti - Approva documento + Lista tipo doc + Lista documenti + Approva documento
    diff --git a/server/templates/server/user_list.html b/server/templates/server/user_list.html index 3837619..8abf5e8 100644 --- a/server/templates/server/user_list.html +++ b/server/templates/server/user_list.html @@ -3,26 +3,251 @@ {% block title %}Admin - Lista Utenti{% endblock %} {% block nav %} - Home - Admin - Lista Utenti + Home + Admin + Lista Utenti {% endblock %} {% block content %} - - - - - - - - - {% for user in users %} - - {% for att in user %} - - {% endfor %} - - {% endfor %} -
    UsernameNomeCognomeCodiceStato
    {{att}}
    +
    +{% csrf_token %} + + +
    +{% endblock %} + +{% block script %} +document.addEventListener('DOMContentLoaded', function() { + var elems = document.querySelectorAll('.collapsible'); + var instances = M.Collapsible.init(elems, {}); +}); + +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 diff --git a/server/views.py b/server/views.py index fb6af0f..0954a1f 100644 --- a/server/views.py +++ b/server/views.py @@ -5,6 +5,7 @@ from django.db.models import Q from django.http import HttpResponseRedirect, FileResponse from django.db.models.deletion import ProtectedError from django.template.loader import get_template +from django.conf import settings import dateparser from datetime import datetime @@ -140,61 +141,27 @@ def docapprove(request): def ulist(request): context = {} if (request.user.is_staff): - parent_group = request.user.groups.values_list('name', flat=True)[ - 0] - users = User.objects.filter(groups__name=parent_group) + parent_group = request.user.groups.values_list('name', flat=True)[0] + group = Group.objects.get(name=parent_group) + if request.method == "POST": + if request.POST["action"][0] == 'f': + document = Document.objects.get(id=request.POST["action"][1:]) + if document.group == group: + template = get_template('server/download_doc.html') + doc = [document, KeyVal.objects.filter(container=document), document.personal_data, document.medical_data, parent_group] + context = {'doc': doc} + html = template.render(context) + pdf = pdfkit.from_string(html, False) + result = BytesIO(pdf) + result.seek(0) + + return FileResponse(result, as_attachment=True, filename=document.user.username+"_"+document.document_type.name+".pdf") + users = User.objects.filter(groups__name=parent_group).order_by("first_name") out = [] for user in users: - code = "" - parent_name = "" - via = "" - cap = "" - country = "" - nationality = "" - born_date = "" - home_phone = "" - phone = "" - school = "" - year = "" - status = "" - if user.is_staff: - status = "Staff" - elif user.has_perm("approved"): - status = "Attivo" - else: - status = "In attesa" - if len(UserCode.objects.filter(user=user)) > 0: - usercode = UserCode.objects.filter(user=user)[0] - code = 'U' + str(usercode.code) - 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 - school = usercode.school - year = usercode.year - else: - status = "Non registrato" - out.append([ - status, - user.username, - user.first_name, - user.last_name, - born_date, - parent_name, - user.email, - phone, - home_phone, - via, - cap, - country, - nationality, - school, - year, - code]) + usercode = UserCode.objects.filter(user=user)[0] + documents = Document.objects.filter(Q(user=user) & ~Q(status='archive')) + out.append([user, usercode, parent_group, documents]) context = {'users': out} return render(request, 'server/user_list.html', context) else: @@ -214,6 +181,7 @@ def doctype(request): medic = True custom = True message = True + group_bool = True public_check = 'checked="checked"' selfsign_check = 'checked="checked"' hidden_check = 'checked="checked"' @@ -221,11 +189,15 @@ def doctype(request): medic_check = 'checked="checked"' custom_check = 'checked="checked"' message_check = 'checked="checked"' + group_check = 'checked="checked"' if request.method == "POST": selected = [] + parent_groups = request.user.groups.values_list('name', flat=True) for i in request.POST.keys(): if i.isdigit(): - selected.append(DocumentType.objects.get(id=i)) + docc = DocumentType.objects.get(id=i) + if docc.group.name in parent_groups: + selected.append(docc) for i in selected: if request.POST["action"] == 'delete': @@ -248,6 +220,7 @@ def doctype(request): medic = "filter_medic" in request.POST custom = "filter_custom" in request.POST message = "filter_message" in request.POST + group_bool = "filter_group" in request.POST if request.POST["action"] == 'clear': public = True @@ -257,6 +230,7 @@ def doctype(request): medic = True custom = True message = True + group_bool = True parent_group = request.user.groups.values_list('name', flat=True)[ 0] @@ -284,6 +258,9 @@ def doctype(request): if not message: public_types = public_types.filter(custom_message=False) message_check = "" + if not group_bool: + public_types = public_types.filter(custom_group=False) + group_check = "" out = [] for doc in public_types: @@ -300,6 +277,7 @@ def doctype(request): 'medic_check': medic_check, 'custom_check': custom_check, 'message_check': message_check, + 'group_check': group_check, 'error': error, 'error_text': error_text, } @@ -319,12 +297,14 @@ def doccreate(request): personal_data = False medical_data = False custom_data = False + custom_group_bool = False name = "" + custom_group = "" - enabled_check = 'checked="checked' + enabled_check = 'checked="checked"' private_check = 'checked="checked"' personal_check = 'checked="checked"' - sign_check = 'checked="checked' + sign_check = 'checked="checked"' medical_check = "" custom_check = "" custom_message_check = "" @@ -347,8 +327,20 @@ def doccreate(request): custom_message = "custom_message" in request.POST.keys() custom_message_text = request.POST["custom_message_text"] name = request.POST["name"] + custom_group = request.POST["custom_group"] + + if custom_group != "": + print("here") + if custom_group not in request.user.groups.values_list('name', flat=True): + context["error"] = "true" + context["error_text"] = "Non puoi creare un tipo assegnato ad un gruppo di cui non fai parte" + return render(request, 'server/doc_create.html', context) + else: + group = Group.objects.filter(name=custom_group)[0] + custom_group_bool = True + doctype = DocumentType( - auto_sign=auto_sign, custom_message=custom_message, custom_message_text=custom_message_text, name=request.POST["name"], enabled=enabled, group_private=group_private, group=group, personal_data=personal_data, medical_data=medical_data, custom_data=custom_data) + custom_group=custom_group_bool, auto_sign=auto_sign, custom_message=custom_message, custom_message_text=custom_message_text, name=request.POST["name"], enabled=enabled, group_private=group_private, group=group, personal_data=personal_data, medical_data=medical_data, custom_data=custom_data) doctype.save() if custom_data: custom = request.POST["custom"] @@ -387,11 +379,13 @@ def doclist(request): older = zurich.localize(datetime.now()) owner = [] types = [] + groups = [] chips_owner = [] chips_types = [] + chips_groups = [] if request.method == "POST": - if request.POST["action"][0] == 'f': + if request.POST["action"][0] == 'k': document = Document.objects.get(id=request.POST["action"][1:]) if document.group == group: template = get_template('server/download_doc.html') @@ -405,9 +399,12 @@ def doclist(request): return FileResponse(result, as_attachment=True, filename=document.user.username+"_"+document.document_type.name+".pdf") selected = [] + parent_groups = request.user.groups.values_list('name', flat=True) for i in request.POST.keys(): if i.isdigit(): - selected.append(Document.objects.get(id=i)) + docc = Document.objects.get(id=i) + if docc.group.name in parent_groups: + selected.append(docc) for i in selected: if request.POST["action"] == 'delete': @@ -438,6 +435,7 @@ def doclist(request): older = zurich.localize(dateparser.parse(request.POST["older"]) + timedelta(days=1)) owner = request.POST["owner"].split("^|") types = request.POST["type"].split("^|") + groups = request.POST["groups"].split("^|") if request.POST["action"] == 'clear': hidden = False @@ -448,8 +446,14 @@ def doclist(request): older = zurich.localize(datetime.now()) owner = [] types = [] + groups = [] + + parent_groups = request.user.groups.values_list('name', flat=True) + q_obj = Q() + for i in parent_groups: + q_obj |= Q(group__name=i) - documents = Document.objects.filter(group=group) + documents = Document.objects.filter(q_obj) if not hidden: documents = documents.filter(~Q(status="archive")) @@ -485,6 +489,15 @@ def doclist(request): documents = documents.filter(q_obj) + if len(groups) > 0: + if groups[0] != "": + q_obj = Q() + for g in groups: + q_obj |= Q(group__name=g) + chips_groups.append(g) + + documents = documents.filter(q_obj) + out = [] for i in documents: personal = None @@ -503,6 +516,7 @@ def doclist(request): context = { "types": auto_types, "users": users, + "groups": parent_groups, "docs": out, "hidden_check": hidden_check, "wait_check": wait_check, @@ -512,8 +526,10 @@ def doclist(request): "older": older, "chips_owner": chips_owner, "chips_type": chips_types, + "chips_groups": chips_groups, 'error': error, 'error_text': error_text, + 'settings': settings, } return render(request, 'server/doc_list.html', context) else: -- cgit v1.2.1