From 576cdae5270b12b36b8a522ce37fac26d46dbde9 Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Wed, 5 Jan 2022 20:30:42 +0100 Subject: add radio buttons to custom parameters --- client/migrations/0013_keys_key_extra.py | 18 ++++++++++++++++ client/models.py | 1 + client/templates/client/doc_create.html | 28 ++++++++++++++++++++----- client/templates/client/doc_edit.html | 35 +++++++++++++++++++++++++++----- client/templatetags/app_filter.py | 14 ++++++++++++- client/views.py | 21 +++++++++++++++---- 6 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 client/migrations/0013_keys_key_extra.py (limited to 'client') diff --git a/client/migrations/0013_keys_key_extra.py b/client/migrations/0013_keys_key_extra.py new file mode 100644 index 0000000..2c9937c --- /dev/null +++ b/client/migrations/0013_keys_key_extra.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.4 on 2022-01-05 17:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('client', '0012_remove_usercode_midata_code'), + ] + + operations = [ + migrations.AddField( + model_name='keys', + name='key_extra', + field=models.CharField(default='', max_length=1024), + ), + ] diff --git a/client/models.py b/client/models.py index b94806b..cd3cf88 100644 --- a/client/models.py +++ b/client/models.py @@ -96,6 +96,7 @@ class Keys(models.Model): container = models.ForeignKey( DocumentType, db_index=True, on_delete=models.CASCADE) key = models.CharField(max_length=240, db_index=True) + key_extra = models.CharField(max_length=1024, default="") class UserCode(models.Model): diff --git a/client/templates/client/doc_create.html b/client/templates/client/doc_create.html index 2f70ef3..ce5f39d 100644 --- a/client/templates/client/doc_create.html +++ b/client/templates/client/doc_create.html @@ -6,6 +6,8 @@ Crea Iscrizione {% endblock %} +{% load app_filter %} + {% block content %}
@@ -107,12 +109,28 @@ {% endif %} {% if custom_data %} {% for key in keys %} -
-
- - + {% if key.key_extra|first in '!' %} +
+
+ {% with arr=key.key_extra|parse_multiple_choice %} + {{arr.0}} + {% for val in arr.1 %} +

+ {% endfor %} + {% endwith %} +
-
+ {% else %} +
+
+ + +
+
+ {% endif %} {% endfor %} {% endif %}
diff --git a/client/templates/client/doc_edit.html b/client/templates/client/doc_edit.html index a622ea8..a9cc056 100644 --- a/client/templates/client/doc_edit.html +++ b/client/templates/client/doc_edit.html @@ -6,6 +6,8 @@ Modifica iscrizione {% endblock %} +{% load app_filter %} + {% block content %}
@@ -63,12 +65,35 @@ {% endif %} {% if custom_data %} {% for key in keys %} -
-
- - + {% if key.key_extra|first in '!' %} +
+
+ {% with arr=key.key_extra|parse_multiple_choice %} + {{arr.0}} + {% for val in arr.1 %} + {% if key.value == val %} +

+ {% else %} +

+ {% endif %} + {% endfor %} + {% endwith %} +
-
+ {% else %} +
+
+ + +
+
+ {% endif %} {% endfor %} {% endif %}
diff --git a/client/templatetags/app_filter.py b/client/templatetags/app_filter.py index 9f378e1..df92775 100644 --- a/client/templatetags/app_filter.py +++ b/client/templatetags/app_filter.py @@ -34,4 +34,16 @@ def doc_count(doc): if doc.max_instances != 0: doc_count += "/" + str(doc.max_instances) - return doc_count \ No newline at end of file + return doc_count + +@register.filter(name="parse_multiple_choice") +def parse_multiple_choice(str): + if len(str) < 3: + return [str, []] + + str = str[3:] + arr = str.split(",") + if len(arr) < 2: + return [arr[0], []] + + return [arr[0], arr[1:]] \ No newline at end of file diff --git a/client/views.py b/client/views.py index 467fee6..4813e8f 100644 --- a/client/views.py +++ b/client/views.py @@ -1,3 +1,4 @@ +from django.db.models.expressions import OuterRef, Subquery from django.template.loader import get_template from client.models import GroupSettings, UserCode, Keys, DocumentType, Document, PersonalData, KeyVal, MedicalData from django.db.models import Q @@ -94,7 +95,10 @@ def index(request): context['personal_data'] = document_type.personal_data context['medical_data'] = document_type.medical_data context['custom_data'] = document_type.custom_data - context['keys'] = KeyVal.objects.filter(container=document) + keys = Keys.objects.filter(container=document_type).annotate(value=Subquery( + KeyVal.objects.filter(container=document, key=OuterRef('key')).values('value') + )) + context['keys'] = keys context['custom_message'] = document_type.custom_message context['custom_message_text'] = document_type.custom_message_text return edit_wrapper(request, context) @@ -264,6 +268,11 @@ def edit_wrapper(request, context): if document.user != request.user: return + # check if document is editable + if document.status != "wait" and document.status != "autosign": + # user is cheating + return + # update compilation date document.compilation_date = pytz.timezone('Europe/Zurich').localize(datetime.now()) document.save(update_fields=["compilation_date"]) @@ -293,9 +302,13 @@ def edit_wrapper(request, context): for i in request.POST.keys(): if i == "doc" or i == "csrfmiddlewaretoken": continue - key = KeyVal.objects.get(id=i) - key.value = request.POST[i] - key.save() + key = KeyVal.objects.filter(key=i, container=document) + if len(key) == 0: + new_key = KeyVal(container=document, key=i, value=request.POST[i]) + new_key.save() + else: + key[0].value = request.POST[i] + key[0].save() return HttpResponseRedirect('/') -- cgit v1.2.1