From 81bd6f6a265ae1b2390a4a01c97b4b79e70169d3 Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Wed, 6 Nov 2013 20:07:33 +0100 Subject: [PATCH] Added error checking in case someone writes a long love letter to design in 'paper_size' or similar fields and doesn't want the form to crash. --- bestilling/bestilling/baseviews.py | 4 ++-- bestilling/bestilling/utils.py | 4 ++-- bestilling/design/views.py | 12 ++++++------ bestilling/prm/views.py | 4 ++-- bestilling/tekst/views.py | 9 +++++---- bestilling/templates/design/form.html | 4 ++-- bestilling/templates/form_base.html | 4 ++-- bestilling/templates/prm/form.html | 2 +- bestilling/templates/tekst/form.html | 4 ++-- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/bestilling/bestilling/baseviews.py b/bestilling/bestilling/baseviews.py index 4900518..cd08311 100644 --- a/bestilling/bestilling/baseviews.py +++ b/bestilling/bestilling/baseviews.py @@ -52,9 +52,9 @@ class BaseFormView(TemplateView): def _get_errors(self, params): return { - 'client_error' : params['client'] == '', + 'client_error' : params['client'] == '' or len(params['client']) > 50, 'deadline_error' : params['deadline'] == '', - 'contact_name_error' : params['contact_name'] == '', + 'contact_name_error' : params['contact_name'] == '' or len(params['contact_name']) > 50, 'contact_email_error' : not utils.is_email_valid(params['contact_email']), 'contact_number_error' : not utils.is_phone_valid(params['contact_number']), } diff --git a/bestilling/bestilling/utils.py b/bestilling/bestilling/utils.py index 74ffb91..63ba1f4 100644 --- a/bestilling/bestilling/utils.py +++ b/bestilling/bestilling/utils.py @@ -2,8 +2,8 @@ import re def is_phone_valid(phone_str): pattern = re.compile('^(\+\d{2,4})?\s*(\s?\d){6,}$') - return pattern.match(phone_str) is not None + return pattern.match(phone_str) is not None and len(phone_str) <= 20 def is_email_valid(email_str): pattern = re.compile('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$') - return pattern.match(email_str) is not None + return pattern.match(email_str) is not None and len(email_str) <= 50 diff --git a/bestilling/design/views.py b/bestilling/design/views.py index 8705034..4d8bba4 100644 --- a/bestilling/design/views.py +++ b/bestilling/design/views.py @@ -32,12 +32,12 @@ class DesignFormView(BaseFormView): def _get_errors(self, params): errors = super(DesignFormView, self)._get_errors(params) errors.update({ - 'format_error' : params['format'] == '', - 'format_other_error' : params['format'] == 'other' and params['format_other'] == '', - 'paper_size_error' : params['paper_size'] == '', - 'paper_size_other_error' : params['paper_size'] == 'other' and params['paper_size_other'] == '', - 'colour_error' : params['colour'] == '', - 'marger_error' : params['marger'] == '', + 'format_error' : params['format'] == '' or len(params['format']) > 50, + 'format_other_error' : params['format'] == 'other' and (params['format_other'] == '' or len(params['format_other']) > 50), + 'paper_size_error' : params['paper_size'] == '' or len(params['paper_size']) > 50, + 'paper_size_other_error' : params['paper_size'] == 'other' and (params['paper_size_other'] == '' or len(params['paper_size_other']) > 50), + 'colour_error' : params['colour'] == '' or len(params['colour']) > 50, + 'marger_error' : params['marger'] == '' or len(params['marger']) > 50, 'content_error' : False, }) diff --git a/bestilling/prm/views.py b/bestilling/prm/views.py index e166f90..0f669f8 100644 --- a/bestilling/prm/views.py +++ b/bestilling/prm/views.py @@ -28,8 +28,8 @@ class PrmFormView(BaseFormView): def _get_errors(self, params): errors = super(PrmFormView, self)._get_errors(params) errors.update({ - 'assignment_type_error' : params['assignment_type'] == '', - 'assignment_type_other_error' : params['assignment_type'] == 'other' and params['assignment_type_other'] == '', + 'assignment_type_error' : params['assignment_type'] == '' or len(params['assignment_type']) > 50, + 'assignment_type_other_error' : params['assignment_type'] == 'other' and (params['assignment_type_other'] == '' or len(params['assignment_type_other']) > 50), 'content_error' : params['content'] == '', }) diff --git a/bestilling/tekst/views.py b/bestilling/tekst/views.py index c8fc6fc..3ae544e 100644 --- a/bestilling/tekst/views.py +++ b/bestilling/tekst/views.py @@ -32,10 +32,11 @@ class TekstFormView(BaseFormView): def _get_errors(self, params): errors = super(TekstFormView, self)._get_errors(params) errors.update({ - 'text_type_error' : params['text_type'] == '', - 'text_type_other_error' : params['text_type'] == 'other' and params['text_type_other'] == '', - 'length_error' : params['length'] == '', - 'image_error' : params['image'] == '', + 'text_type_error' : params['text_type'] == '' or len(params['text_type']) > 50, + 'text_type_other_error' : params['text_type'] == 'other' and (params['text_type_other'] == '' or len(params['text_type_other']) > 50), + 'length_error' : params['length'] == '' or len(params['length']) > 50, + 'image_error' : params['image'] == '' or len(params['image']) > 50, + 'interview_name_error' : len(params['interview_name']) > 20, 'content_error' : False, }) diff --git a/bestilling/templates/design/form.html b/bestilling/templates/design/form.html index bfbddb2..214d411 100644 --- a/bestilling/templates/design/form.html +++ b/bestilling/templates/design/form.html @@ -33,7 +33,7 @@ {% if errors.format_other_error %} - + {% endif %} @@ -62,7 +62,7 @@ Annet: {% if errors.paper_size_other_error %} - + {% endif %} diff --git a/bestilling/templates/form_base.html b/bestilling/templates/form_base.html index 41603be..0476cad 100644 --- a/bestilling/templates/form_base.html +++ b/bestilling/templates/form_base.html @@ -31,7 +31,7 @@ {% if errors.client_error %} - + {% endif %} @@ -53,7 +53,7 @@
{% if errors.contact_name_error %} - + {% endif %}
diff --git a/bestilling/templates/prm/form.html b/bestilling/templates/prm/form.html index 59cbd05..9bb9a77 100644 --- a/bestilling/templates/prm/form.html +++ b/bestilling/templates/prm/form.html @@ -27,7 +27,7 @@ {% if errors.assignment_type_other_error %} - + {% endif %}
diff --git a/bestilling/templates/tekst/form.html b/bestilling/templates/tekst/form.html index c7ac0a3..9073eb4 100644 --- a/bestilling/templates/tekst/form.html +++ b/bestilling/templates/tekst/form.html @@ -30,7 +30,7 @@ {% if errors.text_type_other_error %} - + {% endif %}
@@ -74,7 +74,7 @@
Intervju {% if errors.interview_error %} - + {% endif %}