hosting: Updated OrdersHostingDetailView for stripe charging in this view
This commit is contained in:
parent
67ab4ef0a1
commit
0fc0b0630c
1 changed files with 67 additions and 42 deletions
109
hosting/views.py
109
hosting/views.py
|
@ -7,7 +7,8 @@ from django.shortcuts import render
|
||||||
from django.http import Http404
|
from django.http import Http404
|
||||||
from django.core.urlresolvers import reverse_lazy, reverse
|
from django.core.urlresolvers import reverse_lazy, reverse
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.views.generic import View, CreateView, FormView, ListView, DetailView, \
|
from django.views.generic import View, CreateView, FormView, ListView, \
|
||||||
|
DetailView, \
|
||||||
DeleteView, TemplateView, UpdateView
|
DeleteView, TemplateView, UpdateView
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
@ -24,11 +25,14 @@ from django.utils.safestring import mark_safe
|
||||||
|
|
||||||
from membership.models import CustomUser, StripeCustomer
|
from membership.models import CustomUser, StripeCustomer
|
||||||
from utils.stripe_utils import StripeUtils
|
from utils.stripe_utils import StripeUtils
|
||||||
from utils.forms import BillingAddressForm, PasswordResetRequestForm, UserBillingAddressForm
|
from utils.forms import BillingAddressForm, PasswordResetRequestForm, \
|
||||||
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin
|
UserBillingAddressForm
|
||||||
|
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, \
|
||||||
|
LoginViewMixin
|
||||||
from utils.mailer import BaseEmail
|
from utils.mailer import BaseEmail
|
||||||
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
|
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
|
||||||
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm, generate_ssh_key_name
|
from .forms import HostingUserSignupForm, HostingUserLoginForm, \
|
||||||
|
UserHostingKeyForm, generate_ssh_key_name
|
||||||
from .mixins import ProcessVMSelectionMixin
|
from .mixins import ProcessVMSelectionMixin
|
||||||
|
|
||||||
from opennebula_api.models import OpenNebulaManager
|
from opennebula_api.models import OpenNebulaManager
|
||||||
|
@ -296,7 +300,8 @@ class PasswordResetConfirmView(PasswordResetConfirmViewMixin):
|
||||||
|
|
||||||
form = self.form_class(request.POST)
|
form = self.form_class(request.POST)
|
||||||
|
|
||||||
if user is not None and default_token_generator.check_token(user, token):
|
if user is not None and default_token_generator.check_token(user,
|
||||||
|
token):
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
new_password = form.cleaned_data['new_password2']
|
new_password = form.cleaned_data['new_password2']
|
||||||
user.set_password(new_password)
|
user.set_password(new_password)
|
||||||
|
@ -397,7 +402,8 @@ class SSHKeyListView(LoginRequiredMixin, ListView):
|
||||||
def render_to_response(self, context, **response_kwargs):
|
def render_to_response(self, context, **response_kwargs):
|
||||||
if not self.queryset:
|
if not self.queryset:
|
||||||
return HttpResponseRedirect(reverse('hosting:choice_ssh_keys'))
|
return HttpResponseRedirect(reverse('hosting:choice_ssh_keys'))
|
||||||
return super(SSHKeyListView, self).render_to_response(context, **response_kwargs)
|
return super(SSHKeyListView, self).render_to_response(context,
|
||||||
|
**response_kwargs)
|
||||||
|
|
||||||
|
|
||||||
class SSHKeyChoiceView(LoginRequiredMixin, View):
|
class SSHKeyChoiceView(LoginRequiredMixin, View):
|
||||||
|
@ -548,23 +554,10 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
form = self.get_form()
|
form = self.get_form()
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
|
|
||||||
# Get billing address data
|
# Get billing address data
|
||||||
billing_address_data = form.cleaned_data
|
billing_address_data = form.cleaned_data
|
||||||
|
|
||||||
context = self.get_context_data()
|
|
||||||
|
|
||||||
template = request.session.get('template')
|
|
||||||
specs = request.session.get('specs')
|
|
||||||
|
|
||||||
vm_template_id = template.get('id', 1)
|
|
||||||
|
|
||||||
final_price = specs.get('price')
|
|
||||||
|
|
||||||
token = form.cleaned_data.get('token')
|
token = form.cleaned_data.get('token')
|
||||||
|
|
||||||
owner = self.request.user
|
owner = self.request.user
|
||||||
|
|
||||||
# Get or create stripe customer
|
# Get or create stripe customer
|
||||||
customer = StripeCustomer.get_or_create(email=owner.email,
|
customer = StripeCustomer.get_or_create(email=owner.email,
|
||||||
token=token)
|
token=token)
|
||||||
|
@ -579,12 +572,13 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
||||||
request.session['billing_address'] = billing_address.id
|
request.session['billing_address'] = billing_address.id
|
||||||
request.session['token'] = token
|
request.session['token'] = token
|
||||||
request.session['customer'] = customer.id
|
request.session['customer'] = customer.id
|
||||||
return HttpResponseRedirect(reverse('hosting:orders'))
|
return HttpResponseRedirect(reverse('hosting:order-confirmation'))
|
||||||
else:
|
else:
|
||||||
return self.form_invalid(form)
|
return self.form_invalid(form)
|
||||||
|
|
||||||
|
|
||||||
class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailView):
|
class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin,
|
||||||
|
DetailView):
|
||||||
template_name = "hosting/order_detail.html"
|
template_name = "hosting/order_detail.html"
|
||||||
context_object_name = "order"
|
context_object_name = "order"
|
||||||
login_url = reverse_lazy('hosting:login')
|
login_url = reverse_lazy('hosting:login')
|
||||||
|
@ -596,26 +590,52 @@ class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, Detai
|
||||||
context = super(DetailView, self).get_context_data(**kwargs)
|
context = super(DetailView, self).get_context_data(**kwargs)
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
owner = self.request.user
|
owner = self.request.user
|
||||||
manager = OpenNebulaManager(email=owner.email,
|
if 'specs' not in self.request.session or 'user' not in self.request.session:
|
||||||
password=owner.password)
|
return HttpResponseRedirect(
|
||||||
|
reverse('hosting:create_virtual_machine'))
|
||||||
|
if 'token' not in self.request.session:
|
||||||
|
return HttpResponseRedirect(reverse('hosting:payment'))
|
||||||
|
stripe_customer_id = self.request.session.get('customer')
|
||||||
|
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
||||||
|
stripe_utils = StripeUtils()
|
||||||
|
card_details = stripe_utils.get_card_details(customer.stripe_id,
|
||||||
|
self.request.session.get(
|
||||||
|
'token'))
|
||||||
|
if not card_details.get('response_object'):
|
||||||
|
msg = card_details.get('error')
|
||||||
|
messages.add_message(self.request, messages.ERROR, msg,
|
||||||
|
extra_tags='failed_payment')
|
||||||
|
return HttpResponseRedirect(
|
||||||
|
reverse('hosting:payment') + '#payment_error')
|
||||||
|
|
||||||
if self.request.GET.get('page', '') == 'payment':
|
if self.request.GET.get('page', '') == 'payment':
|
||||||
context['page_header_text'] = _('Confirm Order')
|
context['page_header_text'] = _('Confirm Order')
|
||||||
else:
|
else:
|
||||||
context['page_header_text'] = _('Invoice')
|
context['page_header_text'] = _('Invoice')
|
||||||
try:
|
|
||||||
vm = manager.get_vm(obj.vm_id)
|
if obj.vm_id:
|
||||||
context['vm'] = VirtualMachineSerializer(vm).data
|
try:
|
||||||
except WrongIdError:
|
manager = OpenNebulaManager(email=owner.email,
|
||||||
messages.error(self.request,
|
password=owner.password)
|
||||||
'The VM you are looking for is unavailable at the moment. \
|
vm = manager.get_vm(obj.vm_id)
|
||||||
Please contact Data Center Light support.'
|
context['vm'] = VirtualMachineSerializer(vm).data
|
||||||
)
|
except WrongIdError:
|
||||||
self.kwargs['error'] = 'WrongIdError'
|
messages.error(self.request,
|
||||||
context['error'] = 'WrongIdError'
|
'The VM you are looking for is unavailable at the moment. \
|
||||||
except ConnectionRefusedError:
|
Please contact Data Center Light support.'
|
||||||
messages.error(self.request,
|
)
|
||||||
'In order to create a VM, you need to create/upload your SSH KEY first.'
|
self.kwargs['error'] = 'WrongIdError'
|
||||||
)
|
context['error'] = 'WrongIdError'
|
||||||
|
except ConnectionRefusedError:
|
||||||
|
messages.error(self.request,
|
||||||
|
'In order to create a VM, you need to create/upload your SSH KEY first.'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
context['site_url'] = reverse('hosting:create_virtual_machine')
|
||||||
|
context['cc_last4'] = card_details.get('response_object').get(
|
||||||
|
'last4')
|
||||||
|
context['cc_brand'] = card_details.get('response_object').get(
|
||||||
|
'cc_brand')
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
@ -692,7 +712,8 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||||
configuration_options = HostingPlan.get_serialized_configs()
|
configuration_options = HostingPlan.get_serialized_configs()
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'templates': VirtualMachineTemplateSerializer(templates, many=True).data,
|
'templates': VirtualMachineTemplateSerializer(templates,
|
||||||
|
many=True).data,
|
||||||
'configuration_options': configuration_options,
|
'configuration_options': configuration_options,
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
|
@ -763,7 +784,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
serializer = VirtualMachineSerializer(vm)
|
serializer = VirtualMachineSerializer(vm)
|
||||||
context = {
|
context = {
|
||||||
'virtual_machine': serializer.data,
|
'virtual_machine': serializer.data,
|
||||||
'order': HostingOrder.objects.get(vm_id=serializer.data['vm_id'])
|
'order': HostingOrder.objects.get(
|
||||||
|
vm_id=serializer.data['vm_id'])
|
||||||
}
|
}
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
@ -794,7 +816,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
|
|
||||||
context = {
|
context = {
|
||||||
'vm': vm,
|
'vm': vm,
|
||||||
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host())
|
'base_url': "{0}://{1}".format(self.request.scheme,
|
||||||
|
self.request.get_host())
|
||||||
}
|
}
|
||||||
email_data = {
|
email_data = {
|
||||||
'subject': 'Virtual machine plan canceled',
|
'subject': 'Virtual machine plan canceled',
|
||||||
|
@ -814,7 +837,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
|
||||||
return HttpResponseRedirect(self.get_success_url())
|
return HttpResponseRedirect(self.get_success_url())
|
||||||
|
|
||||||
|
|
||||||
class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, ListView):
|
class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin,
|
||||||
|
ListView):
|
||||||
template_name = "hosting/bills.html"
|
template_name = "hosting/bills.html"
|
||||||
login_url = reverse_lazy('hosting:login')
|
login_url = reverse_lazy('hosting:login')
|
||||||
permission_required = ['view_hostingview']
|
permission_required = ['view_hostingview']
|
||||||
|
@ -824,7 +848,8 @@ class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, ListView)
|
||||||
ordering = '-id'
|
ordering = '-id'
|
||||||
|
|
||||||
|
|
||||||
class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailView):
|
class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin,
|
||||||
|
DetailView):
|
||||||
template_name = "hosting/bill_detail.html"
|
template_name = "hosting/bill_detail.html"
|
||||||
login_url = reverse_lazy('hosting:login')
|
login_url = reverse_lazy('hosting:login')
|
||||||
permission_required = ['view_hostingview']
|
permission_required = ['view_hostingview']
|
||||||
|
|
Loading…
Reference in a new issue