<s> from decimal import Decimal as D <EOL> from django import http <EOL> from django . contrib import messages <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from oscar . core import prices <EOL> from oscar . core . loading import get_class , get_model <EOL> from . import exceptions <EOL> Repository = get_class ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> OrderTotalCalculator = get_class ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> CheckoutSessionData = get_class ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> ShippingAddress = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> BillingAddress = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> UserAddress = get_model ( '<STR_LIT:address>' , '<STR_LIT>' ) <EOL> class CheckoutSessionMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> pre_conditions = None <EOL> skip_conditions = None <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> self . checkout_session = CheckoutSessionData ( request ) <EOL> try : <EOL> self . check_pre_conditions ( request ) <EOL> except exceptions . FailedPreCondition as e : <EOL> for message in e . messages : <EOL> messages . warning ( request , message ) <EOL> return http . HttpResponseRedirect ( e . url ) <EOL> try : <EOL> self . check_skip_conditions ( request ) <EOL> except exceptions . PassedSkipCondition as e : <EOL> return http . HttpResponseRedirect ( e . url ) <EOL> return super ( CheckoutSessionMixin , self ) . dispatch ( <EOL> request , * args , ** kwargs ) <EOL> def check_pre_conditions ( self , request ) : <EOL> pre_conditions = self . get_pre_conditions ( request ) <EOL> for method_name in pre_conditions : <EOL> if not hasattr ( self , method_name ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" % ( <EOL> method_name ) ) <EOL> getattr ( self , method_name ) ( request ) <EOL> def get_pre_conditions ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if self . pre_conditions is None : <EOL> return [ ] <EOL> return self . pre_conditions <EOL> def check_skip_conditions ( self , request ) : <EOL> skip_conditions = self . get_skip_conditions ( request ) <EOL> for method_name in skip_conditions : <EOL> if not hasattr ( self , method_name ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" % ( <EOL> method_name ) ) <EOL> getattr ( self , method_name ) ( request ) <EOL> def get_skip_conditions ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if self . skip_conditions is None : <EOL> return [ ] <EOL> return self . skip_conditions <EOL> def check_basket_is_not_empty ( self , request ) : <EOL> if request . basket . is_empty : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( <EOL> "<STR_LIT>" ) <EOL> ) <EOL> def check_basket_is_valid ( self , request ) : <EOL> """<STR_LIT>""" <EOL> messages = [ ] <EOL> strategy = request . strategy <EOL> for line in request . basket . all_lines ( ) : <EOL> result = strategy . fetch_for_line ( line ) <EOL> is_permitted , reason = result . availability . is_purchase_permitted ( <EOL> line . quantity ) <EOL> if not is_permitted : <EOL> msg = _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % { <EOL> '<STR_LIT:title>' : line . product . get_title ( ) , <EOL> '<STR_LIT>' : reason } <EOL> messages . append ( msg ) <EOL> if messages : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> messages = messages <EOL> ) <EOL> def check_user_email_is_captured ( self , request ) : <EOL> if not request . user . is_authenticated ( ) and not self . checkout_session . get_guest_email ( ) : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( <EOL> "<STR_LIT>" ) <EOL> ) <EOL> def check_shipping_data_is_captured ( self , request ) : <EOL> if not request . basket . is_shipping_required ( ) : <EOL> if not self . checkout_session . is_shipping_method_set ( <EOL> self . request . basket ) : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> ) <EOL> return <EOL> self . check_a_valid_shipping_address_is_captured ( ) <EOL> self . check_a_valid_shipping_method_is_captured ( ) <EOL> def check_a_valid_shipping_address_is_captured ( self ) : <EOL> if not self . checkout_session . is_shipping_address_set ( ) : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> shipping_address = self . get_shipping_address ( <EOL> basket = self . request . basket ) <EOL> if not shipping_address : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> def check_a_valid_shipping_method_is_captured ( self ) : <EOL> if not self . checkout_session . is_shipping_method_set ( <EOL> self . request . basket ) : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( "<STR_LIT>" ) <EOL> ) <EOL> shipping_address = self . get_shipping_address ( <EOL> basket = self . request . basket ) <EOL> shipping_method = self . get_shipping_method ( <EOL> basket = self . request . basket , <EOL> shipping_address = shipping_address ) <EOL> if not shipping_method : <EOL> raise exceptions . FailedPreCondition ( <EOL> url = reverse ( '<STR_LIT>' ) , <EOL> message = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> ) <EOL> def check_payment_data_is_captured ( self , request ) : <EOL> pass <EOL> def skip_unless_basket_requires_shipping ( self , request ) : <EOL> if not request . basket . is_shipping_required ( ) : <EOL> raise exceptions . PassedSkipCondition ( <EOL> url = reverse ( '<STR_LIT>' ) <EOL> ) <EOL> def skip_unless_payment_is_required ( self , request ) : <EOL> shipping_address = self . get_shipping_address ( request . basket ) <EOL> shipping_method = self . get_shipping_method ( <EOL> request . basket , shipping_address ) <EOL> if shipping_method : <EOL> shipping_charge = shipping_method . calculate ( request . basket ) <EOL> else : <EOL> shipping_charge = prices . Price ( <EOL> currency = request . basket . currency , excl_tax = D ( '<STR_LIT>' ) , <EOL> tax = D ( '<STR_LIT>' ) <EOL> ) <EOL> total = self . get_order_totals ( request . basket , shipping_charge ) <EOL> if total . excl_tax == D ( '<STR_LIT>' ) : <EOL> raise exceptions . PassedSkipCondition ( <EOL> url = reverse ( '<STR_LIT>' ) <EOL> ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( CheckoutSessionMixin , self ) . get_context_data ( ) <EOL> ctx . update ( self . build_submission ( ** kwargs ) ) <EOL> ctx . update ( kwargs ) <EOL> ctx . update ( ctx [ '<STR_LIT>' ] ) <EOL> return ctx <EOL> def build_submission ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> basket = kwargs . get ( '<STR_LIT>' , self . request . basket ) <EOL> shipping_address = self . get_shipping_address ( basket ) <EOL> shipping_method = self . get_shipping_method ( <EOL> basket , shipping_address ) <EOL> billing_address = self . get_billing_address ( shipping_address ) <EOL> if not shipping_method : <EOL> total = shipping_charge = None <EOL> else : <EOL> shipping_charge = shipping_method . calculate ( basket ) <EOL> total = self . get_order_totals ( <EOL> basket , shipping_charge = shipping_charge ) <EOL> submission = { <EOL> '<STR_LIT:user>' : self . request . user , <EOL> '<STR_LIT>' : basket , <EOL> '<STR_LIT>' : shipping_address , <EOL> '<STR_LIT>' : shipping_method , <EOL> '<STR_LIT>' : shipping_charge , <EOL> '<STR_LIT>' : billing_address , <EOL> '<STR_LIT>' : total , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : { } } <EOL> if billing_address : <EOL> submission [ '<STR_LIT>' ] [ '<STR_LIT>' ] = billing_address <EOL> submission . update ( kwargs ) <EOL> if ( not submission [ '<STR_LIT:user>' ] . is_authenticated ( ) and <EOL> '<STR_LIT>' not in submission [ '<STR_LIT>' ] ) : <EOL> email = self . checkout_session . get_guest_email ( ) <EOL> submission [ '<STR_LIT>' ] [ '<STR_LIT>' ] = email <EOL> return submission <EOL> def get_shipping_address ( self , basket ) : <EOL> """<STR_LIT>""" <EOL> if not basket . is_shipping_required ( ) : <EOL> return None <EOL> addr_data = self . checkout_session . new_shipping_address_fields ( ) <EOL> if addr_data : <EOL> return ShippingAddress ( ** addr_data ) <EOL> addr_id = self . checkout_session . shipping_user_address_id ( ) <EOL> if addr_id : <EOL> try : <EOL> address = UserAddress . _default_manager . get ( pk = addr_id ) <EOL> except UserAddress . DoesNotExist : <EOL> return None <EOL> else : <EOL> shipping_addr = ShippingAddress ( ) <EOL> address . populate_alternative_model ( shipping_addr ) <EOL> return shipping_addr <EOL> def get_shipping_method ( self , basket , shipping_address = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> code = self . checkout_session . shipping_method_code ( basket ) <EOL> methods = Repository ( ) . get_shipping_methods ( <EOL> basket = basket , user = self . request . user , <EOL> shipping_addr = shipping_address , request = self . request ) <EOL> for method in methods : <EOL> if method . code == code : <EOL> return method <EOL> def get_billing_address ( self , shipping_address ) : <EOL> """<STR_LIT>""" <EOL> if not self . checkout_session . is_billing_address_set ( ) : <EOL> return None <EOL> if self . checkout_session . is_billing_address_same_as_shipping ( ) : <EOL> if shipping_address : <EOL> address = BillingAddress ( ) <EOL> shipping_address . populate_alternative_model ( address ) <EOL> return address <EOL> addr_data = self . checkout_session . new_billing_address_fields ( ) <EOL> if addr_data : <EOL> return BillingAddress ( ** addr_data ) <EOL> addr_id = self . checkout_session . billing_user_address_id ( ) <EOL> if addr_id : <EOL> try : <EOL> user_address = UserAddress . _default_manager . get ( pk = addr_id ) <EOL> except UserAddress . DoesNotExist : <EOL> return None <EOL> else : <EOL> billing_address = BillingAddress ( ) <EOL> user_address . populate_alternative_model ( billing_address ) <EOL> return billing_address <EOL> def get_order_totals ( self , basket , shipping_charge , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return OrderTotalCalculator ( self . request ) . calculate ( <EOL> basket , shipping_charge , ** kwargs ) </s>
