From b30f41219cd98822ecbae870ab88b580d3ede916 Mon Sep 17 00:00:00 2001
From: Siarhei Puhach <siarhei.puhach@itechart-group.com>
Date: Fri, 14 Jul 2017 11:34:40 +0300
Subject: [PATCH] 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 @@
             <span> Core</span>
             <i class="fa fa-plus-circle right" data-plus="cpu"  aria-hidden="true"></i>
             </div>
-            <div class="help-block with-errors"></div>
+            <div class="help-block with-errors">
+                {% for message in messages %}
+                    {% if 'cores' in message.tags %}
+                     <ul class="list-unstyled"><li>
+                        {{ message|safe }}
+                    </li></ul>
+                    {% endif %}
+                {% endfor %}
+            </div>
         </div>
         <div class="form-group">
             <div class="description input">
@@ -31,7 +39,15 @@
             <span> GB RAM</span>
             <i class="fa fa-plus-circle right" data-plus="ram"  aria-hidden="true"></i>
             </div>
-            <div class="help-block with-errors"></div>
+            <div class="help-block with-errors">
+                {% for message in messages %}
+                    {% if 'memory' in message.tags %}
+                     <ul class="list-unstyled"><li>
+                        {{ message|safe }}
+                    </li></ul>
+                    {% endif %}
+                {% endfor %}
+            </div>
         </div>
         <div class="form-group">
             <div class="description input">
@@ -40,7 +56,15 @@
             <span>{% trans "GB Storage (SSD)" %}</span>
             <i class="fa fa-plus-circle right" data-plus="storage"  aria-hidden="true"></i>
             </div>
-            <div class="help-block with-errors"></div>
+            <div class="help-block with-errors">
+                {% for message in messages %}
+                    {% if 'storage' in message.tags %}
+                     <ul class="list-unstyled"><li>
+                        {{ message|safe }}
+                    </li></ul>
+                    {% endif %}
+                {% endfor %}
+            </div>
         </div>
         <div class="description select-configuration input form-group justify-center">
             <label for="config">OS</label>
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: