From 5a2657e3573537a108c1b06cce2da0f8c6b09ccd Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 17:21:38 +0200 Subject: [PATCH] Create billing_address_form only for a logged user --- datacenterlight/views.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 0f117012..041a7710 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -341,21 +341,32 @@ class PaymentOrderView(FormView): else: return BillingAddressFormSignup - def get_context_data(self, **kwargs): context = super(PaymentOrderView, self).get_context_data(**kwargs) if 'billing_address_data' in self.request.session: billing_address_data = self.request.session['billing_address_data'] else: billing_address_data = {} + + billing_address_form = None + if self.request.user.is_authenticated(): + if billing_address_data: + billing_address_form = BillingAddressForm( + prefix='billing_address_form', + initial=billing_address_data + ) + else: + billing_address_form = BillingAddressForm( + prefix='billing_address_form', + instance=self.request.user.billing_addresses.first() + ) + context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'site_url': reverse('datacenterlight:index'), 'login_form': HostingUserLoginForm(prefix='login_form'), - 'billing_address_form': BillingAddressForm( - prefix='billing_address_form', - instance=self.request.user.billing_addresses.first() - ) if self.request.user.is_authenticated() else + 'billing_address_form': billing_address_form + if self.request.user.is_authenticated() else BillingAddressFormSignup( prefix='billing_address_form_signup', initial=billing_address_data @@ -380,7 +391,7 @@ class PaymentOrderView(FormView): auth_user = authenticate(email=email, password=password) if auth_user: login(self.request, auth_user) - return HttpResponseRedirect(self.get_success_url()) + return HttpResponseRedirect('') else: context = self.get_context_data() context['login_form'] = login_form