<s> '''<STR_LIT>''' <EOL> from decimal import Decimal <EOL> from django . core . cache import cache <EOL> from django . template import loader , Context <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext as _ <EOL> from livesettings import config_get_group , config_value <EOL> from shipping . modules . base import BaseShipper <EOL> import datetime <EOL> import logging <EOL> import urllib2 <EOL> try : <EOL> from xml . etree . ElementTree import fromstring , tostring <EOL> except ImportError : <EOL> from elementtree . ElementTree import fromstring , tostring <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class Shipper ( BaseShipper ) : <EOL> def __init__ ( self , cart = None , contact = None , service_type = None ) : <EOL> self . _calculated = False <EOL> self . cart = cart <EOL> self . contact = contact <EOL> if service_type : <EOL> self . service_type_code = service_type [ <NUM_LIT:0> ] <EOL> self . service_type_text = service_type [ <NUM_LIT:1> ] <EOL> else : <EOL> self . service_type_code = '<STR_LIT>' <EOL> self . service_type_text = '<STR_LIT>' <EOL> self . id = u'<STR_LIT>' % ( self . service_type_code ) <EOL> def __str__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT>' <EOL> def description ( self ) : <EOL> '''<STR_LIT>''' <EOL> return _ ( '<STR_LIT>' % self . service_type_text ) <EOL> def cost ( self ) : <EOL> '''<STR_LIT>''' <EOL> assert ( self . _calculated ) <EOL> return ( Decimal ( self . charges ) ) <EOL> def method ( self ) : <EOL> '''<STR_LIT>''' <EOL> return _ ( '<STR_LIT>' ) <EOL> def expectedDelivery ( self ) : <EOL> '''<STR_LIT>''' <EOL> if str ( self . delivery_days ) < > '<STR_LIT:1>' : <EOL> return _ ( '<STR_LIT>' % self . delivery_days ) <EOL> else : <EOL> return _ ( '<STR_LIT>' % self . delivery_days ) <EOL> def valid ( self , order = None ) : <EOL> '''<STR_LIT>''' <EOL> return self . is_valid <EOL> def _process_request ( self , connection , request ) : <EOL> '''<STR_LIT>''' <EOL> conn = urllib2 . Request ( url = connection , data = request . encode ( "<STR_LIT:utf-8>" ) ) <EOL> f = urllib2 . urlopen ( conn ) <EOL> all_results = f . read ( ) <EOL> self . raw = all_results <EOL> return ( fromstring ( all_results ) ) <EOL> def calculate ( self , cart , contact ) : <EOL> '''<STR_LIT>''' <EOL> log . debug ( "<STR_LIT>" ) <EOL> from satchmo_store . shop . models import Config <EOL> settings = config_get_group ( '<STR_LIT>' ) <EOL> verbose = settings . VERBOSE_LOG . value <EOL> self . delivery_days = _ ( '<STR_LIT>' ) <EOL> shop_details = Config . objects . get_current ( ) <EOL> self . packaging = '<STR_LIT>' <EOL> self . is_valid = False <EOL> error = False <EOL> self . charges = <NUM_LIT:0> <EOL> if not settings . CPCID . value : <EOL> log . warn ( "<STR_LIT>" ) <EOL> return <EOL> if settings . LIVE . value : <EOL> connection = settings . CONNECTION . value <EOL> else : <EOL> connection = settings . CONNECTION_TEST . value <EOL> configuration = { <EOL> '<STR_LIT>' : settings . CPCID . value , <EOL> '<STR_LIT>' : settings . TURN_AROUND_TIME . value , <EOL> '<STR_LIT>' : self . packaging , <EOL> '<STR_LIT>' : self . service_type_code , <EOL> '<STR_LIT>' : shop_details , <EOL> } <EOL> c = Context ( { <EOL> '<STR_LIT>' : configuration , <EOL> '<STR_LIT>' : cart , <EOL> '<STR_LIT>' : contact <EOL> } ) <EOL> t = loader . get_template ( '<STR_LIT>' ) <EOL> request = t . render ( c ) <EOL> self . is_valid = False <EOL> cache_key_response = "<STR_LIT>" % int ( cart . id ) <EOL> cache_key_request = "<STR_LIT>" % int ( cart . id ) <EOL> last_request = cache . get ( cache_key_request ) <EOL> tree = cache . get ( cache_key_response ) <EOL> if ( last_request != request ) or tree is None : <EOL> self . verbose_log ( "<STR_LIT>" , cache_key_request , request ) <EOL> cache . set ( cache_key_request , request , <NUM_LIT> ) <EOL> tree = self . _process_request ( connection , request ) <EOL> self . verbose_log ( "<STR_LIT>" , cache_key_response , self . raw ) <EOL> needs_cache = True <EOL> else : <EOL> needs_cache = False <EOL> try : <EOL> status_code = tree . getiterator ( '<STR_LIT>' ) <EOL> status_val = status_code [ <NUM_LIT:0> ] . text <EOL> self . verbose_log ( "<STR_LIT>" , int ( cart . id ) , status_val ) <EOL> except AttributeError : <EOL> status_val = "<STR_LIT>" <EOL> if status_val == '<STR_LIT:1>' : <EOL> self . is_valid = False <EOL> self . _calculated = False <EOL> all_rates = tree . getiterator ( '<STR_LIT>' ) <EOL> for rate in all_rates : <EOL> self . verbose_log ( "<STR_LIT>" , rate . attrib [ '<STR_LIT:id>' ] ) <EOL> if self . service_type_code == rate . attrib [ '<STR_LIT:id>' ] : <EOL> self . charges = Decimal ( rate . find ( '<STR_LIT>' ) . text ) <EOL> delivery_date = rate . find ( '<STR_LIT>' ) . text <EOL> shipping_date = rate . find ( '<STR_LIT>' ) . text <EOL> self . delivery_days = datetime . date ( <EOL> int ( delivery_date [ : <NUM_LIT:4> ] ) , <EOL> int ( delivery_date [ <NUM_LIT:5> : <NUM_LIT:7> ] ) , <EOL> int ( delivery_date [ <NUM_LIT:8> : ] ) ) - datetime . date ( <EOL> int ( shipping_date [ : <NUM_LIT:4> ] ) , <EOL> int ( shipping_date [ <NUM_LIT:5> : <NUM_LIT:7> ] ) , <EOL> int ( shipping_date [ <NUM_LIT:8> : ] ) ) <EOL> self . delivery_days = self . delivery_days . days <EOL> self . is_valid = True <EOL> self . _calculated = True <EOL> if not self . is_valid : <EOL> self . verbose_log ( "<STR_LIT>" , self . service_type_code , self . service_type_text ) <EOL> def verbose_log ( self , * args , ** kwargs ) : <EOL> if config_value ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> log . debug ( * args , ** kwargs ) </s>
