Fixed issues reported by flake

This commit is contained in:
M.Ravi 2017-07-01 15:53:25 +02:00
parent c8cf166571
commit 8fb4607bdd
2 changed files with 23 additions and 27 deletions

View file

@ -21,6 +21,7 @@ def change_lang(context, lang=None, *args, **kwargs):
return "%s" % url return "%s" % url
@register.filter('get_value_from_dict') @register.filter('get_value_from_dict')
def get_value_from_dict(dict_data, key): def get_value_from_dict(dict_data, key):
""" """
@ -28,5 +29,5 @@ def get_value_from_dict(dict_data, key):
""" """
if key: if key:
return dict_data.get(key) return dict_data.get(key)
else : else:
return "" return ""

View file

@ -14,12 +14,10 @@ from django.views.decorators.cache import cache_control
from django.conf import settings from django.conf import settings
from utils.forms import BillingAddressForm, UserBillingAddressForm from utils.forms import BillingAddressForm, UserBillingAddressForm
from utils.models import BillingAddress from utils.models import BillingAddress
from membership.models import StripeCustomer
from hosting.models import HostingOrder, HostingBill from hosting.models import HostingOrder, HostingBill
from utils.stripe_utils import StripeUtils from utils.stripe_utils import StripeUtils
from datetime import datetime from datetime import datetime
from membership.models import CustomUser, StripeCustomer from membership.models import CustomUser, StripeCustomer
from oca.pool import WrongIdError
from opennebula_api.models import OpenNebulaManager from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer
@ -35,15 +33,17 @@ class SuccessView(TemplateView):
if 'specs' not in request.session or 'user' not in request.session: if 'specs' not in request.session or 'user' not in request.session:
return HttpResponseRedirect(reverse('datacenterlight:index')) return HttpResponseRedirect(reverse('datacenterlight:index'))
elif 'token' not in request.session: elif 'token' not in request.session:
return HttpResponseRedirect(reverse('datacenterlight:payment')) return HttpResponseRedirect(reverse('datacenterlight:payment'))
elif 'order_confirmation' not in request.session: elif 'order_confirmation' not in request.session:
return HttpResponseRedirect(reverse('datacenterlight:order_confirmation')) return HttpResponseRedirect(reverse('datacenterlight:order_confirmation'))
else: else:
for session_var in ['specs', 'user', 'template', 'billing_address', 'billing_address_data', 'token', 'customer']: for session_var in ['specs', 'user', 'template', 'billing_address', 'billing_address_data',
'token', 'customer']:
if session_var in request.session: if session_var in request.session:
del request.session[session_var] del request.session[session_var]
return render(request, self.template_name) return render(request, self.template_name)
class PricingView(TemplateView): class PricingView(TemplateView):
template_name = "datacenterlight/pricing.html" template_name = "datacenterlight/pricing.html"
@ -74,7 +74,6 @@ class PricingView(TemplateView):
price = request.POST.get('total') price = request.POST.get('total')
template_id = int(request.POST.get('config')) template_id = int(request.POST.get('config'))
manager = OpenNebulaManager() manager = OpenNebulaManager()
template = manager.get_template(template_id) template = manager.get_template(template_id)
@ -199,7 +198,7 @@ class IndexView(CreateView):
del request.session['specs'] del request.session['specs']
if 'user' in request.session: if 'user' in request.session:
del request.session['user'] del request.session['user']
if 'billing_address_data' in request.session : if 'billing_address_data' in request.session:
del request.session['billing_address_data'] del request.session['billing_address_data']
try: try:
manager = OpenNebulaManager() manager = OpenNebulaManager()
@ -312,7 +311,7 @@ class IndexView(CreateView):
class PaymentOrderView(FormView): class PaymentOrderView(FormView):
template_name = 'hosting/payment.html' template_name = 'hosting/payment.html'
form_class = BillingAddressForm form_class = BillingAddressForm
def get_form_kwargs(self): def get_form_kwargs(self):
form_kwargs = super(PaymentOrderView, self).get_form_kwargs() form_kwargs = super(PaymentOrderView, self).get_form_kwargs()
billing_address_data = self.request.session.get('billing_address_data') billing_address_data = self.request.session.get('billing_address_data')
@ -327,7 +326,6 @@ class PaymentOrderView(FormView):
}) })
return form_kwargs return form_kwargs
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(PaymentOrderView, self).get_context_data(**kwargs) context = super(PaymentOrderView, self).get_context_data(**kwargs)
context.update({ context.update({
@ -349,9 +347,8 @@ class PaymentOrderView(FormView):
billing_address_data = form.cleaned_data billing_address_data = form.cleaned_data
token = form.cleaned_data.get('token') token = form.cleaned_data.get('token')
user = request.session.get('user') user = request.session.get('user')
try: try:
custom_user = CustomUser.objects.get(email=user.get('email')) CustomUser.objects.get(email=user.get('email'))
except CustomUser.DoesNotExist: except CustomUser.DoesNotExist:
password = CustomUser.get_random_password() password = CustomUser.get_random_password()
# Register the user, and do not send emails # Register the user, and do not send emails
@ -384,7 +381,7 @@ class OrderConfirmationView(DetailView):
payment_template_name = 'hosting/payment.html' payment_template_name = 'hosting/payment.html'
context_object_name = "order" context_object_name = "order"
model = HostingOrder model = HostingOrder
@cache_control(no_cache=True, must_revalidate=True, no_store=True) @cache_control(no_cache=True, must_revalidate=True, no_store=True)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
if 'specs' not in request.session or 'user' not in request.session: if 'specs' not in request.session or 'user' not in request.session:
@ -397,11 +394,11 @@ class OrderConfirmationView(DetailView):
card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token')) card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token'))
context = { context = {
'site_url': reverse('datacenterlight:index'), 'site_url': reverse('datacenterlight:index'),
'cc_last4' : card_details.get('response_object').get('last4'), 'cc_last4': card_details.get('response_object').get('last4'),
'cc_brand' : card_details.get('response_object').get('brand') 'cc_brand': card_details.get('response_object').get('brand')
} }
return render(request, self.template_name, context) return render(request, self.template_name, context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
template = request.session.get('template') template = request.session.get('template')
specs = request.session.get('specs') specs = request.session.get('specs')
@ -411,7 +408,6 @@ class OrderConfirmationView(DetailView):
billing_address_data = request.session.get('billing_address_data') billing_address_data = request.session.get('billing_address_data')
billing_address_id = request.session.get('billing_address') billing_address_id = request.session.get('billing_address')
billing_address = BillingAddress.objects.filter(id=billing_address_id).first() billing_address = BillingAddress.objects.filter(id=billing_address_id).first()
token = request.session.get('token')
vm_template_id = template.get('id', 1) vm_template_id = template.get('id', 1)
final_price = specs.get('price') final_price = specs.get('price')
@ -423,29 +419,28 @@ class OrderConfirmationView(DetailView):
# Check if the payment was approved # Check if the payment was approved
if not charge: if not charge:
context = {}
context.update({ context.update({
'paymentError': charge_response.get('error') 'paymentError': charge_response.get('error')
# TODO add logic in payment form to autofill data
#'form': form
}) })
return render(request, self.payment_template_name, context) return render(request, self.payment_template_name, context)
charge = charge_response.get('response_object') charge = charge_response.get('response_object')
# Create OpenNebulaManager # Create OpenNebulaManager
manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME, manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME,
password=settings.OPENNEBULA_PASSWORD) password=settings.OPENNEBULA_PASSWORD)
# Create a vm using oneadmin, also specify the name # Create a vm using oneadmin, also specify the name
vm_id = manager.create_vm( vm_id = manager.create_vm(
template_id=vm_template_id, template_id=vm_template_id,
specs=specs, specs=specs,
vm_name="{email}-{template_name}-{date}".format( vm_name="{email}-{template_name}-{date}".format(
email=user.get('email'), email=user.get('email'),
template_name=template.get('name'), template_name=template.get('name'),
date=int(datetime.now().strftime("%s"))) date=int(datetime.now().strftime("%s")))
) )
# Create a Hosting Order # Create a Hosting Order
order = HostingOrder.create( order = HostingOrder.create(
price=final_price, price=final_price,
@ -453,9 +448,9 @@ class OrderConfirmationView(DetailView):
customer=customer, customer=customer,
billing_address=billing_address billing_address=billing_address
) )
# Create a Hosting Bill # Create a Hosting Bill
bill = HostingBill.create( HostingBill.create(
customer=customer, billing_address=billing_address) customer=customer, billing_address=billing_address)
# Create Billing Address for User if he does not have one # Create Billing Address for User if he does not have one
@ -473,9 +468,9 @@ class OrderConfirmationView(DetailView):
# If the Stripe payment was successed, set order status approved # If the Stripe payment was successed, set order status approved
order.set_approved() order.set_approved()
vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data
context = { context = {
'name': user.get('name'), 'name': user.get('name'),
'email': user.get('email'), 'email': user.get('email'),