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
@ -438,10 +439,10 @@ class PaymentOrderView(FormView):
email=this_user.get('email'), email=this_user.get('email'),
token=token, token=token,
customer_name=form.cleaned_data.get('name')) customer_name=form.cleaned_data.get('name'))
#try: # try:
# custom_user = CustomUser.objects.get( # custom_user = CustomUser.objects.get(
# email=this_user.get('email')) # email=this_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
# custom_user = CustomUser.register( # custom_user = CustomUser.register(
@ -453,7 +454,7 @@ class PaymentOrderView(FormView):
# username=custom_user.email, # username=custom_user.email,
# password=password) # password=password)
# login(request, new_user) # login(request, new_user)
#else: # else:
# # new user used the email of existing user, fail # # new user used the email of existing user, fail
# messages.error( # messages.error(
# self.request, # self.request,
@ -462,15 +463,15 @@ class PaymentOrderView(FormView):
# ) # )
# return HttpResponseRedirect( # return HttpResponseRedirect(
# reverse('datacenterlight:payment')) # reverse('datacenterlight:payment'))
#billing_address_data = form.cleaned_data # billing_address_data = form.cleaned_data
#billing_address_data.update({ # billing_address_data.update({
# 'user': custom_user.id # 'user': custom_user.id
#}) # })
#billing_address_user_form = UserBillingAddressForm( # billing_address_user_form = UserBillingAddressForm(
# instance=custom_user.billing_addresses.first(), # instance=custom_user.billing_addresses.first(),
# data=billing_address_data) # data=billing_address_data)
#billing_address_user_form.save() # billing_address_user_form.save()
#for k, v in form.cleaned_data.iteritems(): # for k, v in form.cleaned_data.iteritems():
request.session['billing_address_data'] = form.cleaned_data request.session['billing_address_data'] = form.cleaned_data
request.session['user'] = this_user request.session['user'] = this_user
# Get or create stripe customer # Get or create stripe customer
@ -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(