<s> """<STR_LIT>""" <EOL> from decimal import Decimal <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . utils . translation import get_language , ugettext_lazy as _ <EOL> from shipping . modules . base import BaseShipper <EOL> from product . models import Product <EOL> import datetime <EOL> import logging <EOL> import operator <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class ProductShippingPriceException ( Exception ) : <EOL> def __init__ ( self , reason ) : <EOL> self . reason = reason <EOL> class Shipper ( BaseShipper ) : <EOL> def __init__ ( self , carrier ) : <EOL> self . id = carrier . key <EOL> self . carrier = carrier <EOL> super ( BaseShipper , self ) . __init__ ( ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % self . id <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . carrier . description <EOL> def cost ( self ) : <EOL> """<STR_LIT>""" <EOL> assert ( self . _calculated ) <EOL> total = Decimal ( "<STR_LIT>" ) <EOL> for cartitem in self . cart . cartitem_set . all ( ) : <EOL> p = cartitem . product <EOL> if p . is_shippable : <EOL> total += self . carrier . price ( p ) * cartitem . quantity <EOL> return total <EOL> def method ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . carrier . method <EOL> def expectedDelivery ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . carrier . delivery <EOL> def valid ( self , order = None ) : <EOL> """<STR_LIT>""" <EOL> if order : <EOL> try : <EOL> for item in order . orderitem_set . all ( ) : <EOL> p = item . product <EOL> price = self . carrier . price ( p ) <EOL> except ProductShippingPriceException : <EOL> return False <EOL> elif self . cart : <EOL> try : <EOL> price = self . cost ( ) <EOL> except ProductShippingPriceException : <EOL> return False <EOL> return True <EOL> class Carrier ( models . Model ) : <EOL> key = models . SlugField ( _ ( '<STR_LIT>' ) ) <EOL> ordering = models . IntegerField ( _ ( '<STR_LIT>' ) , default = <NUM_LIT:0> ) <EOL> active = models . BooleanField ( _ ( '<STR_LIT>' ) , default = True ) <EOL> def _find_translation ( self , language_code = None ) : <EOL> if not language_code : <EOL> language_code = get_language ( ) <EOL> c = self . translations . filter ( languagecode__exact = language_code ) <EOL> ct = c . count ( ) <EOL> if not c or ct == <NUM_LIT:0> : <EOL> pos = language_code . find ( '<STR_LIT:->' ) <EOL> if pos > - <NUM_LIT:1> : <EOL> short_code = language_code [ : pos ] <EOL> log . debug ( "<STR_LIT>" , short_code ) <EOL> c = self . translations . filter ( languagecode__exact = short_code ) <EOL> ct = c . count ( ) <EOL> if ct > <NUM_LIT:0> : <EOL> log . debug ( "<STR_LIT>" , short_code ) <EOL> if not c or ct == <NUM_LIT:0> : <EOL> c = self . translations . filter ( languagecode__istartswith = settings . LANGUAGE_CODE ) <EOL> ct = c . count ( ) <EOL> if not c or ct == <NUM_LIT:0> : <EOL> c = self . translations . all ( ) <EOL> ct = c . count ( ) <EOL> if ct > <NUM_LIT:0> : <EOL> trans = c [ <NUM_LIT:0> ] <EOL> else : <EOL> trans = None <EOL> return trans <EOL> def delivery ( self ) : <EOL> """<STR_LIT>""" <EOL> trans = self . _find_translation ( ) <EOL> if trans : <EOL> return trans . delivery <EOL> else : <EOL> return "<STR_LIT>" <EOL> delivery = property ( delivery ) <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> trans = self . _find_translation ( ) <EOL> if trans : <EOL> return trans . description <EOL> else : <EOL> return "<STR_LIT>" <EOL> description = property ( description ) <EOL> def method ( self ) : <EOL> """<STR_LIT>""" <EOL> trans = self . _find_translation ( ) <EOL> if trans : <EOL> return trans . method <EOL> else : <EOL> return "<STR_LIT>" <EOL> method = property ( method ) <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> trans = self . _find_translation ( ) <EOL> if trans : <EOL> return trans . name <EOL> else : <EOL> return "<STR_LIT>" <EOL> name = property ( name ) <EOL> def price ( self , product ) : <EOL> """<STR_LIT>""" <EOL> prices = product . smart_relation ( '<STR_LIT>' ) <EOL> q = prices . filter ( carrier = self ) <EOL> if q . count ( ) > <NUM_LIT:0> : <EOL> return q [ <NUM_LIT:0> ] . price <EOL> else : <EOL> log . debug ( "<STR_LIT>" , self , product . slug ) <EOL> raise ProductShippingPriceException ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . name , self . method ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , ) <EOL> class CarrierTranslation ( models . Model ) : <EOL> carrier = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> languagecode = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:10> , choices = settings . LANGUAGES , ) <EOL> name = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:50> , ) <EOL> description = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:200> ) <EOL> method = models . CharField ( _ ( '<STR_LIT>' ) , help_text = _ ( "<STR_LIT>" ) , max_length = <NUM_LIT:200> ) <EOL> delivery = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:200> ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> class ProductShippingPrice ( models . Model ) : <EOL> product = models . ForeignKey ( Product , related_name = "<STR_LIT>" ) <EOL> carrier = models . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> price = models . DecimalField ( _ ( "<STR_LIT>" ) , max_digits = <NUM_LIT:10> , decimal_places = <NUM_LIT:2> , ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . price ) <EOL> class Meta : <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> import config </s>
