From 23bd0fa147b5234caeb369f6bd9aefc4cac1846c Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 15 Apr 2018 21:03:31 +0200 Subject: [PATCH] Pass context params to various landing templates --- datacenterlight/views.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 1089ceed..e2d28245 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -19,11 +19,11 @@ from hosting.models import HostingOrder from membership.models import CustomUser, StripeCustomer from opennebula_api.serializers import VMTemplateSerializer from utils.forms import BillingAddressForm, BillingAddressFormSignup -from utils.hosting_utils import get_vm_price +from utils.hosting_utils import get_vm_price, get_vm_price_with_vat from utils.stripe_utils import StripeUtils from utils.tasks import send_plain_email_task from .forms import ContactForm -from .models import VMTemplate +from .models import VMTemplate, VMPricing from .utils import get_cms_integration logger = logging.getLogger(__name__) @@ -93,7 +93,8 @@ class IndexView(CreateView): @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']: + for session_var in ['specs', 'user', 'billing_address_data', + 'pricing_name']: if session_var in request.session: del request.session[session_var] return HttpResponseRedirect(reverse('datacenterlight:cms_index')) @@ -106,13 +107,30 @@ class IndexView(CreateView): storage = request.POST.get('storage') storage_field = forms.IntegerField(validators=[self.validate_storage]) template_id = int(request.POST.get('config')) - vm_pricing_name = request.POST.get('pricing_name') + 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 referer_url = request.META['HTTP_REFERER'] + 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 HttpResponseRedirect(referer_url + "#order_form") + else: + vm_pricing_name = vm_pricing.name + try: cores = cores_field.clean(cores) except ValidationError as err: @@ -140,7 +158,7 @@ class IndexView(CreateView): ) return HttpResponseRedirect(referer_url + "#order_form") - amount_to_be_charged = get_vm_price( + amount_to_be_charged, vat = get_vm_price_with_vat( cpu=cores, memory=memory, disk_size=storage, @@ -150,7 +168,10 @@ class IndexView(CreateView): 'cpu': cores, 'memory': memory, 'disk_size': storage, - 'price': amount_to_be_charged + 'price': amount_to_be_charged, + 'vat': vat, + 'total_price': amount_to_be_charged + vat, + 'pricing_name': vm_pricing_name } request.session['specs'] = specs request.session['template'] = template_data @@ -224,7 +245,10 @@ class PaymentOrderView(FormView): 'site_url': reverse('datacenterlight:index'), 'login_form': HostingUserLoginForm(prefix='login_form'), 'billing_address_form': billing_address_form, - 'cms_integration': get_cms_integration('default') + 'cms_integration': get_cms_integration('default'), + 'vm_pricing': VMPricing.get_vm_pricing_by_name( + self.request.session['specs']['pricing_name'] + ) }) return context @@ -493,7 +517,7 @@ class OrderConfirmationView(DetailView): stripe_subscription_obj.id, card_details_dict) for session_var in ['specs', 'template', 'billing_address', 'billing_address_data', - 'token', 'customer']: + 'token', 'customer', 'pricing_name']: if session_var in request.session: del request.session[session_var]