diff options
author | Andrea Lepori <alepori@student.ethz.ch> | 2022-01-05 20:30:42 +0100 |
---|---|---|
committer | Andrea Lepori <alepori@student.ethz.ch> | 2022-01-05 20:30:54 +0100 |
commit | 576cdae5270b12b36b8a522ce37fac26d46dbde9 (patch) | |
tree | b5fdd3d657c75510ff1faab35098650445ded4fb /client | |
parent | force user linked to midata to use midata login (diff) | |
download | scout-subs-576cdae5270b12b36b8a522ce37fac26d46dbde9.tar.gz scout-subs-576cdae5270b12b36b8a522ce37fac26d46dbde9.zip |
add radio buttons to custom parameters
Diffstat (limited to 'client')
-rw-r--r-- | client/migrations/0013_keys_key_extra.py | 18 | ||||
-rw-r--r-- | client/models.py | 1 | ||||
-rw-r--r-- | client/templates/client/doc_create.html | 28 | ||||
-rw-r--r-- | client/templates/client/doc_edit.html | 35 | ||||
-rw-r--r-- | client/templatetags/app_filter.py | 14 | ||||
-rw-r--r-- | client/views.py | 21 |
6 files changed, 102 insertions, 15 deletions
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 @@ <a href="#!" class="breadcrumb hide-on-med-and-down">Crea Iscrizione</a> {% endblock %} +{% load app_filter %} + {% block content %} <div class="row"> <div class="col l8 offset-l2 s12"> @@ -107,12 +109,28 @@ {% endif %} {% if custom_data %} {% for key in keys %} - <div class="row"> - <div class="input-field col s12"> - <input value="" name="{{key.id}}" id="{{key.id}}" type="text"> - <label for="{{key.id}}">{{key.key}}</label> + {% if key.key_extra|first in '!' %} + <div class="row"> + <div class="col s12"> + {% with arr=key.key_extra|parse_multiple_choice %} + {{arr.0}} + {% for val in arr.1 %} + <p><label> + <input class="with-gap" name="{{key.id}}" value="{{val}}" type="radio"/> + <span>{{val}}</span> + </label></p> + {% endfor %} + {% endwith %} + </div> </div> - </div> + {% else %} + <div class="row"> + <div class="input-field col s12"> + <input value="" name="{{key.id}}" id="{{key.id}}" type="text"> + <label for="{{key.id}}">{{key.key}}</label> + </div> + </div> + {% endif %} {% endfor %} {% endif %} <div class="row"> 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 @@ <a href="#!" class="breadcrumb hide-on-med-and-down">Modifica iscrizione</a> {% endblock %} +{% load app_filter %} + {% block content %} <div class="row"> <div class="col l8 offset-l2 s12"> @@ -63,12 +65,35 @@ {% endif %} {% if custom_data %} {% for key in keys %} - <div class="row"> - <div class="input-field col s12"> - <input value="{{key.value}}" name="{{key.id}}" id="{{key.id}}" type="text"> - <label for="{{key.id}}">{{key.key}}</label> + {% if key.key_extra|first in '!' %} + <div class="row"> + <div class="col s12"> + {% with arr=key.key_extra|parse_multiple_choice %} + {{arr.0}} + {% for val in arr.1 %} + {% if key.value == val %} + <p><label> + <input class="with-gap" name="{{key.key}}" value="{{val}}" type="radio" checked/> + <span>{{val}}</span> + </label></p> + {% else %} + <p><label> + <input class="with-gap" name="{{key.key}}" value="{{val}}" type="radio"/> + <span>{{val}}</span> + </label></p> + {% endif %} + {% endfor %} + {% endwith %} + </div> </div> - </div> + {% else %} + <div class="row"> + <div class="input-field col s12"> + <input value="{{key.value}}" name="{{key.key}}" id="{{key.id}}" type="text"> + <label for="{{key.id}}">{{key.key}}</label> + </div> + </div> + {% endif %} {% endfor %} {% endif %} <div class="row"> 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('/') |