From dd4d3315c156d79c1141e3b44dae440364a693fb Mon Sep 17 00:00:00 2001 From: Andrea Lepori Date: Wed, 17 Nov 2021 12:06:31 +0100 Subject: add account data validation --- accounts/templates/accounts/index.html | 75 ++++++++++++++++++++++------------ accounts/views.py | 29 +++++++++++++ 2 files changed, 78 insertions(+), 26 deletions(-) (limited to 'accounts') diff --git a/accounts/templates/accounts/index.html b/accounts/templates/accounts/index.html index 0b66290..0af8ce1 100644 --- a/accounts/templates/accounts/index.html +++ b/accounts/templates/accounts/index.html @@ -27,17 +27,22 @@ {% csrf_token %}
- + +
- + +
- + +
+
+
+ +
- - + + +
- + +
- + +
- + +
- + +
- + +
- +
- + +
- +
- +
@@ -114,23 +127,27 @@
- + +
- + +
- + +
- + +
- +
@@ -141,16 +158,19 @@
- + +
- + +
- + +
@@ -169,16 +189,19 @@
Medico di famiglia
- + +
- + +
- + +
diff --git a/accounts/views.py b/accounts/views.py index 7038ccd..c73ecfd 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -93,6 +93,11 @@ def personal(request): branca_pionieri = "" branca_rover = "" + # variables for validation + validation_dic = {} + required_fields = ["first_name", "last_name", "email", "parent_name", "via", "cap", "country", "nationality", "phone", "avs_number", "emer_name", "emer_relative", "cell_phone", "address", "health_care", "injuries", "rc", "medic_name", "medic_phone", "medic_address"] + + # variables for throwing errors to the user error = False error_text = "" @@ -144,6 +149,7 @@ def personal(request): usercode.phone = request.POST["phone"] usercode.school = request.POST["school"] usercode.avs_number = request.POST["avs_number"] + if request.POST["year"].isdigit(): usercode.year = request.POST["year"] else: @@ -174,6 +180,21 @@ def personal(request): medic.misc = request.POST["misc"] medic.save() + if request.POST["birth_date"] == "" or request.POST["birth_date"] == "01 Gennaio 1970" or request.POST["birth_date"] == "None": + validation_dic["birth_date"] = 'class="datepicker validate invalid" required="" aria-required="true"' + error = True + error_text = "Alcuni campi richiesti non sono stati compilati" + else: + validation_dic["birth_date"] = 'class="datepicker validate" required="" aria-required="true"' + + for i in required_fields: + if request.POST[i] == "": + error = True + error_text = "Alcuni campi richiesti non sono stati compilati" + validation_dic[i] = 'class="validate invalid" required="" aria-required="true"' + else: + validation_dic[i] = 'class="validate" required="" aria-required="true"' + # if "branca" in request.POST: # if request.POST["branca"] != "": # request.user.groups.clear() @@ -280,6 +301,12 @@ def personal(request): if not error: return HttpResponseRedirect("") + else: + # no post, create empty validation + validation_dic["birth_date"] = 'class="datepicker validate" required="" aria-required="true"' + for i in required_fields: + validation_dic[i] = 'class="validate" required="" aria-required="true"' + # check if user is in a group and set multiple choice to that if len(request.user.groups.values_list('name', flat=True)) == 0: branca_default = "selected" @@ -323,8 +350,10 @@ def personal(request): else: card_name = '' + # fill context context = { + 'validation_dic': validation_dic, 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email, -- cgit v1.2.1