Compare commits

...

3 commits

Author SHA1 Message Date
PCoder
610dc6ecc1 Fix bugs 2019-12-24 17:15:38 +05:30
PCoder
ff07cab96c Move validation of calculator to plugin 2019-12-24 16:57:35 +05:30
PCoder
1b11f74a40 Fix spelling 2019-12-24 16:25:05 +05:30
3 changed files with 143 additions and 51 deletions
Changelog
datacenterlight
hosting

View file

@ -1,5 +1,5 @@
2.8.1: 2019-12-24
* [dcl cms navbar plugin]: Provide an option to show non transparent navar always
* [dcl cms navbar plugin]: Provide an option to show non transparent navbar always
2.8: 2019-12-20
* ldap_migration: Migrate django users to Ldap
Notes for deployment:

View file

@ -1,5 +1,14 @@
import logging
from cms.plugin_base import CMSPluginBase
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 (
DCLBannerItemPluginModel, DCLBannerListPluginModel, DCLContactPluginModel,
@ -8,8 +17,11 @@ from .cms_models import (
DCLSectionPluginModel, DCLNavbarPluginModel,
DCLSectionPromoPluginModel, DCLCalculatorPluginModel
)
from .models import VMTemplate
from .models import VMTemplate, VMPricing
from datacenterlight.utils import clear_all_session_vars
from opennebula_api.serializers import VMTemplateSerializer
logger = logging.getLogger(__name__)
@plugin_pool.register_plugin
@ -87,22 +99,136 @@ class DCLCalculatorPlugin(CMSPluginBase):
def render(self, context, instance, placeholder):
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 hasattr(instance, 'template'):
self.render_template = instance.template
request = context['request']
if request.method == "POST" and "order_form" in request.POST.keys():
cores = request.POST.get('cpu')
cores_field = forms.IntegerField(validators=[self.validate_cores])
memory = request.POST.get('ram')
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'))
pricing_name = 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(
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(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(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(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
}
request.session['specs'] = specs
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 request.POST:
try:
plugin = DCLCalculatorPluginModel.objects.get(
id=request.POST['pid']
)
except DCLCalculatorPluginModel.DoesNotExist as dne:
logger.error(
str(dne) + " plugin_id: " + 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
class DCLBannerListPlugin(CMSPluginBase):

View file

@ -1439,40 +1439,6 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
template_name = "hosting/create_virtual_machine.html"
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)
def get(self, request, *args, **kwargs):
context = {