From 9f474d32ef47b02868a31bd59eaf969087d67a16 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Thu, 19 Aug 2021 12:19:41 +0200 Subject: use query instead of for loop in client --- client/templates/client/doc_create.html | 4 +- client/templates/client/doc_edit.html | 4 +- client/templates/client/index.html | 151 +++++++++++++++----------------- client/templatetags/__init__.py | 0 client/templatetags/app_filter.py | 7 ++ client/views.py | 68 ++++---------- version.txt | 2 +- 7 files changed, 103 insertions(+), 133 deletions(-) create mode 100644 client/templatetags/__init__.py create mode 100644 client/templatetags/app_filter.py diff --git a/client/templates/client/doc_create.html b/client/templates/client/doc_create.html index 04f08e6..2f70ef3 100644 --- a/client/templates/client/doc_create.html +++ b/client/templates/client/doc_create.html @@ -109,8 +109,8 @@ {% for key in keys %}
- - + +
{% endfor %} diff --git a/client/templates/client/doc_edit.html b/client/templates/client/doc_edit.html index f7e8f72..a622ea8 100644 --- a/client/templates/client/doc_edit.html +++ b/client/templates/client/doc_edit.html @@ -65,8 +65,8 @@ {% for key in keys %}
- - + +
{% endfor %} diff --git a/client/templates/client/index.html b/client/templates/client/index.html index 092fe7f..1117cba 100644 --- a/client/templates/client/index.html +++ b/client/templates/client/index.html @@ -3,6 +3,7 @@ {% block title %}Home{% endblock %} {% block content %} +{% load app_filter %}
Aggiungi un'iscrizione
@@ -34,49 +35,49 @@ {% for doc in docs %}
  • - {% if doc.0.status == "wait" %} + {% if doc.status == "wait" %} timelapse - {% elif doc.0.status == "ok" %} + {% elif doc.status == "ok" %} check - {% elif doc.0.status == "autosign" %} + {% elif doc.status == "autosign" %} assignment_turned_in {% endif %} - {{doc.0.document_type.name}} - {{doc.0.compilation_date}} + {{doc.document_type.name}} + {{doc.compilation_date}}
    - {% if doc.0.status == "wait" %} + {% if doc.status == "wait" %}

    - {% elif doc.0.status == "autosign" %} - @@ -273,52 +274,52 @@
    Scheda medica personale
    - +
    - +
    - +
    - +
    Deve assumere regolarmente medicamenti  
    - +
    Informazioni particolari sullo stato di salute: (postumi di operazioni, incidenti, malattie, disturbi fisici)  
    - +
  • {% endif %} {% endif %} - {% if doc.0.document_type.custom_data %} + {% if doc.document_type.custom_data %}
  • add_circle_outlineDati aggiuntivi @@ -326,7 +327,7 @@
    - {% for key in doc.1 %} + {% for key in doc|doc_key %} @@ -337,31 +338,26 @@ {% endif %} - {% if doc.0.document_type.medical_data %}
  • - {% if doc.0.medical_data %} - attach_fileAllegati - {% else %} - attach_fileAllegati - {% endif %} + attach_fileAllegati
    - {% if doc.0.medical_data %}
    - {% if doc.5 %} {% endif %} + {% if doc.signed_doc %} {% endif %}
    + {% if doc.medical_data %}
    - {% if doc.6 %}{% endif %} + {% if doc.medical_data.health_care_certificate %}{% endif %}
    @@ -370,15 +366,14 @@
    - {% if doc.7 %}{% endif %} + {% if doc.medical_data.vac_certificate %}{% endif %}
    + {% endif %}
    - {% endif %}
  • - {% endif %} diff --git a/client/templatetags/__init__.py b/client/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/client/templatetags/app_filter.py b/client/templatetags/app_filter.py new file mode 100644 index 0000000..3120b8e --- /dev/null +++ b/client/templatetags/app_filter.py @@ -0,0 +1,7 @@ +from django import template +from client.models import KeyVal + +register = template.Library() +@register.filter(name="doc_key") +def doc_key(doc): + return KeyVal.objects.filter(container=doc) \ No newline at end of file diff --git a/client/views.py b/client/views.py index 841b254..bc0b071 100644 --- a/client/views.py +++ b/client/views.py @@ -93,49 +93,27 @@ def index(request): 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['keys'] = KeyVal.objects.filter(container=document) 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 = "" - sign_doc_file = "" - if i.personal_data: - personal = i.personal_data - if i.medical_data: - medical = i.medical_data - if medical.vac_certificate.name: - vac_file = "/server/media/" + str(i.id) + "/vac_certificate/doc" - - if medical.health_care_certificate.name: - health_file = "/server/media/" + str(i.id) + "/health_care_certificate/doc" - - if i.signed_doc: - sign_doc_file = "/server/media/" + str(i.id) + "/signed_doc/doc" - - 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, sign_doc_file]) + Q(user=request.user) & ~Q(status='archive')).select_related("personal_data", "medical_data", "document_type", "user") + + vac_file = ["/server/media/", "/vac_certificate/doc"] + health_file = ["/server/media/", "/health_care_certificate/doc"] + sign_doc_file = ["/server/media/", "/signed_doc/doc"] context = { - "docs": out, - "empty": len(out) == 0, + "docs": documents, + "base_group": groups[0].name, + "empty": len(documents) == 0, "group_view": group_view, + "vac_file": vac_file, + "health_file": health_file, + "sign_doc_file": sign_doc_file } return render(request, 'client/index.html', context) @@ -152,14 +130,10 @@ def create(request): if not request.user.is_staff and "capi" not in request.user.groups.values_list('name',flat = True): filter = filter & Q(staff_only=False) - doctypes = DocumentType.objects.filter(filter) - 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) + # remove from the list documents from already used types + doctypes = DocumentType.objects.filter(filter).values_list("id", flat=True).difference(Document.objects.filter(user=request.user).select_related("document_type").values_list("document_type", flat=True)) - context['docs'] = out + context['docs'] = DocumentType.objects.filter(id__in=doctypes) if request.method == "POST": if request.POST["action"] == "details": # user has to select a document type @@ -184,11 +158,7 @@ def create(request): 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, ""]) - context['keys'] = out_keys + context['keys'] = Keys.objects.filter(container=document_type) context['custom_message'] = document_type.custom_message context['custom_message_text'] = document_type.custom_message_text elif request.POST["action"] == "save": @@ -258,8 +228,7 @@ def create(request): for i in request.POST.keys(): 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('/') @@ -315,8 +284,7 @@ def edit_wrapper(request, context): for i in request.POST.keys(): 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.get(id=i) key.value = request.POST[i] key.save() diff --git a/version.txt b/version.txt index b78418a..1c220c3 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ version=0.3 -rev=9 +rev=10 -- cgit v1.2.1
    {{key.key}} {{key.value}}