Formatted code and added condition to create user only if an email does not exist

This commit is contained in:
M.Ravi 2017-09-22 11:45:21 +02:00
parent e5ad57d3f1
commit f67b69d490

View file

@ -32,6 +32,7 @@ from .models import BetaAccess, BetaAccessVMType, BetaAccessVM, VMTemplate
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class ContactUsView(FormView): class ContactUsView(FormView):
template_name = "datacenterlight/contact_form.html" template_name = "datacenterlight/contact_form.html"
form_class = ContactForm form_class = ContactForm
@ -479,7 +480,8 @@ class PaymentOrderView(FormView):
return self.render_to_response( return self.render_to_response(
self.get_context_data(form=form)) self.get_context_data(form=form))
request.session['token'] = token request.session['token'] = token
request.session['customer'] = customer.id if request.user.is_authenticated() else customer request.session[
'customer'] = customer.id if request.user.is_authenticated() else customer
return HttpResponseRedirect( return HttpResponseRedirect(
reverse('datacenterlight:order_confirmation')) reverse('datacenterlight:order_confirmation'))
else: else:
@ -500,7 +502,8 @@ class OrderConfirmationView(DetailView):
return HttpResponseRedirect(reverse('datacenterlight:payment')) return HttpResponseRedirect(reverse('datacenterlight:payment'))
stripe_customer_id = request.session.get('customer') stripe_customer_id = request.session.get('customer')
if request.user.is_authenticated(): if request.user.is_authenticated():
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() customer = StripeCustomer.objects.filter(
id=stripe_customer_id).first()
stripe_api_cus_id = customer.stripe_id stripe_api_cus_id = customer.stripe_id
else: else:
stripe_api_cus_id = stripe_customer_id stripe_api_cus_id = stripe_customer_id
@ -529,7 +532,8 @@ class OrderConfirmationView(DetailView):
user = request.session.get('user') user = request.session.get('user')
stripe_customer_id = request.session.get('customer') stripe_customer_id = request.session.get('customer')
if request.user.is_authenticated(): if request.user.is_authenticated():
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() customer = StripeCustomer.objects.filter(
id=stripe_customer_id).first()
stripe_api_cus_id = customer.stripe_id stripe_api_cus_id = customer.stripe_id
else: else:
stripe_api_cus_id = stripe_customer_id stripe_api_cus_id = stripe_customer_id
@ -579,13 +583,16 @@ class OrderConfirmationView(DetailView):
return HttpResponseRedirect( return HttpResponseRedirect(
reverse('datacenterlight:payment') + '#payment_error') reverse('datacenterlight:payment') + '#payment_error')
# Create user if the user is not logged in # Create user if the user is not logged in and if he is not already
if not request.user.is_authenticated(): # registered
if not request.user.is_authenticated() and CustomUser.objects.filter(
email=user.get('email')).exists():
try: try:
custom_user = CustomUser.objects.get( custom_user = CustomUser.objects.get(
email=user.get('email')) email=user.get('email'))
except CustomUser.DoesNotExist: except CustomUser.DoesNotExist:
logger.debug("Customer {} does not exist.".format(user.get('email'))) logger.debug(
"Customer {} does not exist.".format(user.get('email')))
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
custom_user = CustomUser.register( custom_user = CustomUser.register(