From b30f41219cd98822ecbae870ab88b580d3ede916 Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Fri, 14 Jul 2017 11:34:40 +0300 Subject: [PATCH 1/2] Added backend cpu,ram,ssd fields validation --- .../datacenterlight/calculator_form.html | 30 +++++++++++++-- datacenterlight/views.py | 37 +++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/calculator_form.html b/datacenterlight/templates/datacenterlight/calculator_form.html index 09b59b6f..bebdc8aa 100644 --- a/datacenterlight/templates/datacenterlight/calculator_form.html +++ b/datacenterlight/templates/datacenterlight/calculator_form.html @@ -22,7 +22,15 @@ Core -
+
+ {% for message in messages %} + {% if 'cores' in message.tags %} + + {% endif %} + {% endfor %} +
@@ -31,7 +39,15 @@ GB RAM
-
+
+ {% for message in messages %} + {% if 'memory' in message.tags %} +
  • + {{ message|safe }} +
+ {% endif %} + {% endfor %} +
@@ -40,7 +56,15 @@ {% trans "GB Storage (SSD)" %}
-
+
+ {% for message in messages %} + {% if 'storage' in message.tags %} +
  • + {{ message|safe }} +
+ {% endif %} + {% endfor %} +
diff --git a/datacenterlight/views.py b/datacenterlight/views.py index ea7ca3ef..48119a85 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -194,6 +194,18 @@ class IndexView(CreateView): success_url = "/datacenterlight#requestform" success_message = "Thank you, we will contact you as soon as possible" + def validate_cores(self, value): + if (value > 48) or (value < 1): + raise ValidationError(_('Not a proper cores number')) + + def validate_memory(self, value): + if (value > 200) or (value < 2): + raise ValidationError(_('Not a proper ram number')) + + def validate_storage(self, value): + if (value > 2000) or (value < 10): + raise ValidationError(_('Not a proper storage number')) + @cache_control(no_cache=True, must_revalidate=True, no_store=True) def get(self, request, *args, **kwargs): for session_var in ['specs', 'user', 'billing_address_data']: @@ -217,8 +229,11 @@ class IndexView(CreateView): def post(self, request): cores = request.POST.get('cpu') + cores_field = forms.IntegerField(validators=[self.validate_cores]) memory = request.POST.get('ram') + memory_field = forms.IntegerField(validators=[self.validate_memory]) storage = request.POST.get('storage') + storage_field = forms.IntegerField(validators=[self.validate_storage]) price = request.POST.get('total') template_id = int(request.POST.get('config')) manager = OpenNebulaManager() @@ -229,6 +244,28 @@ class IndexView(CreateView): email = request.POST.get('email') name_field = forms.CharField() email_field = forms.EmailField() + + try: + cores = cores_field.clean(cores) + except ValidationError as err: + msg = '{} {}.'.format(cores, _('is not a proper cores number')) + messages.add_message(self.request, messages.ERROR, msg, extra_tags='cores') + return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") + + try: + memory = memory_field.clean(memory) + except ValidationError as err: + msg = '{} {}.'.format(memory, _('is not a proper memory number')) + messages.add_message(self.request, messages.ERROR, msg, extra_tags='memory') + return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") + + try: + storage = storage_field.clean(storage) + except ValidationError as err: + msg = '{} {}.'.format(storage, _('is not a proper storage number')) + messages.add_message(self.request, messages.ERROR, msg, extra_tags='storage') + return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") + try: name = name_field.clean(name) except ValidationError as err: From 9883a3f9080a3474995027f2e983d20cb7d8558f Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 26 Jul 2017 01:55:31 +0530 Subject: [PATCH 2/2] Refactored the validation error messages --- datacenterlight/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index c7720e39..fc0b8b2e 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -248,21 +248,21 @@ class IndexView(CreateView): try: cores = cores_field.clean(cores) except ValidationError as err: - msg = '{} {}.'.format(cores, _('is not a proper cores number')) + msg = '{} : {}.'.format(cores, str(err)) messages.add_message(self.request, messages.ERROR, msg, extra_tags='cores') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: memory = memory_field.clean(memory) except ValidationError as err: - msg = '{} {}.'.format(memory, _('is not a proper memory number')) + msg = '{} : {}.'.format(memory, str(err)) messages.add_message(self.request, messages.ERROR, msg, extra_tags='memory') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: storage = storage_field.clean(storage) except ValidationError as err: - msg = '{} {}.'.format(storage, _('is not a proper storage number')) + msg = '{} : {}.'.format(storage, str(err)) messages.add_message(self.request, messages.ERROR, msg, extra_tags='storage') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")