<s> from decimal import Decimal as D <EOL> from django import http <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from oscar . apps . checkout import views <EOL> from oscar . apps . payment import exceptions <EOL> from oscar . apps . payment . models import Source , SourceType <EOL> from oscar_accounts import exceptions as act_exceptions <EOL> from oscar_accounts import security <EOL> from oscar_accounts . checkout import forms , gateway <EOL> from oscar_accounts . checkout . allocation import Allocations <EOL> class PaymentDetailsView ( views . PaymentDetailsView ) : <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( PaymentDetailsView , self ) . get_context_data ( ** kwargs ) <EOL> ctx [ '<STR_LIT>' ] = security . is_blocked ( self . request ) <EOL> form = forms . ValidAccountForm ( self . request . user ) <EOL> ctx [ '<STR_LIT>' ] = form <EOL> if self . request . user . is_authenticated ( ) : <EOL> ctx [ '<STR_LIT>' ] = gateway . user_accounts ( self . request . user ) <EOL> allocations = self . get_account_allocations ( ) <EOL> ctx [ '<STR_LIT>' ] = allocations <EOL> ctx [ '<STR_LIT>' ] = ctx [ '<STR_LIT>' ] - allocations . total <EOL> return ctx <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> action = self . request . POST . get ( '<STR_LIT:action>' , None ) <EOL> if action == '<STR_LIT>' : <EOL> return self . select_account ( request ) <EOL> elif action == '<STR_LIT>' : <EOL> return self . add_allocation ( request ) <EOL> elif action == '<STR_LIT>' : <EOL> return self . remove_allocation ( request ) <EOL> return super ( PaymentDetailsView , self ) . post ( request , * args , ** kwargs ) <EOL> def handle_payment ( self , order_number , total , ** kwargs ) : <EOL> allocations = self . get_account_allocations ( ) <EOL> if allocations . total != total : <EOL> raise exceptions . UnableToTakePayment ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> gateway . redeem ( order_number , self . request . user , allocations ) <EOL> except act_exceptions . AccountException : <EOL> raise exceptions . UnableToTakePayment ( <EOL> "<STR_LIT>" ) <EOL> source_type , __ = SourceType . objects . get_or_create ( <EOL> name = "<STR_LIT>" ) <EOL> for code , amount in allocations . items ( ) : <EOL> source = Source ( <EOL> source_type = source_type , <EOL> amount_debited = amount , reference = code ) <EOL> self . add_payment_source ( source ) <EOL> self . add_payment_event ( "<STR_LIT>" , total ) <EOL> def select_account ( self , request ) : <EOL> ctx = self . get_context_data ( ) <EOL> if security . is_blocked ( request ) : <EOL> messages . error ( request , <EOL> "<STR_LIT>" ) <EOL> return http . HttpResponseRedirect ( <EOL> reverse ( '<STR_LIT>' ) ) <EOL> form = forms . ValidAccountForm ( self . request . user , <EOL> self . request . POST ) <EOL> ctx [ '<STR_LIT>' ] = form <EOL> if not form . is_valid ( ) : <EOL> security . record_failed_request ( self . request ) <EOL> return self . render_to_response ( ctx ) <EOL> security . record_successful_request ( self . request ) <EOL> ctx [ '<STR_LIT>' ] = forms . AllocationForm ( <EOL> form . account , self . request . basket , <EOL> ctx [ '<STR_LIT>' ] , <EOL> ctx [ '<STR_LIT>' ] , <EOL> self . get_account_allocations ( ) ) <EOL> return self . render_to_response ( ctx ) <EOL> def add_allocation ( self , request ) : <EOL> account_form = forms . ValidAccountForm ( request . user , <EOL> self . request . POST ) <EOL> if not account_form . is_valid ( ) : <EOL> messages . error ( request , <EOL> _ ( "<STR_LIT>" ) ) <EOL> return http . HttpResponseRedirect ( reverse ( <EOL> '<STR_LIT>' ) ) <EOL> ctx = self . get_context_data ( ) <EOL> allocation_form = forms . AllocationForm ( <EOL> account_form . account , self . request . basket , <EOL> ctx [ '<STR_LIT>' ] , <EOL> ctx [ '<STR_LIT>' ] , <EOL> self . get_account_allocations ( ) , <EOL> data = self . request . POST ) <EOL> if not allocation_form . is_valid ( ) : <EOL> ctx = self . get_context_data ( ) <EOL> ctx [ '<STR_LIT>' ] = allocation_form <EOL> ctx [ '<STR_LIT>' ] = account_form <EOL> return self . render_to_response ( ctx ) <EOL> self . store_allocation_in_session ( allocation_form ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return http . HttpResponseRedirect ( reverse ( <EOL> '<STR_LIT>' ) ) <EOL> def remove_allocation ( self , request ) : <EOL> code = None <EOL> for key in request . POST . keys ( ) : <EOL> if key . startswith ( '<STR_LIT>' ) : <EOL> code = key . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> allocations = self . get_account_allocations ( ) <EOL> if not allocations . contains ( code ) : <EOL> messages . error ( <EOL> request , _ ( "<STR_LIT>" ) % code ) <EOL> else : <EOL> allocations . remove ( code ) <EOL> self . set_account_allocations ( allocations ) <EOL> messages . success ( request , _ ( "<STR_LIT>" ) ) <EOL> return http . HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def store_allocation_in_session ( self , form ) : <EOL> allocations = self . get_account_allocations ( ) <EOL> allocations . add ( form . account . code , form . cleaned_data [ '<STR_LIT>' ] ) <EOL> self . set_account_allocations ( allocations ) <EOL> def get_account_allocations ( self ) : <EOL> return self . checkout_session . _get ( '<STR_LIT>' , '<STR_LIT>' , <EOL> Allocations ( ) ) <EOL> def set_account_allocations ( self , allocations ) : <EOL> return self . checkout_session . _set ( '<STR_LIT>' , <EOL> '<STR_LIT>' , allocations ) </s>
