<s> import logging <EOL> from rest_framework import serializers <EOL> from oscarapi . utils import ( <EOL> overridable , <EOL> OscarModelSerializer , <EOL> OscarHyperlinkedModelSerializer , <EOL> DrillDownHyperlinkedIdentityField <EOL> ) <EOL> from oscarapi . serializers . fields import TaxIncludedDecimalField <EOL> from django . utils . translation import ugettext as _ <EOL> from oscar . core . loading import get_model <EOL> from decimal import Decimal <EOL> logger = logging . getLogger ( __name__ ) <EOL> Basket = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Line = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> LineAttribute = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> StockRecord = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Voucher = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class VoucherSerializer ( OscarModelSerializer ) : <EOL> class Meta : <EOL> model = Voucher <EOL> fields = overridable ( '<STR_LIT>' , default = ( <EOL> '<STR_LIT:name>' , '<STR_LIT:code>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) ) <EOL> class OfferDiscountSerializer ( serializers . Serializer ) : <EOL> description = serializers . CharField ( ) <EOL> name = serializers . CharField ( ) <EOL> amount = serializers . DecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , source = '<STR_LIT>' ) <EOL> class VoucherDiscountSerializer ( OfferDiscountSerializer ) : <EOL> voucher = VoucherSerializer ( required = False ) <EOL> class BasketSerializer ( serializers . HyperlinkedModelSerializer ) : <EOL> lines = serializers . HyperlinkedIdentityField ( view_name = '<STR_LIT>' ) <EOL> offer_discounts = OfferDiscountSerializer ( many = True , required = False ) <EOL> total_excl_tax = serializers . DecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , required = False ) <EOL> total_excl_tax_excl_discounts = serializers . DecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , required = False ) <EOL> total_incl_tax = TaxIncludedDecimalField ( <EOL> excl_tax_field = '<STR_LIT>' , decimal_places = <NUM_LIT:2> , <EOL> max_digits = <NUM_LIT:12> , required = False ) <EOL> total_incl_tax_excl_discounts = TaxIncludedDecimalField ( <EOL> excl_tax_field = '<STR_LIT>' , decimal_places = <NUM_LIT:2> , <EOL> max_digits = <NUM_LIT:12> , required = False ) <EOL> total_tax = TaxIncludedDecimalField ( <EOL> excl_tax_value = Decimal ( '<STR_LIT>' ) , decimal_places = <NUM_LIT:2> , <EOL> max_digits = <NUM_LIT:12> , required = False ) <EOL> currency = serializers . CharField ( required = False ) <EOL> voucher_discounts = VoucherDiscountSerializer ( many = True , required = False ) <EOL> class Meta : <EOL> model = Basket <EOL> fields = overridable ( '<STR_LIT>' , default = ( <EOL> '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT>' , <EOL> '<STR_LIT:url>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def get_validation_exclusions ( self , instance = None ) : <EOL> """<STR_LIT>""" <EOL> return super ( BasketSerializer , self ) . get_validation_exclusions ( <EOL> instance ) + [ '<STR_LIT>' ] <EOL> class LineAttributeSerializer ( OscarHyperlinkedModelSerializer ) : <EOL> class Meta : <EOL> model = LineAttribute <EOL> class BasketLineSerializer ( OscarHyperlinkedModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> url = DrillDownHyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , <EOL> extra_url_kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> attributes = LineAttributeSerializer ( <EOL> many = True , fields = ( '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT:value>' ) , required = False ) <EOL> price_excl_tax = serializers . DecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , <EOL> source = '<STR_LIT>' ) <EOL> price_incl_tax = TaxIncludedDecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , <EOL> excl_tax_field = '<STR_LIT>' , <EOL> source = '<STR_LIT>' ) <EOL> price_incl_tax_excl_discounts = TaxIncludedDecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , <EOL> excl_tax_field = '<STR_LIT>' , <EOL> source = '<STR_LIT>' ) <EOL> price_excl_tax_excl_discounts = serializers . DecimalField ( <EOL> decimal_places = <NUM_LIT:2> , max_digits = <NUM_LIT:12> , <EOL> source = '<STR_LIT>' ) <EOL> warning = serializers . CharField ( <EOL> read_only = True , required = False , source = '<STR_LIT>' ) <EOL> @ property <EOL> def basket_pk ( self ) : <EOL> return self . kwargs . get ( '<STR_LIT>' ) <EOL> class Meta : <EOL> model = Line <EOL> fields = overridable ( '<STR_LIT>' , default = ( <EOL> '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) ) <EOL> class LineSerializer ( serializers . HyperlinkedModelSerializer ) : <EOL> """<STR_LIT>""" <EOL> attributes = LineAttributeSerializer ( <EOL> many = True , <EOL> fields = ( '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT:value>' ) , <EOL> required = False , <EOL> read_only = True ) <EOL> class Meta : <EOL> model = Line <EOL> class StockRecordSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = StockRecord <EOL> class VoucherAddSerializer ( serializers . Serializer ) : <EOL> vouchercode = serializers . CharField ( max_length = <NUM_LIT> , required = True ) <EOL> def validate ( self , attrs ) : <EOL> request = self . context . get ( '<STR_LIT>' ) <EOL> try : <EOL> voucher = Voucher . objects . get ( code = attrs . get ( '<STR_LIT>' ) ) <EOL> if not voucher . is_active ( ) : <EOL> message = _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT:code>' : voucher . code <EOL> } <EOL> raise serializers . ValidationError ( message ) <EOL> is_available , message = voucher . is_available_to_user ( request . user ) <EOL> if not is_available : <EOL> raise serializers . ValidationError ( message ) <EOL> except Voucher . DoesNotExist : <EOL> raise serializers . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> self . instance = voucher <EOL> return attrs <EOL> def create ( self , validated_data ) : <EOL> return Voucher . objects . create ( ** validated_data ) </s>
