Handle login and billing address forms together in PaymentOrderView
This commit is contained in:
		
					parent
					
						
							
								02d144fbf9
							
						
					
				
			
			
				commit
				
					
						564f47cf8a
					
				
			
		
					 2 changed files with 56 additions and 26 deletions
				
			
		|  | @ -30,7 +30,7 @@ | ||||||
|                             {% csrf_token %} |                             {% csrf_token %} | ||||||
|                             {% bootstrap_field field show_label=False type='fields'%} |                             {% bootstrap_field field show_label=False type='fields'%} | ||||||
|                             {% endfor %} |                             {% endfor %} | ||||||
|                             <p class="red">{{login_form.non_field_errors|striptags}}</p> |                             <p class="text-danger">{{login_form.non_field_errors|striptags}}</p> | ||||||
|                             <input type='hidden' name='next' value='{{request.path}}'/> |                             <input type='hidden' name='next' value='{{request.path}}'/> | ||||||
|                             <div class="form-group text-right"> |                             <div class="form-group text-right"> | ||||||
|                                 <button type="submit" class="btn btn-wide btn-vm-contact" name="login_form">{% trans "LOGIN" %}</button> |                                 <button type="submit" class="btn btn-wide btn-vm-contact" name="login_form">{% trans "LOGIN" %}</button> | ||||||
|  | @ -59,7 +59,7 @@ | ||||||
|                     {% endfor %} |                     {% endfor %} | ||||||
|                     <form role="form" id="billing-form" method="post" action="" novalidate> |                     <form role="form" id="billing-form" method="post" action="" novalidate> | ||||||
|                         {% csrf_token %} |                         {% csrf_token %} | ||||||
|                         {% for field in form %} |                         {% for field in billing_address_form %} | ||||||
|                         {% bootstrap_field field show_label=False type='fields'%} |                         {% bootstrap_field field show_label=False type='fields'%} | ||||||
|                         {% endfor %} |                         {% endfor %} | ||||||
|                     </form> |                     </form> | ||||||
|  | @ -164,12 +164,13 @@ | ||||||
|                                             </p> |                                             </p> | ||||||
|                                         {% endif %} |                                         {% endif %} | ||||||
|                                     {% endfor %} |                                     {% endfor %} | ||||||
| 
 |                                     {% comment %} | ||||||
|                                     {% for error in form.non_field_errors %} |                                     {% for error in form.non_field_errors %} | ||||||
|                                         <p class="card-warning-content card-warning-error"> |                                         <p class="card-warning-content card-warning-error"> | ||||||
|                                             {{ error|escape }} |                                             {{ error|escape }} | ||||||
|                                         </p> |                                         </p> | ||||||
|                                     {% endfor %} |                                     {% endfor %} | ||||||
|  |                                     {% endcomment %} | ||||||
|                                 </div> |                                 </div> | ||||||
|                                 <div class="text-right"> |                                 <div class="text-right"> | ||||||
|                                     <button class="btn btn-vm-contact btn-wide" type="submit">{%trans "SUBMIT" %}</button> |                                     <button class="btn btn-vm-contact btn-wide" type="submit">{%trans "SUBMIT" %}</button> | ||||||
|  |  | ||||||
|  | @ -341,26 +341,25 @@ class PaymentOrderView(FormView): | ||||||
|         else: |         else: | ||||||
|             return BillingAddressFormSignup |             return BillingAddressFormSignup | ||||||
| 
 | 
 | ||||||
|     def get_form_kwargs(self): |  | ||||||
|         form_kwargs = super(PaymentOrderView, self).get_form_kwargs() |  | ||||||
|         # if user is signed in, get billing address |  | ||||||
|         if self.request.user.is_authenticated(): |  | ||||||
|             form_kwargs.update({ |  | ||||||
|                 'instance': self.request.user.billing_addresses.first() |  | ||||||
|             }) |  | ||||||
|         if 'billing_address_data' in self.request.session: |  | ||||||
|             billing_address_data = self.request.session['billing_address_data'] |  | ||||||
|             form_kwargs.update({ |  | ||||||
|                 'initial': billing_address_data |  | ||||||
|             }) |  | ||||||
|         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) | ||||||
|  |         if 'billing_address_data' in self.request.session: | ||||||
|  |             billing_address_data = self.request.session['billing_address_data'] | ||||||
|  |         else: | ||||||
|  |             billing_address_data = {} | ||||||
|         context.update({ |         context.update({ | ||||||
|             'stripe_key': settings.STRIPE_API_PUBLIC_KEY, |             'stripe_key': settings.STRIPE_API_PUBLIC_KEY, | ||||||
|             'site_url': reverse('datacenterlight:index'), |             'site_url': reverse('datacenterlight:index'), | ||||||
|             'login_form': HostingUserLoginForm() |             '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 | ||||||
|  |             BillingAddressFormSignup( | ||||||
|  |                 prefix='billing_address_form_signup', | ||||||
|  |                 initial=billing_address_data | ||||||
|  |             ) | ||||||
|         }) |         }) | ||||||
|         return context |         return context | ||||||
| 
 | 
 | ||||||
|  | @ -372,9 +371,32 @@ class PaymentOrderView(FormView): | ||||||
|         return self.render_to_response(self.get_context_data()) |         return self.render_to_response(self.get_context_data()) | ||||||
| 
 | 
 | ||||||
|     def post(self, request, *args, **kwargs): |     def post(self, request, *args, **kwargs): | ||||||
|         form = self.get_form() |         if 'login_form' in request.POST: | ||||||
|         if form.is_valid(): |             login_form = HostingUserLoginForm(data=request.POST, | ||||||
|             token = form.cleaned_data.get('token') |                                               prefix='login_form') | ||||||
|  |             if login_form.is_valid(): | ||||||
|  |                 email = login_form.cleaned_data.get('email') | ||||||
|  |                 password = login_form.cleaned_data.get('password') | ||||||
|  |                 auth_user = authenticate(email=email, password=password) | ||||||
|  |                 if auth_user: | ||||||
|  |                     login(self.request, auth_user) | ||||||
|  |                     return HttpResponseRedirect(self.get_success_url()) | ||||||
|  |             else: | ||||||
|  |                 context = self.get_context_data() | ||||||
|  |                 context['login_form'] = login_form | ||||||
|  |                 return self.render_to_response(context) | ||||||
|  |         if request.user.is_authenticated(): | ||||||
|  |             address_form = BillingAddressForm( | ||||||
|  |                 data=request.POST, | ||||||
|  |                 prefix='billing_address_form' | ||||||
|  |             ) | ||||||
|  |         else: | ||||||
|  |             address_form = BillingAddressFormSignup( | ||||||
|  |                 data=request.POST, | ||||||
|  |                 prefix='billing_address_form_signup' | ||||||
|  |             ) | ||||||
|  |         if address_form.is_valid(): | ||||||
|  |             token = address_form.cleaned_data.get('token') | ||||||
|             if request.user.is_authenticated(): |             if request.user.is_authenticated(): | ||||||
|                 this_user = { |                 this_user = { | ||||||
|                     'email': request.user.email, |                     'email': request.user.email, | ||||||
|  | @ -384,8 +406,8 @@ class PaymentOrderView(FormView): | ||||||
|                     email=this_user.get('email'), |                     email=this_user.get('email'), | ||||||
|                     token=token) |                     token=token) | ||||||
|             else: |             else: | ||||||
|                 user_email = form.cleaned_data.get('email') |                 user_email = address_form.cleaned_data.get('email') | ||||||
|                 user_name = form.cleaned_data.get('name') |                 user_name = address_form.cleaned_data.get('name') | ||||||
|                 this_user = { |                 this_user = { | ||||||
|                     'email': user_email, |                     'email': user_email, | ||||||
|                     'name': user_name |                     'name': user_name | ||||||
|  | @ -418,13 +440,18 @@ class PaymentOrderView(FormView): | ||||||
|                         token=token, |                         token=token, | ||||||
|                         customer_name=user_name) |                         customer_name=user_name) | ||||||
| 
 | 
 | ||||||
|             request.session['billing_address_data'] = form.cleaned_data |             request.session['billing_address_data'] = address_form.cleaned_data | ||||||
|             request.session['user'] = this_user |             request.session['user'] = this_user | ||||||
|             # Get or create stripe customer |             # Get or create stripe customer | ||||||
|             if not customer: |             if not customer: | ||||||
|                 form.add_error("__all__", "Invalid credit card") |                 address_form.add_error( | ||||||
|  |                     "__all__", "Invalid credit card" | ||||||
|  |                 ) | ||||||
|                 return self.render_to_response( |                 return self.render_to_response( | ||||||
|                     self.get_context_data(form=form)) |                     self.get_context_data( | ||||||
|  |                         billing_address_form=address_form | ||||||
|  |                     ) | ||||||
|  |                 ) | ||||||
|             request.session['token'] = token |             request.session['token'] = token | ||||||
|             if type(customer) is StripeCustomer: |             if type(customer) is StripeCustomer: | ||||||
|                 request.session['customer'] = customer.stripe_id |                 request.session['customer'] = customer.stripe_id | ||||||
|  | @ -433,7 +460,9 @@ class PaymentOrderView(FormView): | ||||||
|             return HttpResponseRedirect( |             return HttpResponseRedirect( | ||||||
|                 reverse('datacenterlight:order_confirmation')) |                 reverse('datacenterlight:order_confirmation')) | ||||||
|         else: |         else: | ||||||
|             return self.form_invalid(form) |             context = self.get_context_data() | ||||||
|  |             context['billing_address_form'] = address_form | ||||||
|  |             return self.render_to_response(context) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class OrderConfirmationView(DetailView): | class OrderConfirmationView(DetailView): | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue