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 +++++++++++++++---- server/views.py | 12 ++++++++--- templates/registration/base_client.html | 8 ++++++++ version.txt | 2 +- 9 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 client/migrations/0013_keys_key_extra.py 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('/') diff --git a/server/views.py b/server/views.py index 78be0b2..0e032f0 100644 --- a/server/views.py +++ b/server/views.py @@ -672,10 +672,16 @@ def doccreate(request): # create custom keys if custom_data: custom = request.POST["custom"] - custom.replace("\r", "") - custom = custom.split("\n") + custom = custom.splitlines() for i in custom: - key = Keys(key=i, container=doctype) + val = i + if val.startswith("!"): + if len(val) < 3: + val = val + + val = val[3:].split(",")[0] + + key = Keys(key=val, key_extra=i, container=doctype) key.save() return HttpResponseRedirect('doctype') diff --git a/templates/registration/base_client.html b/templates/registration/base_client.html index cfb6024..362238c 100644 --- a/templates/registration/base_client.html +++ b/templates/registration/base_client.html @@ -31,6 +31,14 @@ background-color: {{hexcolor}} !important; } + [type="radio"]:checked + span::after, [type="radio"].with-gap:checked + span::before, [type="radio"].with-gap:checked + span::after { + border: 2px solid {{hexcolor}}; + } + + [type="radio"]:checked + span::after, [type="radio"].with-gap:checked + span::after { + background-color: {{hexcolor}}; + } + .switch label input[type="checkbox"]:checked + .lever { background-color: {{hexlightcolor}}; } diff --git a/version.txt b/version.txt index 5ffa9dc..dd7b026 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ version=0.4 -rev=18 +rev=19 -- cgit v1.2.1