From 58f3293762b098ae0f4668fe2118aa7198f162ca Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Thu, 19 Aug 2021 22:19:58 +0200 Subject: remove some for loops user db query instead --- server/templates/server/data_request.html | 10 +- server/templates/server/doc_type.html | 2 +- server/templates/server/user_list.html | 101 ++++++++-------- server/views.py | 194 +++++++++++------------------- 4 files changed, 134 insertions(+), 173 deletions(-) (limited to 'server') diff --git a/server/templates/server/data_request.html b/server/templates/server/data_request.html index fbbcbc0..7728cd9 100644 --- a/server/templates/server/data_request.html +++ b/server/templates/server/data_request.html @@ -29,7 +29,7 @@
Invia - Copia risultato + Copia risultato
@@ -38,6 +38,12 @@ {% endblock %} {% block script %} +$(document).ready(function(){ + {% if error %} + M.toast({html: '{{ error }}', classes: 'orange'}) + {% endif %} +}); + document.addEventListener('DOMContentLoaded', function() { var elems = document.querySelectorAll('select'); var options = {}; @@ -45,6 +51,8 @@ document.addEventListener('DOMContentLoaded', function() { }); function copy() { + M.toast({html: 'Valore copiato!', classes: 'green'}) + var copyText = document.getElementById("data"); copyText.select(); copyText.setSelectionRange(0, 99999); /*For mobile devices*/ diff --git a/server/templates/server/doc_type.html b/server/templates/server/doc_type.html index 1ef1433..047a15a 100644 --- a/server/templates/server/doc_type.html +++ b/server/templates/server/doc_type.html @@ -146,7 +146,7 @@
edit Modifica tipo - file_download Scarica CSV + file_download Scarica CSV {% if doctype.0.medical_data %} file_download Scarica CSV (con dati medici) {% endif %} diff --git a/server/templates/server/user_list.html b/server/templates/server/user_list.html index aa2f43e..a14daea 100644 --- a/server/templates/server/user_list.html +++ b/server/templates/server/user_list.html @@ -18,6 +18,7 @@ {% endblock %} {% block content %} +{% load app_filter %}
{% csrf_token %} @@ -25,43 +26,43 @@ {% for user in users %}
  • - {% if user.0.is_staff %} + {% if user.user.is_staff %} star - {% elif user.6 %} + {% elif "capi" in user.user|user_groups %} star_border {% endif %} - {{user.0.first_name}} {{user.0.last_name}} + {{user.user.first_name}} {{user.user.last_name}}
    - {% if not user.0.is_staff %} -
    - +
    - +
    - +
    @@ -190,7 +191,7 @@ È sostenitore REGA   @@ -201,15 +202,15 @@
    Medico di famiglia
    - +
    - +
    - +
  • @@ -218,45 +219,45 @@
    Scheda medica personale
    - +
    - +
    - +
    - +
    Deve assumere regolarmente medicamenti  
    - +
    Informazioni particolari sullo stato di salute: (postumi di operazioni, incidenti, malattie, disturbi fisici)  
    - +
    @@ -270,7 +271,7 @@
    - {% if user.4 %}{% endif %} + {% if user.medic.health_care_certificate %}{% endif %}
    @@ -279,7 +280,7 @@
    - {% if user.5 %}{% endif %} + {% if user.medic.vac_certificate %}{% endif %}
    @@ -292,7 +293,7 @@
      - {% for doc in user.3 %} + {% for doc in request.user|user_docs:user.user %}
    • diff --git a/server/views.py b/server/views.py index 54f1352..0cdcf2a 100644 --- a/server/views.py +++ b/server/views.py @@ -270,9 +270,7 @@ def ulist(request): user = User.objects.get(id=request.POST["action"][1:]) # check if user has permission to deapprove user if user.groups.all()[0] == group: - content_type = ContentType.objects.get_for_model(Document) - permission = Permission.objects.get( - content_type=content_type, codename="approved") + permission = Permission.objects.get(codename="approved") user.user_permissions.remove(permission) return HttpResponseRedirect("ulist") # make user "capo" @@ -290,35 +288,18 @@ def ulist(request): return HttpResponseRedirect("ulist") # list users with their documents - users = list(User.objects.filter( - groups__name=parent_group).filter(groups__name="capi").order_by("first_name")) - users += list(User.objects.filter( - groups__name=parent_group).exclude(groups__name="capi").order_by("first_name")) - out = [] - for user in users: - # list only approved users - if not user.has_perm("client.approved") and not user.is_staff: - continue + permission = Permission.objects.get(codename="approved") - usercode = UserCode.objects.filter(user=user)[0] - # get all user documents - documents = Document.objects.filter(Q(user=user) & ~Q( - status='archive') & Q(group__name=parent_group)) + usercodes = UserCode.objects.filter(Q(user__user_permissions=permission) | Q(user__is_staff=True)).select_related("user", "medic").order_by("user__last_name") - # generate link for images - vac_file = "" - health_file = "" - if usercode.medic: - if usercode.medic.vac_certificate.name: - vac_file = "/server/media/" + str(usercode.id) + "/vac_certificate/usercode" + vac_file = ["/server/media/", "/vac_certificate/usercode"] + health_file = ["/server/media/", "/health_care_certificate/usercode"] - if usercode.medic.health_care_certificate.name: - health_file = "/server/media/" + str(usercode.id) + "/health_care_certificate/usercode" - - out.append([user, usercode, parent_group, - documents, vac_file, health_file, "capi" in user.groups.values_list('name',flat = True)]) - - context = {'users': out} + context = { + 'users': usercodes, + 'vac_file': vac_file, + 'health_file': health_file, + } return render(request, 'server/user_list.html', context) @@ -385,14 +366,14 @@ def doctype(request): return docedit_wrapper(request, context) # check if request to download - elif request.POST["action"][0] == 'd': + elif request.POST["action"][0] == 'p': document_type = DocumentType.objects.get(id=request.POST["action"][1:]) # check if user has permission on the document if document_type.group.name not in parent_groups: return - docs = Document.objects.filter(document_type=document_type) + docs = Document.objects.filter(document_type=document_type).select_related("personal_data", "medical_data", "user") # get time for filename current_time = datetime.strftime(datetime.now(), "%H_%M__%d_%m_%y") @@ -519,26 +500,23 @@ def doctype(request): docc = DocumentType.objects.get(id=i) # check if user has permission if docc.group.name in parent_groups: - selected.append(docc) + # execute action + if request.POST["action"] == 'delete': + try: + docc.delete() + except ProtectedError: + error = True + error_text = "Non puoi eliminare un tipo a cui é collegato uno o piú documenti" + elif request.POST["action"] == 'hide': + docc.enabled = False + docc.save() + elif request.POST["action"] == 'show': + docc.enabled = True + docc.save() else: error = True error_text = "Non puoi modificare un documento non del tuo gruppo" - # execute action on selected types - for i in selected: - if request.POST["action"] == 'delete': - try: - i.delete() - except ProtectedError: - error = True - error_text = "Non puoi eliminare un tipo a cui é collegato uno o piú documenti" - elif request.POST["action"] == 'hide': - i.enabled = False - i.save() - elif request.POST["action"] == 'show': - i.enabled = True - i.save() - # check which filters are applied public = "filter_public" in request.POST selfsign = "filter_selfsign" in request.POST @@ -561,9 +539,7 @@ def doctype(request): group_bool = True # get documents from the list - q_obj = Q() - for i in parent_groups: - q_obj |= Q(group__name=i) + q_obj = Q(group__name__in=parent_groups) public_types = DocumentType.objects.filter(q_obj) @@ -945,31 +921,30 @@ def doclist(request): if docc.group.name in parent_groups: selected.append(docc) - # execute action on selected documents - for i in selected: - if request.POST["action"] == 'delete' and settings.DEBUG: - i.delete() - elif request.POST["action"] == 'approve' and settings.DEBUG: - i.status = 'ok' - i.save() - elif request.POST["action"] == 'archive': - if i.status == 'ok': - i.status = 'archive' - if i.medical_data: - i.medical_data.delete() - i.medical_data.save() - i.medical_data = None - i.save() - else: - error = True - error_text = "Non puoi archiviare un documento non approvato" - elif request.POST["action"] == 'unarchive': - if i.status == 'archive': - i.status = 'ok' - i.save() - else: - error = True - error_text = "Non puoi dearchiviare un documento non archiviato" + # execute action on selected documents + if request.POST["action"] == 'delete' and settings.DEBUG: + docc.delete() + elif request.POST["action"] == 'approve' and settings.DEBUG: + docc.status = 'ok' + docc.save() + elif request.POST["action"] == 'archive': + if docc.status == 'ok': + docc.status = 'archive' + if docc.medical_data: + docc.medical_data.delete() + docc.medical_data.save() + docc.medical_data = None + docc.save() + else: + error = True + error_text = "Non puoi archiviare un documento non approvato" + elif request.POST["action"] == 'unarchive': + if docc.status == 'archive': + docc.status = 'ok' + docc.save() + else: + error = True + error_text = "Non puoi dearchiviare un documento non archiviato" # get filter values hidden = "filter_hidden" in request.POST @@ -998,9 +973,7 @@ def doclist(request): groups = [] # filter documents based on group of staff - q_obj = Q() - for i in parent_groups: - q_obj |= Q(group__name=i) + q_obj = Q(group__name__in=parent_groups) documents = Document.objects.filter(q_obj) @@ -1026,34 +999,26 @@ def doclist(request): # filter types, owner, groups using chips if len(types) > 0: if types[0] != "": - q_obj = Q() - for t in types: - q_obj |= Q(document_type__name=t) - chips_types.append(t) - + q_obj = Q(document_type__name__in=types) + chips_types += types documents = documents.filter(q_obj) if len(owner) > 0: if owner[0] != "": - q_obj = Q() - for u in owner: - user = u.split("(")[0][:-1] - q_obj |= Q(user__username=user) - chips_owner.append(u) - + q_obj = Q(user__username__in=list(map(lambda x: x.split("(")[0][:-1], owner))) + chips_owner += owner 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) - + q_obj = Q(group__name__in=groups) + chips_groups += groups documents = documents.filter(q_obj) out = [] users = [] + + # TODO convert this in a query for i in documents: # filter for confirmed with attachment documents and approved if signdoc: @@ -1287,34 +1252,25 @@ def doclist_readonly(request): # filter types, owner, groups using chips if len(types) > 0: if types[0] != "": - q_obj = Q() - for t in types: - q_obj |= Q(document_type__name=t) - chips_types.append(t) - + q_obj = Q(document_type__name__in=types) + chips_types += types documents = documents.filter(q_obj) if len(owner) > 0: if owner[0] != "": - q_obj = Q() - for u in owner: - user = u.split("(")[0][:-1] - q_obj |= Q(user__username=user) - chips_owner.append(u) - + q_obj = Q(user__username__in=list(map(lambda x: x.split("(")[0][:-1], owner))) + chips_owner += owner 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) - + q_obj = Q(group__name__in=groups) + chips_groups += groups documents = documents.filter(q_obj) out = [] users = [] + # TODO user query instead of for for i in documents: # filter for confirmed with attachment documents and approved if signdoc: @@ -1584,19 +1540,15 @@ def data_request(request): parent_group = request.user.groups.values_list('name', flat=True)[0] if request.method == "POST": - if request.POST["request"] == "email_all": - users = User.objects.filter(groups__name=parent_group) - data = "" - for user in users: - data += user.email + ", " - data = data[:-2] + if "request" not in request.POST.keys(): + context["error"] = "Selezionare una richesta" + elif request.POST["request"] == "email_all": + users_email = User.objects.filter(groups__name=parent_group).values_list("email", flat=True) + data = ", ".join(users_email) context["data"] = data elif request.POST["request"] == "email_non_staff": - users = User.objects.filter(groups__name=parent_group).exclude(groups__name="capi") - data = "" - for user in users: - data += user.email + ", " - data = data[:-2] + users_email = User.objects.filter(groups__name=parent_group).exclude(groups__name="capi").values_list("email", flat=True) + data = ", ".join(users_email) context["data"] = data elif request.POST["request"] == "data_user": users = User.objects.filter(groups__name=parent_group) -- cgit v1.2.1