def add_allocation(self, request): # We have two forms to validate, first check the account form account_form = forms.ValidAccountForm(request.user, self.request.POST) if not account_form.is_valid(): # Only manipulation can get us here messages.error(request, _("An error occurred allocating from your account")) return http.HttpResponseRedirect( reverse('checkout:payment-details')) # Account is still valid, now check requested allocation ctx = self.get_context_data() allocation_form = forms.AllocationForm( account_form.account, self.request.basket, ctx['shipping_charge'].incl_tax if ctx['shipping_charge'].is_tax_known else ctx['shipping_charge'].excl_tax, ctx['order_total'].incl_tax if ctx['order_total'].is_tax_known else ctx['order_total'].excl_tax, self.get_account_allocations(), data=self.request.POST) if not allocation_form.is_valid(): ctx = self.get_context_data() ctx['allocation_form'] = allocation_form ctx['account_form'] = account_form return self.render_to_response(ctx) # Allocation is valid - record in session and reload page self.store_allocation_in_session(allocation_form) messages.success(request, _("Allocation recorded")) return http.HttpResponseRedirect(reverse('checkout:payment-details'))
def select_account(self, request): ctx = self.get_context_data() # Check for blocked users if security.is_blocked(request): messages.error(request, "You are currently blocked from using accounts") return http.HttpResponseRedirect( reverse('checkout:payment-deatils')) # If account form has been submitted, validate it and show the # allocation form if the account has non-zero balance form = forms.ValidAccountForm(self.request.user, self.request.POST) ctx['account_form'] = form if not form.is_valid(): security.record_failed_request(self.request) return self.render_to_response(ctx) security.record_successful_request(self.request) ctx['allocation_form'] = forms.AllocationForm( form.account, self.request.basket, ctx['shipping_charge'].incl_tax if ctx['shipping_charge'].is_tax_known else ctx['shipping_charge'].excl_tax, ctx['order_total'].incl_tax if ctx['order_total'].is_tax_known else ctx['order_total'].excl_tax, self.get_account_allocations()) return self.render_to_response(ctx)
def get_context_data(self, **kwargs): ctx = super(PaymentDetailsView, self).get_context_data(**kwargs) ctx['paysource'] = self.get_paymethod() ctx['paymethod'] = self.paymentsource_name[self.get_paymethod()] if self.get_paymethod() == 'oscar_account': # Add variable to indicate if the user is blocked from paying with # accounts. ctx['is_blocked'] = security.is_blocked(self.request) form = forms.ValidAccountForm(self.request.user) ctx['account_form'] = form # Add accounts that are linked to this user if self.request.user.is_authenticated(): ctx['user_accounts'] = gateway.user_accounts( self.request.user)[0] code = ctx['user_accounts'].code balance = ctx['user_accounts'].balance order_total = ctx['order_total'].incl_tax if ctx[ 'order_total'].is_tax_known else ctx['order_total'].excl_tax if balance < order_total: self.store_allocation_in_session({ 'code': code, 'amount': balance }) else: self.store_allocation_in_session({ 'code': code, 'amount': order_total }) ctx['allocation_form'] = forms.AllocationForm( ctx['user_accounts'], self.request.basket, ctx['shipping_charge'].incl_tax if ctx['shipping_charge'].is_tax_known else ctx['shipping_charge'].excl_tax, order_total, self.get_account_allocations()) return ctx