Move validation of calculator to plugin
This commit is contained in:
parent
1b11f74a40
commit
ff07cab96c
2 changed files with 141 additions and 49 deletions
|
|
@ -1,5 +1,14 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
from cms.plugin_base import CMSPluginBase
|
from cms.plugin_base import CMSPluginBase
|
||||||
from cms.plugin_pool import plugin_pool
|
from cms.plugin_pool import plugin_pool
|
||||||
|
from django import forms
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib import messages
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.shortcuts import redirect
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.core.urlresolvers import reverse
|
||||||
|
|
||||||
from .cms_models import (
|
from .cms_models import (
|
||||||
DCLBannerItemPluginModel, DCLBannerListPluginModel, DCLContactPluginModel,
|
DCLBannerItemPluginModel, DCLBannerListPluginModel, DCLContactPluginModel,
|
||||||
|
|
@ -8,8 +17,11 @@ from .cms_models import (
|
||||||
DCLSectionPluginModel, DCLNavbarPluginModel,
|
DCLSectionPluginModel, DCLNavbarPluginModel,
|
||||||
DCLSectionPromoPluginModel, DCLCalculatorPluginModel
|
DCLSectionPromoPluginModel, DCLCalculatorPluginModel
|
||||||
)
|
)
|
||||||
from .models import VMTemplate
|
from .models import VMTemplate, VMPricing
|
||||||
from datacenterlight.utils import clear_all_session_vars
|
from datacenterlight.utils import clear_all_session_vars
|
||||||
|
from opennebula_api.serializers import VMTemplateSerializer
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@plugin_pool.register_plugin
|
@plugin_pool.register_plugin
|
||||||
|
|
@ -87,22 +99,136 @@ class DCLCalculatorPlugin(CMSPluginBase):
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
clear_all_session_vars(context['request'])
|
clear_all_session_vars(context['request'])
|
||||||
context = super(DCLCalculatorPlugin, self).render(
|
|
||||||
context, instance, placeholder
|
|
||||||
)
|
|
||||||
ids = instance.vm_templates_to_show
|
|
||||||
if ids:
|
|
||||||
context['templates'] = VMTemplate.objects.filter(
|
|
||||||
vm_type=instance.vm_type
|
|
||||||
).filter(opennebula_vm_template_id__in=ids).order_by('name')
|
|
||||||
else:
|
|
||||||
context['templates'] = VMTemplate.objects.filter(
|
|
||||||
vm_type=instance.vm_type
|
|
||||||
).order_by('name')
|
|
||||||
context['instance'] = instance
|
|
||||||
context['min_ram'] = 0.5 if instance.enable_512mb_ram else 1
|
|
||||||
return context
|
|
||||||
|
|
||||||
|
if instance and instance.template:
|
||||||
|
self.render_template = instance.template
|
||||||
|
|
||||||
|
if self.request.method == "POST" and "order_form" in self.request.POST.keys():
|
||||||
|
cores = self.request.POST.get('cpu')
|
||||||
|
cores_field = forms.IntegerField(validators=[self.validate_cores])
|
||||||
|
memory = self.request.POST.get('ram')
|
||||||
|
memory_field = forms.FloatField(validators=[self.validate_memory])
|
||||||
|
storage = self.request.POST.get('storage')
|
||||||
|
storage_field = forms.IntegerField(
|
||||||
|
validators=[self.validate_storage])
|
||||||
|
template_id = int(self.request.POST.get('config'))
|
||||||
|
pricing_name = self.request.POST.get('pricing_name')
|
||||||
|
vm_pricing = VMPricing.get_vm_pricing_by_name(pricing_name)
|
||||||
|
template = VMTemplate.objects.filter(
|
||||||
|
opennebula_vm_template_id=template_id).first()
|
||||||
|
template_data = VMTemplateSerializer(template).data
|
||||||
|
|
||||||
|
if vm_pricing is None:
|
||||||
|
vm_pricing_name_msg = _(
|
||||||
|
"Incorrect pricing name. Please contact support"
|
||||||
|
"{support_email}".format(
|
||||||
|
support_email=settings.DCL_SUPPORT_FROM_ADDRESS
|
||||||
|
)
|
||||||
|
)
|
||||||
|
messages.add_message(
|
||||||
|
self.request, messages.ERROR, vm_pricing_name_msg,
|
||||||
|
extra_tags='pricing'
|
||||||
|
)
|
||||||
|
return redirect(self)
|
||||||
|
else:
|
||||||
|
vm_pricing_name = vm_pricing.name
|
||||||
|
|
||||||
|
try:
|
||||||
|
cores = cores_field.clean(cores)
|
||||||
|
except ValidationError as err:
|
||||||
|
msg = '{} : {}.'.format(cores, str(err))
|
||||||
|
messages.add_message(self.request, messages.ERROR, msg,
|
||||||
|
extra_tags='cores')
|
||||||
|
return redirect(self)
|
||||||
|
|
||||||
|
try:
|
||||||
|
memory = memory_field.clean(memory)
|
||||||
|
except ValidationError as err:
|
||||||
|
msg = '{} : {}.'.format(memory, str(err))
|
||||||
|
messages.add_message(self.request, messages.ERROR, msg,
|
||||||
|
extra_tags='memory')
|
||||||
|
return redirect(self)
|
||||||
|
|
||||||
|
try:
|
||||||
|
storage = storage_field.clean(storage)
|
||||||
|
except ValidationError as err:
|
||||||
|
msg = '{} : {}.'.format(storage, str(err))
|
||||||
|
messages.add_message(self.request, messages.ERROR, msg,
|
||||||
|
extra_tags='storage')
|
||||||
|
return redirect(self)
|
||||||
|
|
||||||
|
price, vat, vat_percent, discount = get_vm_price_with_vat(
|
||||||
|
cpu=cores,
|
||||||
|
memory=memory,
|
||||||
|
ssd_size=storage,
|
||||||
|
pricing_name=vm_pricing_name
|
||||||
|
)
|
||||||
|
|
||||||
|
specs = {
|
||||||
|
'cpu': cores,
|
||||||
|
'memory': memory,
|
||||||
|
'disk_size': storage,
|
||||||
|
'discount': discount,
|
||||||
|
'price': price,
|
||||||
|
'vat': vat,
|
||||||
|
'vat_percent': vat_percent,
|
||||||
|
'total_price': round(price + vat - discount['amount'], 2),
|
||||||
|
'pricing_name': vm_pricing_name
|
||||||
|
}
|
||||||
|
|
||||||
|
self.request.session['specs'] = specs
|
||||||
|
self.request.session['template'] = template_data
|
||||||
|
return redirect(reverse('datacenterlight:payment'))
|
||||||
|
else:
|
||||||
|
context = super(DCLCalculatorPlugin, self).render(
|
||||||
|
context, instance, placeholder
|
||||||
|
)
|
||||||
|
ids = instance.vm_templates_to_show
|
||||||
|
if ids:
|
||||||
|
context['templates'] = VMTemplate.objects.filter(
|
||||||
|
vm_type=instance.vm_type
|
||||||
|
).filter(opennebula_vm_template_id__in=ids).order_by('name')
|
||||||
|
else:
|
||||||
|
context['templates'] = VMTemplate.objects.filter(
|
||||||
|
vm_type=instance.vm_type
|
||||||
|
).order_by('name')
|
||||||
|
context['instance'] = instance
|
||||||
|
context['min_ram'] = 0.5 if instance.enable_512mb_ram else 1
|
||||||
|
return context
|
||||||
|
|
||||||
|
def validate_cores(self, value):
|
||||||
|
if (value > 48) or (value < 1):
|
||||||
|
raise ValidationError(_('Invalid number of cores'))
|
||||||
|
|
||||||
|
def validate_memory(self, value):
|
||||||
|
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):
|
||||||
|
if (value > 2000) or (value < 10):
|
||||||
|
raise ValidationError(_('Invalid storage size'))
|
||||||
|
|
||||||
@plugin_pool.register_plugin
|
@plugin_pool.register_plugin
|
||||||
class DCLBannerListPlugin(CMSPluginBase):
|
class DCLBannerListPlugin(CMSPluginBase):
|
||||||
|
|
|
||||||
|
|
@ -1439,40 +1439,6 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||||
template_name = "hosting/create_virtual_machine.html"
|
template_name = "hosting/create_virtual_machine.html"
|
||||||
login_url = reverse_lazy('hosting:login')
|
login_url = reverse_lazy('hosting:login')
|
||||||
|
|
||||||
def validate_cores(self, value):
|
|
||||||
if (value > 48) or (value < 1):
|
|
||||||
raise ValidationError(_('Invalid number of cores'))
|
|
||||||
|
|
||||||
def validate_memory(self, value):
|
|
||||||
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):
|
|
||||||
if (value > 2000) or (value < 10):
|
|
||||||
raise ValidationError(_('Invalid storage size'))
|
|
||||||
|
|
||||||
@method_decorator(decorators)
|
@method_decorator(decorators)
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
context = {
|
context = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue