def post(self, request, *args, **kwargs): ctxt = {} if 'login' in request.POST: # Check if login button is clicked login_form = LoginForm( request.POST) # Assign login form fields to variable if login_form.is_valid( ): # Return True if the form has no errors, or False otherwise try: user = User.authenticate_from_form(login_form) except User.DoesNotExist: messages.error(request, 'Incorrect login') if not user: # Check if credentials are valid and User object exist messages.error(request, 'Invalid login details given.') else: if not user.is_active: # Report error if user is not active messages.error(request, 'Your account is inactive.') else: auth_login(request, user) # Log in user user.set_session_expiration(request) next_url = request.GET.get('next') if next_url: return HttpResponseRedirect(next_url) hello = user.first_name if user.first_name else user.username messages.success(request, 'Welcome {}'.format(hello)) return redirect( self.page_to_redirect) # Go to index view elif 'register' in request.POST: registration_form = RegisterForm( request.POST) # Assign register form fields to variable if registration_form.is_valid( ): # Return True if the form has no errors, or False otherwise user = UserManager.create_from_form(registration_form) user.send_activation_link(request) messages.success( request, 'Please confirm your email address to complete the registration.' ) else: ctxt['registration_form'] = registration_form return render(request, self.template_name, self.get_context_data(**ctxt))
def post(self, request, *args, **kwargs): kwargs = super().get_context_data(request, **kwargs) # Stop proceeding if any of form is incorrectly completed if self.invalid_forms: return render(request, self.template_name, kwargs) else: if not self.has_all_billing_data: self.user.update_from_form(self.billing_form) elif self.create_account: self.user = UserManager.create_from_form(self.billing_form) self.cart.book() self.shipment = self.assign_new_shipment() if not self.shipment: messages.error(request, 'There was a problem creating the shipment. Administrator has been informed.') else: self.cart.update_after_shipment_creating(self.user, self.billing_form.cleaned_data['email']) self.send_checkout_email(request) # Save user and shipment after successful updating cart and sending checkout email if self.create_account: self.user.save() self.shipment.save() if request.user.is_authenticated: return redirect(reverse('summary', kwargs={'ref_code': self.cart.ref_code, 'oidb64': urlsafe_base64_encode( force_bytes(self.cart.id)), })) # Redirect newly created user or user without account to index page and display proper message return redirect(reverse('index'))