From 76efc35324b61c563c2de1e69834b6ccbe4ea99b Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 22 Sep 2018 06:44:37 +0200 Subject: [PATCH 01/17] Add enable_512mb_ram option in model/plugin --- datacenterlight/cms_models.py | 1 + datacenterlight/cms_plugins.py | 2 ++ datacenterlight/static/datacenterlight/js/main.js | 6 +++++- .../datacenterlight/includes/_calculator_form.html | 6 ++++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/datacenterlight/cms_models.py b/datacenterlight/cms_models.py index 62a7b312..407ff6de 100644 --- a/datacenterlight/cms_models.py +++ b/datacenterlight/cms_models.py @@ -350,3 +350,4 @@ class DCLCalculatorPluginModel(CMSPlugin): "in the backend to be automatically listed in this " "calculator instance." ) + enable_512mb_ram = models.BooleanField(default=False) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index 95a496d8..98b144b4 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -97,6 +97,8 @@ class DCLCalculatorPlugin(CMSPluginBase): context['templates'] = VMTemplate.objects.filter( vm_type=instance.vm_type ) + if instance.enable_512mb_ram: + context['enable_512mb_ram'] = True return context diff --git a/datacenterlight/static/datacenterlight/js/main.js b/datacenterlight/static/datacenterlight/js/main.js index 292e8c16..679d38ea 100644 --- a/datacenterlight/static/datacenterlight/js/main.js +++ b/datacenterlight/static/datacenterlight/js/main.js @@ -5,6 +5,10 @@ /* --------------------------------------------- Scripts initialization --------------------------------------------- */ + var minRam = 1; + if(window.enable_512mb){ + minRam = 0.5; + } var cardPricing = { 'cpu': { 'id': 'coreValue', @@ -16,7 +20,7 @@ 'ram': { 'id': 'ramValue', 'value': 2, - 'min': 1, + 'min': minRam, 'max': 200, 'interval': 1 }, diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index 72ca5a05..bc4299ed 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -9,6 +9,7 @@ window.ssdUnitPrice = {{vm_pricing.ssd_unit_price|default:0}}; window.hddUnitPrice = {{vm_pricing.hdd_unit_price|default:0}}; window.discountAmount = {{vm_pricing.discount_amount|default:0}}; + window.enable_512mb = {{vm_pricing.enable_512mb|default:false}}; {% endif %} @@ -54,8 +55,9 @@
- + {% firstof if enable_512mb_ram "0.5" endif "1" as myvar %} + GB RAM
From 12f139976d295138bacc3d107af5a7bedf0f3ea5 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 27 Sep 2018 09:06:39 +0200 Subject: [PATCH 02/17] Simplify logic to set min_ram in the calculator form --- datacenterlight/cms_plugins.py | 3 +-- .../datacenterlight/includes/_calculator_form.html | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index 98b144b4..404f3181 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -97,8 +97,7 @@ class DCLCalculatorPlugin(CMSPluginBase): context['templates'] = VMTemplate.objects.filter( vm_type=instance.vm_type ) - if instance.enable_512mb_ram: - context['enable_512mb_ram'] = True + context['min_ram'] = 0.5 if instance.enable_512mb_ram else 1 return context diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index bc4299ed..eed79e27 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -9,7 +9,7 @@ window.ssdUnitPrice = {{vm_pricing.ssd_unit_price|default:0}}; window.hddUnitPrice = {{vm_pricing.hdd_unit_price|default:0}}; window.discountAmount = {{vm_pricing.discount_amount|default:0}}; - window.enable_512mb = {{vm_pricing.enable_512mb|default:false}}; + window.minRam = {{min_ram}}; {% endif %} @@ -55,9 +55,8 @@
- {% firstof if enable_512mb_ram "0.5" endif "1" as myvar %} - + GB RAM
From 21084cdc9ff7486ad0af4646f9a3c7fbfd109e4e Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 27 Sep 2018 09:07:28 +0200 Subject: [PATCH 03/17] Initialize minRam js variable from what is passed from backend --- datacenterlight/static/datacenterlight/js/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/datacenterlight/static/datacenterlight/js/main.js b/datacenterlight/static/datacenterlight/js/main.js index 679d38ea..bc8efc1c 100644 --- a/datacenterlight/static/datacenterlight/js/main.js +++ b/datacenterlight/static/datacenterlight/js/main.js @@ -6,8 +6,8 @@ Scripts initialization --------------------------------------------- */ var minRam = 1; - if(window.enable_512mb){ - minRam = 0.5; + if(window.minRam){ + minRam = window.minRam; } var cardPricing = { 'cpu': { From e1ce017ec8bff9047b5cc256431f279f4b83236d Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 27 Sep 2018 22:48:23 +0200 Subject: [PATCH 04/17] Add migration --- ...lcalculatorpluginmodel_enable_512mb_ram.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py diff --git a/datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py b/datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py new file mode 100644 index 00000000..a29e76b5 --- /dev/null +++ b/datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2018-09-27 20:46 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('datacenterlight', '0025_dclnavbarpluginmodel_show_login_option'), + ] + + operations = [ + migrations.AddField( + model_name='dclcalculatorpluginmodel', + name='enable_512mb_ram', + field=models.BooleanField(default=False), + ), + ] From db20e3cbe74f38ba0cfc7cab6e0ec86bd00ceddd Mon Sep 17 00:00:00 2001 From: PCoder Date: Fri, 28 Sep 2018 08:07:08 +0200 Subject: [PATCH 05/17] Add plus minus ram handler for 512MB option --- datacenterlight/static/datacenterlight/js/main.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/datacenterlight/static/datacenterlight/js/main.js b/datacenterlight/static/datacenterlight/js/main.js index bc8efc1c..0999caa5 100644 --- a/datacenterlight/static/datacenterlight/js/main.js +++ b/datacenterlight/static/datacenterlight/js/main.js @@ -148,14 +148,22 @@ var data = $(this).data('minus'); if (cardPricing[data].value > cardPricing[data].min) { - cardPricing[data].value = Number(cardPricing[data].value) - cardPricing[data].interval; + if(data === 'ram' && cardPricing[data].value === 1){ + cardPricing[data].value = 0.5; + } else { + cardPricing[data].value = Number(cardPricing[data].value) - cardPricing[data].interval; + } } _fetchPricing(); }); $('.fa-plus-circle.right').click(function(event) { var data = $(this).data('plus'); if (cardPricing[data].value < cardPricing[data].max) { - cardPricing[data].value = Number(cardPricing[data].value) + cardPricing[data].interval; + if(data === 'ram' && cardPricing[data].value === 0.5){ + cardPricing[data].value = 1; + } else { + cardPricing[data].value = Number(cardPricing[data].value) + cardPricing[data].interval; + } } _fetchPricing(); }); From 24d719e4f1c80f313bbbb38b5bbce744a785756f Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 29 Sep 2018 07:37:23 +0200 Subject: [PATCH 06/17] Update migration --- ...m.py => 0027_dclcalculatorpluginmodel_enable_512mb_ram.py} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename datacenterlight/migrations/{0026_dclcalculatorpluginmodel_enable_512mb_ram.py => 0027_dclcalculatorpluginmodel_enable_512mb_ram.py} (73%) diff --git a/datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py b/datacenterlight/migrations/0027_dclcalculatorpluginmodel_enable_512mb_ram.py similarity index 73% rename from datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py rename to datacenterlight/migrations/0027_dclcalculatorpluginmodel_enable_512mb_ram.py index a29e76b5..bd639c9d 100644 --- a/datacenterlight/migrations/0026_dclcalculatorpluginmodel_enable_512mb_ram.py +++ b/datacenterlight/migrations/0027_dclcalculatorpluginmodel_enable_512mb_ram.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.9.4 on 2018-09-27 20:46 +# Generated by Django 1.9.4 on 2018-09-29 05:36 from __future__ import unicode_literals from django.db import migrations, models @@ -8,7 +8,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('datacenterlight', '0025_dclnavbarpluginmodel_show_login_option'), + ('datacenterlight', '0026_dclcalculatorpluginmodel_default_selected_template'), ] operations = [ From f85ef714abf217fedca458537ce79d3404fcc4a5 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 1 Oct 2018 07:50:15 +0200 Subject: [PATCH 07/17] Pass instance context Also POST plugin_id as a form parameter to check enable_512mb_ram case --- datacenterlight/cms_plugins.py | 4 +--- .../templates/datacenterlight/includes/_calculator_form.html | 5 +++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index af32e64f..ebd4fb8d 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -97,9 +97,7 @@ class DCLCalculatorPlugin(CMSPluginBase): context['templates'] = VMTemplate.objects.filter( vm_type=instance.vm_type ).order_by('name') - context['default_selected_template'] = ( - instance.default_selected_template - ) + context['instance'] = instance context['min_ram'] = 0.5 if instance.enable_512mb_ram else 1 return context diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index f7deb13f..0b73c83f 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -15,6 +15,7 @@
{% csrf_token %} +

{% trans "VM hosting" %}

@@ -93,11 +94,11 @@
- \ No newline at end of file + From c2dbbf04245cb1a869ac945cb6da967053f83bee Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 1 Oct 2018 07:52:17 +0200 Subject: [PATCH 08/17] Update views.py to include 512mb ram case RAM server side validation is as follows: - pid is a mandatory parameter for a valid RAM, otherwise a validation error is raised - check if enable_512mb_ram is enabled. In this case, validate if the input ram is either a whole number or 0.5 and in the range 0.5 <= value <= 200 - otherwise check ram is a whole number in the range 1 <= value <= 200 --- datacenterlight/views.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index be4e5700..5b2dc3e1 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -20,6 +20,7 @@ from utils.forms import BillingAddressForm, BillingAddressFormSignup from utils.hosting_utils import get_vm_price_with_vat from utils.stripe_utils import StripeUtils from utils.tasks import send_plain_email_task +from .cms_models import DCLCalculatorPluginModel from .forms import ContactForm from .models import VMTemplate, VMPricing from .utils import get_cms_integration, create_vm @@ -82,7 +83,29 @@ class IndexView(CreateView): raise ValidationError(_('Invalid number of cores')) def validate_memory(self, value): - if (value > 200) or (value < 1): + if 'pid' in self.request.POST: + try: + plugin = DCLCalculatorPluginModel.objects.get( + id=self.request.POST['pid'] + ) + except DCLCalculatorPluginModel.DoesNotExist as dne: + logger.error( + str(dne) + " plugin_id: " + self.request.POST['pid'] + ) + raise ValidationError(_('Invalid calculator properties')) + if plugin.enable_512mb_ram: + if value % 1 == 0 or value == 0.5: + logger.debug( + "Given ram {value} is either 0.5 or a" + " whole number".format(value=value) + ) + if (value > 200) or (value < 0.5): + raise ValidationError(_('Invalid RAM size')) + else: + raise ValidationError(_('Invalid RAM size')) + elif (value > 200) or (value < 1) or (value % 1 != 0): + raise ValidationError(_('Invalid RAM size')) + else: raise ValidationError(_('Invalid RAM size')) def validate_storage(self, value): @@ -101,7 +124,7 @@ class IndexView(CreateView): 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]) + memory_field = forms.FloatField(validators=[self.validate_memory]) storage = request.POST.get('storage') storage_field = forms.IntegerField(validators=[self.validate_storage]) template_id = int(request.POST.get('config')) From a93c9001099eff3d628274cce2e6b10488a8435e Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 1 Oct 2018 08:54:22 +0200 Subject: [PATCH 09/17] Pass 512 MB to memory parameter --- opennebula_api/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/opennebula_api/models.py b/opennebula_api/models.py index 0ce5e8a2..adc39bf0 100644 --- a/opennebula_api/models.py +++ b/opennebula_api/models.py @@ -249,8 +249,8 @@ class OpenNebulaManager(): vm_specs = vm_specs_formatter.format( vcpu=int(specs['cpu']), cpu=0.1 * int(specs['cpu']), - memory=1024 * int(specs['memory']), - + memory=(512 if specs['memory'] == 0.5 else + 1024 * int(specs['memory'])), ) vm_specs += """ fs @@ -269,8 +269,8 @@ class OpenNebulaManager(): vm_specs = vm_specs_formatter.format( vcpu=int(specs['cpu']), cpu=0.1 * int(specs['cpu']), - memory=1024 * int(specs['memory']), - + memory=(512 if specs['memory'] == 0.5 else + 1024 * int(specs['memory'])), ) vm_specs += """ fs From f2f95c85593027fdef587bad9a9f117c56ab67f3 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 1 Oct 2018 08:56:45 +0200 Subject: [PATCH 10/17] Update min attribute according to minimum ram in the plugin --- .../templates/datacenterlight/includes/_calculator_form.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index 0b73c83f..6306357e 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -10,6 +10,7 @@ window.hddUnitPrice = {{vm_pricing.hdd_unit_price|default:0}}; window.discountAmount = {{vm_pricing.discount_amount|default:0}}; window.minRam = {{min_ram}}; + window.minRamErr = '{% blocktrans with min_ram=min_ram %}Please enter a value in range {{min_ram}} - 200.{% endblocktrans %}'; {% endif %} @@ -56,7 +57,7 @@
- GB RAM From 26970ece923fb4f45a1db7629d62e143b96f9dd6 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 16 Oct 2018 08:55:05 +0200 Subject: [PATCH 11/17] Set default RAM step to 1 We later change this according to minRam set in the backend --- .../templates/datacenterlight/includes/_calculator_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index 6306357e..d25e9df2 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -58,7 +58,7 @@
+ data-error="{% blocktrans with min_ram=min_ram %}Please enter a value in range {{min_ram}} - 200.{% endblocktrans %}" required step="1"> GB RAM
From b7cc7b08ce951dc28282c1d29ceed5919d76bb96 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 16 Oct 2018 08:58:12 +0200 Subject: [PATCH 12/17] Update calculator min RAM input js validation --- .../static/datacenterlight/js/main.js | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/datacenterlight/static/datacenterlight/js/main.js b/datacenterlight/static/datacenterlight/js/main.js index 0999caa5..7a02b384 100644 --- a/datacenterlight/static/datacenterlight/js/main.js +++ b/datacenterlight/static/datacenterlight/js/main.js @@ -44,6 +44,7 @@ _initNavUrl(); _initPricing(); ajaxForms(); + $('#ramValue').data('old-value', $('#ramValue').val()); }); $(window).resize(function() { @@ -148,29 +149,65 @@ var data = $(this).data('minus'); if (cardPricing[data].value > cardPricing[data].min) { - if(data === 'ram' && cardPricing[data].value === 1){ + if(data === 'ram' && String(cardPricing[data].value) === "1" && minRam === 0.5){ cardPricing[data].value = 0.5; + $('#ramValue').val('0.5'); + $("#ramValue").attr('step', 0.5); } else { cardPricing[data].value = Number(cardPricing[data].value) - cardPricing[data].interval; } } _fetchPricing(); + $('#ramValue').data('old-value', $('#ramValue').val()); }); $('.fa-plus-circle.right').click(function(event) { var data = $(this).data('plus'); if (cardPricing[data].value < cardPricing[data].max) { - if(data === 'ram' && cardPricing[data].value === 0.5){ + if(data === 'ram' && String(cardPricing[data].value) === "0.5" && minRam === 0.5){ cardPricing[data].value = 1; + $('#ramValue').val('1'); + $("#ramValue").attr('step', 1); } else { cardPricing[data].value = Number(cardPricing[data].value) + cardPricing[data].interval; } } _fetchPricing(); + $('#ramValue').data('old-value', $('#ramValue').val()); }); $('.input-price').change(function() { var data = $(this).attr("name"); - cardPricing[data].value = $('input[name=' + data + ']').val(); + var input = $('input[name=' + data + ']'); + var inputValue = input.val(); + console.log(".input-price change " + data + " => " + inputValue); + if(data === 'ram' && window.minRam === 0.5) { + if (inputValue === '1'){ + //$('input[name=' + data + ']').attr('step', 0.5); + } + } + + if(data === 'ram') { + var ramInput = $('#ramValue'); + if ($('#ramValue').data('old-value') < $('#ramValue').val()) { + console.log("$('#ramValue').val() = " + $('#ramValue').val() + ", and minRam = " + minRam); + if($('#ramValue').val() === '1' && minRam === 0.5) { + console.log("Setting step = 1"); + $("#ramValue").attr('step', 1); + $('#ramValue').val('1'); + } + console.log('Alert up'); + } else { + if($('#ramValue').val() === '0' && minRam === 0.5) { + console.log("Setting step = 0.5"); + $("#ramValue").attr('step', 0.5); + $('#ramValue').val('0.5'); + } + console.log('Alert down'); + } + inputValue = $('#ramValue').val(); + $('#ramValue').data('old-value', $('#ramValue').val()); + } + cardPricing[data].value = inputValue; _fetchPricing(); }); } From 0e9f8ce906939ed404302e17fbefd7a2646541c1 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 16 Oct 2018 09:01:00 +0200 Subject: [PATCH 13/17] Cleanup unnecessary code --- datacenterlight/static/datacenterlight/js/main.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/datacenterlight/static/datacenterlight/js/main.js b/datacenterlight/static/datacenterlight/js/main.js index 7a02b384..65db1d6b 100644 --- a/datacenterlight/static/datacenterlight/js/main.js +++ b/datacenterlight/static/datacenterlight/js/main.js @@ -179,30 +179,19 @@ var data = $(this).attr("name"); var input = $('input[name=' + data + ']'); var inputValue = input.val(); - console.log(".input-price change " + data + " => " + inputValue); - if(data === 'ram' && window.minRam === 0.5) { - if (inputValue === '1'){ - //$('input[name=' + data + ']').attr('step', 0.5); - } - } if(data === 'ram') { var ramInput = $('#ramValue'); if ($('#ramValue').data('old-value') < $('#ramValue').val()) { - console.log("$('#ramValue').val() = " + $('#ramValue').val() + ", and minRam = " + minRam); if($('#ramValue').val() === '1' && minRam === 0.5) { - console.log("Setting step = 1"); $("#ramValue").attr('step', 1); $('#ramValue').val('1'); } - console.log('Alert up'); } else { if($('#ramValue').val() === '0' && minRam === 0.5) { - console.log("Setting step = 0.5"); $("#ramValue").attr('step', 0.5); $('#ramValue').val('0.5'); } - console.log('Alert down'); } inputValue = $('#ramValue').val(); $('#ramValue').data('old-value', $('#ramValue').val()); From 814163e58e980a5176d6dacfb64fde65f9c00451 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 17 Oct 2018 09:11:30 +0200 Subject: [PATCH 14/17] Add minRAM js code for hosting also --- hosting/static/hosting/js/initial.js | 48 +++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/hosting/static/hosting/js/initial.js b/hosting/static/hosting/js/initial.js index 9c1c226e..6b6d744d 100644 --- a/hosting/static/hosting/js/initial.js +++ b/hosting/static/hosting/js/initial.js @@ -157,6 +157,10 @@ $( document ).ready(function() { /* --------------------------------------------- Scripts initialization --------------------------------------------- */ + var minRam = 1; + if(window.minRam){ + minRam = window.minRam; + } var cardPricing = { 'cpu': { 'id': 'coreValue', @@ -168,7 +172,7 @@ $( document ).ready(function() { 'ram': { 'id': 'ramValue', 'value': 2, - 'min': 1, + 'min': minRam, 'max': 200, 'interval': 1 }, @@ -188,21 +192,54 @@ $( document ).ready(function() { var data = $(this).data('minus'); if (cardPricing[data].value > cardPricing[data].min) { - cardPricing[data].value = Number(cardPricing[data].value) - cardPricing[data].interval; + if(data === 'ram' && String(cardPricing[data].value) === "1" && minRam === 0.5){ + cardPricing[data].value = 0.5; + $('#ramValue').val('0.5'); + $("#ramValue").attr('step', 0.5); + } else { + cardPricing[data].value = Number(cardPricing[data].value) - cardPricing[data].interval; + } } _fetchPricing(); + $('#ramValue').data('old-value', $('#ramValue').val()); }); $('.fa-plus-circle.right').click(function(event) { var data = $(this).data('plus'); if (cardPricing[data].value < cardPricing[data].max) { - cardPricing[data].value = Number(cardPricing[data].value) + cardPricing[data].interval; + if(data === 'ram' && String(cardPricing[data].value) === "0.5" && minRam === 0.5){ + cardPricing[data].value = 1; + $('#ramValue').val('1'); + $("#ramValue").attr('step', 1); + } else { + cardPricing[data].value = Number(cardPricing[data].value) + cardPricing[data].interval; + } } _fetchPricing(); + $('#ramValue').data('old-value', $('#ramValue').val()); }); $('.input-price').change(function() { var data = $(this).attr("name"); - cardPricing[data].value = $('input[name=' + data + ']').val(); + var input = $('input[name=' + data + ']'); + var inputValue = input.val(); + + if(data === 'ram') { + var ramInput = $('#ramValue'); + if ($('#ramValue').data('old-value') < $('#ramValue').val()) { + if($('#ramValue').val() === '1' && minRam === 0.5) { + $("#ramValue").attr('step', 1); + $('#ramValue').val('1'); + } + } else { + if($('#ramValue').val() === '0' && minRam === 0.5) { + $("#ramValue").attr('step', 0.5); + $('#ramValue').val('0.5'); + } + } + inputValue = $('#ramValue').val(); + $('#ramValue').data('old-value', $('#ramValue').val()); + } + cardPricing[data].value = inputValue; _fetchPricing(); }); } @@ -236,4 +273,5 @@ $( document ).ready(function() { } _initPricing(); -}); \ No newline at end of file + $('#ramValue').data('old-value', $('#ramValue').val()); +}); From 3ebf932422f023c6f08321c0fcdab609f5a05a01 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 17 Oct 2018 09:29:06 +0200 Subject: [PATCH 15/17] Adjust hosting calculator form validation For 512 MB RAM offer --- hosting/views.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index c9b7ab08..c2eef1bd 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -32,6 +32,7 @@ from stored_messages.api import mark_read from stored_messages.models import Message from stored_messages.settings import stored_messages_settings +from datacenterlight.cms_models import DCLCalculatorPluginModel from datacenterlight.models import VMTemplate, VMPricing from datacenterlight.utils import create_vm, get_cms_integration from hosting.models import UserCardDetail @@ -1198,7 +1199,29 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View): raise ValidationError(_('Invalid number of cores')) def validate_memory(self, value): - if (value > 200) or (value < 1): + if 'pid' in self.request.POST: + try: + plugin = DCLCalculatorPluginModel.objects.get( + id=self.request.POST['pid'] + ) + except DCLCalculatorPluginModel.DoesNotExist as dne: + logger.error( + str(dne) + " plugin_id: " + self.request.POST['pid'] + ) + raise ValidationError(_('Invalid calculator properties')) + if plugin.enable_512mb_ram: + if value % 1 == 0 or value == 0.5: + logger.debug( + "Given ram {value} is either 0.5 or a" + " whole number".format(value=value) + ) + if (value > 200) or (value < 0.5): + raise ValidationError(_('Invalid RAM size')) + else: + raise ValidationError(_('Invalid RAM size')) + elif (value > 200) or (value < 1) or (value % 1 != 0): + raise ValidationError(_('Invalid RAM size')) + else: raise ValidationError(_('Invalid RAM size')) def validate_storage(self, value): @@ -1218,7 +1241,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View): 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]) + memory_field = forms.FloatField(validators=[self.validate_memory]) storage = request.POST.get('storage') storage_field = forms.IntegerField(validators=[self.validate_storage]) template_id = int(request.POST.get('config')) From 270c6101118c798a72accdf28585d097f2142ae9 Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 17 Oct 2018 09:56:39 +0200 Subject: [PATCH 16/17] Correct the min value of RAM Based on what is set by the admin in the backend --- .../templates/datacenterlight/includes/_calculator_form.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html index d25e9df2..f9896f17 100644 --- a/datacenterlight/templates/datacenterlight/includes/_calculator_form.html +++ b/datacenterlight/templates/datacenterlight/includes/_calculator_form.html @@ -57,7 +57,7 @@
- GB RAM From c36554b4d16ecb3fb30e2d2e86c2544954f37cbf Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 18 Oct 2018 06:58:18 +0200 Subject: [PATCH 17/17] Round total_price also to 2 decimal places --- datacenterlight/views.py | 2 +- hosting/views.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index af19b244..445ff7cf 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -197,7 +197,7 @@ class IndexView(CreateView): 'vat': vat, 'vat_percent': vat_percent, 'discount': discount, - 'total_price': price + vat - discount['amount'], + 'total_price': round(price + vat - discount['amount'], 2), 'pricing_name': vm_pricing_name } request.session['specs'] = specs diff --git a/hosting/views.py b/hosting/views.py index c2eef1bd..32de4e54 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1305,7 +1305,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View): 'price': price, 'vat': vat, 'vat_percent': vat_percent, - 'total_price': price + vat - discount['amount'], + 'total_price': round(price + vat - discount['amount'], 2), 'pricing_name': vm_pricing_name }