<s> import datetime <EOL> import logging <EOL> import json <EOL> import six <EOL> import requests <EOL> from pyavatax . django_integration import get_django_recorder <EOL> def str_to_class ( klassname ) : <EOL> """<STR_LIT>""" <EOL> import sys <EOL> try : <EOL> identifier = getattr ( sys . modules [ __name__ ] , klassname ) <EOL> except AttributeError : <EOL> raise NameError ( "<STR_LIT>" % klassname ) <EOL> if isinstance ( identifier , six . class_types ) : <EOL> return identifier <EOL> raise TypeError ( "<STR_LIT>" % klassname ) <EOL> def isiterable ( foo ) : <EOL> try : <EOL> iter ( foo ) <EOL> except TypeError : <EOL> return False <EOL> else : <EOL> return True <EOL> class AvalaraLogging ( object ) : <EOL> logger = None <EOL> @ staticmethod <EOL> def get_logger ( ) : <EOL> if not AvalaraLogging . logger : <EOL> AvalaraLogging . logger = logging . getLogger ( '<STR_LIT>' ) <EOL> return AvalaraLogging . logger <EOL> @ staticmethod <EOL> def set_logger ( logger ) : <EOL> if isinstance ( logger , logging . Logger ) : <EOL> AvalaraLogging . logger = logger <EOL> else : <EOL> raise AvalaraException ( '<STR_LIT>' ) <EOL> class AvalaraBase ( object ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ ] <EOL> _contains = [ ] <EOL> _has = [ ] <EOL> def __init__ ( self , allow_new_fields = False , * args , ** kwargs ) : <EOL> self . _setup ( ) <EOL> self . allow_new_fields = allow_new_fields <EOL> self . logger = AvalaraLogging . get_logger ( ) <EOL> self . update ( ** kwargs ) <EOL> def _setup ( self ) : <EOL> """<STR_LIT>""" <EOL> for field in self . _contains : <EOL> setattr ( self , field , [ ] ) <EOL> def clean_me ( self ) : <EOL> pass <EOL> def clean ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return <EOL> """<STR_LIT>""" <EOL> for f in self . _fields : <EOL> clean_fn = '<STR_LIT>' % f <EOL> if hasattr ( self , clean_fn ) : <EOL> getattr ( self , clean_fn ) ( ) <EOL> for f in self . _has : <EOL> if hasattr ( self , f ) : <EOL> getattr ( self , f ) . clean ( ) <EOL> for f in self . _contains : <EOL> for v in getattr ( self , f ) : <EOL> v . clean ( ) <EOL> """<STR_LIT>""" <EOL> self . clean_me ( ) <EOL> def _handle_pluralize ( self , k ) : <EOL> _k = '<STR_LIT>' if k == '<STR_LIT>' else k <EOL> _k = '<STR_LIT>' if k == '<STR_LIT>' else _k <EOL> return _k <EOL> def _invalid_field ( self , field ) : <EOL> msg = AvalaraException . CODE_INVALID_FIELD , '<STR_LIT>' % field <EOL> self . logger . warning ( msg ) <EOL> if not self . allow_new_fields : <EOL> raise AvalaraException ( msg ) <EOL> def update ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for k , v in six . iteritems ( kwargs ) : <EOL> if k in self . _fields : <EOL> setattr ( self , k , v ) <EOL> elif k in self . _has : <EOL> klass = str_to_class ( self . _handle_pluralize ( k ) ) <EOL> if isinstance ( v , klass ) : <EOL> setattr ( self , k , v ) <EOL> elif isinstance ( v , dict ) : <EOL> setattr ( self , k , klass ( allow_new_fields = self . allow_new_fields , ** v ) ) <EOL> elif k in self . _contains : <EOL> klass = str_to_class ( self . _handle_pluralize ( k ) ) <EOL> for _v in v : <EOL> if isinstance ( _v , klass ) : <EOL> getattr ( self , k ) . append ( _v ) <EOL> elif isinstance ( _v , dict ) : <EOL> getattr ( self , k ) . append ( klass ( allow_new_fields = self . allow_new_fields , ** _v ) ) <EOL> else : <EOL> self . _invalid_field ( k ) <EOL> self . clean ( ) <EOL> def todict ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) and not hasattr ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . validate ( ) <EOL> data = { } <EOL> for f in self . _fields : <EOL> if hasattr ( self , f ) : <EOL> v = getattr ( self , f ) <EOL> if isinstance ( v , datetime . date ) or isinstance ( v , datetime . datetime ) : <EOL> v = v . isoformat ( ) <EOL> data [ f ] = v <EOL> for f in self . _has : <EOL> if hasattr ( self , f ) : <EOL> obj = getattr ( self , f ) <EOL> data [ f ] = obj . todict ( ) <EOL> for f in self . _contains : <EOL> if isiterable ( getattr ( self , f ) ) : <EOL> data [ f ] = [ ] <EOL> for obj in getattr ( self , f ) : <EOL> data [ f ] . append ( obj . todict ( ) ) <EOL> else : <EOL> data [ f ] = obj . todict ( ) <EOL> return data <EOL> class BaseAPI ( object ) : <EOL> """<STR_LIT>""" <EOL> default_headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } <EOL> PRODUCTION_HOST = None <EOL> DEVELOPMENT_HOST = None <EOL> protocol = '<STR_LIT>' <EOL> default_timeout = <NUM_LIT> <EOL> logger = None <EOL> def __init__ ( self , username = None , password = None , live = False , timeout = None , proxies = { } , recorder = None , ** kwargs ) : <EOL> self . host = self . PRODUCTION_HOST if live else self . DEVELOPMENT_HOST <EOL> self . url = "<STR_LIT>" % ( BaseAPI . protocol , self . host ) <EOL> self . username = username <EOL> self . proxies = proxies <EOL> self . headers = BaseAPI . default_headers . update ( { '<STR_LIT>' : self . host } ) <EOL> self . password = password <EOL> self . timeout = timeout or BaseAPI . default_timeout <EOL> self . logger = AvalaraLogging . get_logger ( ) <EOL> if recorder is None : <EOL> recorder = get_django_recorder ( ) <EOL> self . recorder = recorder <EOL> def _get ( self , stem , data ) : <EOL> return self . _request ( '<STR_LIT:GET>' , stem , params = data ) <EOL> def _post ( self , stem , data , params = { } ) : <EOL> return self . _request ( '<STR_LIT:POST>' , stem , params = params , data = data ) <EOL> def _request ( self , http_method , stem , data = { } , params = { } ) : <EOL> url = '<STR_LIT>' % ( self . url , stem ) <EOL> data = json . dumps ( data ) <EOL> data = data . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> data = data . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> data = data . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> kwargs = { <EOL> '<STR_LIT>' : params , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : self . headers , <EOL> '<STR_LIT>' : ( self . username , self . password ) , <EOL> '<STR_LIT>' : self . proxies , <EOL> '<STR_LIT>' : self . timeout <EOL> } <EOL> resp = None <EOL> try : <EOL> if http_method == '<STR_LIT:GET>' : <EOL> resp = requests . get ( url , ** kwargs ) <EOL> elif http_method == '<STR_LIT:POST>' : <EOL> resp = requests . post ( url , ** kwargs ) <EOL> except ( requests . exceptions . ConnectionError , requests . exceptions . SSLError , requests . exceptions . HTTPError , requests . exceptions . Timeout ) as e : <EOL> self . logger . warning ( e ) <EOL> raise AvalaraServerNotReachableException ( e ) <EOL> if resp . status_code == requests . codes . ok : <EOL> if resp . json is None : <EOL> raise AvalaraServerDetailException ( resp ) <EOL> return resp <EOL> else : <EOL> raise AvalaraServerDetailException ( resp ) <EOL> class BaseResponse ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> SUCCESS = '<STR_LIT>' <EOL> ERROR = '<STR_LIT>' <EOL> _fields = [ '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' ] <EOL> def __init__ ( self , response , * args , ** kwargs ) : <EOL> self . response = response <EOL> super ( BaseResponse , self ) . __init__ ( * args , allow_new_fields = True , ** response . json ( ) ) <EOL> @ property <EOL> def _details ( self ) : <EOL> try : <EOL> return [ { m . RefersTo : m . Summary } for m in self . Messages ] <EOL> except AttributeError : <EOL> return [ { m . Source : m . Summary } for m in self . Messages ] <EOL> @ property <EOL> def is_success ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self . response , '<STR_LIT>' ) : <EOL> raise AvalaraException ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' not in self . response . json ( ) : <EOL> raise AvalaraException ( '<STR_LIT>' ) <EOL> cond = self . response . json ( ) . get ( '<STR_LIT>' , BaseResponse . ERROR ) == BaseResponse . SUCCESS <EOL> return True if cond else False <EOL> @ property <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in self . response . json ( ) : <EOL> raise AvalaraException ( '<STR_LIT>' ) <EOL> cond = self . response . json ( ) . get ( '<STR_LIT>' , BaseResponse . SUCCESS ) == BaseResponse . ERROR <EOL> return self . _details if cond else False <EOL> class ErrorResponse ( BaseResponse ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' ] <EOL> @ property <EOL> def is_success ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> @ property <EOL> def error ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _details <EOL> class AvalaraBaseException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AvalaraException ( AvalaraBaseException ) : <EOL> """<STR_LIT>""" <EOL> CODE_REQD = <NUM_LIT:50> <EOL> CODE_BAD_ARGS = <NUM_LIT:100> <EOL> CODE_BAD_DOC = <NUM_LIT> <EOL> CODE_BAD_LATLNG = <NUM_LIT> <EOL> CODE_BAD_CANCEL = <NUM_LIT> <EOL> CODE_HAS_FROM = <NUM_LIT> <EOL> CODE_HAS_TO = <NUM_LIT> <EOL> CODE_BAD_ADDRESS = <NUM_LIT> <EOL> CODE_BAD_DETAIL = <NUM_LIT> <EOL> CODE_BAD_LINE = <NUM_LIT> <EOL> CODE_BAD_OVERRIDE = <NUM_LIT> <EOL> CODE_INVALID_FIELD = <NUM_LIT> <EOL> CODE_BAD_DOCTYPE = <NUM_LIT> <EOL> CODE_BAD_DATE = <NUM_LIT> <EOL> CODE_BAD_FLOAT = <NUM_LIT> <EOL> CODE_BAD_BOOL = <NUM_LIT> <EOL> CODE_BAD_ORIGIN = <NUM_LIT> <EOL> CODE_BAD_DEST = <NUM_LIT> <EOL> CODE_TOO_LONG = <NUM_LIT> <EOL> CODE_BAD_OTYPE = <NUM_LIT> <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if len ( args ) > <NUM_LIT:1> and isinstance ( args [ <NUM_LIT:0> ] , int ) : <EOL> self . code = args [ <NUM_LIT:0> ] <EOL> super ( AvalaraException , self ) . __init__ ( * args , ** kwargs ) <EOL> class AvalaraTypeException ( AvalaraException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AvalaraValidationException ( AvalaraException ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class AvalaraServerNotReachableException ( AvalaraBaseException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , request_exception , * args , ** kwargs ) : <EOL> self . request_exception = request_exception <EOL> def __str__ ( self ) : <EOL> return repr ( self . request_exception ) <EOL> class AvalaraServerException ( AvalaraBaseException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , response , * args , ** kwargs ) : <EOL> self . response = response <EOL> self . status_code = response . status_code <EOL> self . raw_response = response . text <EOL> self . request_data = response . request . body <EOL> self . method = response . request . method <EOL> self . url = response . request . url <EOL> self . has_details = False <EOL> try : <EOL> self . has_details = True if response . json ( ) else False <EOL> except : <EOL> pass <EOL> @ property <EOL> def full_request_as_string ( self ) : <EOL> """<STR_LIT>""" <EOL> fmt = "<STR_LIT>" <EOL> return fmt % ( repr ( self . status_code ) , repr ( self . method ) , repr ( self . url ) , repr ( self . request_data ) , repr ( self . errors ) ) <EOL> @ property <EOL> def errors ( self ) : <EOL> """<STR_LIT>""" <EOL> return ErrorResponse ( self . response ) . _details if self . has_details else self . raw_response <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( repr ( self . status_code ) , repr ( self . method ) , repr ( self . url ) ) <EOL> class AvalaraServerDetailException ( AvalaraServerException ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return self . full_request_as_string <EOL> class Document ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> DOC_TYPE_SALE_ORDER = '<STR_LIT>' <EOL> DOC_TYPE_SALE_INVOICE = '<STR_LIT>' <EOL> DOC_TYPE_RETURN_ORDER = '<STR_LIT>' <EOL> DOC_TYPE_RETURN_INVOICE = '<STR_LIT>' <EOL> DOC_TYPE_PURCHASE_ORDER = '<STR_LIT>' <EOL> DOC_TYPE_PURCHASE_INVOICE = '<STR_LIT>' <EOL> DOC_TYPE_INVENTORY_ORDER = '<STR_LIT>' <EOL> DOC_TYPE_INVENTORY_INVOICE = '<STR_LIT>' <EOL> DOC_TYPES = ( DOC_TYPE_SALE_ORDER , DOC_TYPE_SALE_INVOICE , DOC_TYPE_RETURN_ORDER , DOC_TYPE_RETURN_INVOICE , DOC_TYPE_PURCHASE_ORDER , DOC_TYPE_PURCHASE_INVOICE , DOC_TYPE_INVENTORY_ORDER , DOC_TYPE_INVENTORY_INVOICE ) <EOL> CANCEL_POST_FAILED = '<STR_LIT>' <EOL> CANCEL_DOC_DELETED = '<STR_LIT>' <EOL> CANCEL_DOC_VOIDED = '<STR_LIT>' <EOL> CANCEL_ADJUSTMENT_CANCELED = '<STR_LIT>' <EOL> CANCEL_CODES = ( CANCEL_POST_FAILED , CANCEL_DOC_DELETED , CANCEL_DOC_VOIDED , CANCEL_ADJUSTMENT_CANCELED ) <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _has = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , logger = None , * args , ** kwargs ) : <EOL> super ( Document , self ) . __init__ ( * args , ** kwargs ) <EOL> if logger is None : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> Document . logger = logger <EOL> @ staticmethod <EOL> def from_data ( data ) : <EOL> return Document ( ** data ) <EOL> @ staticmethod <EOL> def new_sales_order ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_SALE_ORDER } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_sales_invoice ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_SALE_INVOICE } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_return_order ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_RETURN_ORDER } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_return_invoice ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_RETURN_INVOICE } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_purchase_order ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_PURCHASE_ORDER } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_purchase_invoice ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_PURCHASE_INVOICE } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_inventory_order ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_INVENTORY_ORDER } ) <EOL> return Document ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def new_inventory_invoice ( * args , ** kwargs ) : <EOL> kwargs . update ( { '<STR_LIT>' : Document . DOC_TYPE_INVENTORY_INVOICE } ) <EOL> return Document ( * args , ** kwargs ) <EOL> def clean_DocType ( self ) : <EOL> doc_type = getattr ( self , '<STR_LIT>' , None ) <EOL> if doc_type and doc_type not in Document . DOC_TYPES : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DOCTYPE , '<STR_LIT>' % doc_type ) <EOL> @ staticmethod <EOL> def _clean_float ( f ) : <EOL> if f and not isinstance ( f , float ) : <EOL> return float ( f ) <EOL> elif f is None : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return f <EOL> @ staticmethod <EOL> def _clean_int ( i ) : <EOL> if i and not isinstance ( i , int ) : <EOL> return int ( i ) <EOL> elif i is None : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return i <EOL> @ staticmethod <EOL> def _clean_date ( date ) : <EOL> if date and not isinstance ( date , datetime . date ) : <EOL> return datetime . datetime . strptime ( date , '<STR_LIT>' ) . date ( ) <EOL> else : <EOL> return date <EOL> def clean_DocDate ( self ) : <EOL> doc_date = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> date = Document . _clean_date ( doc_date ) <EOL> setattr ( self , '<STR_LIT>' , date ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DATE , '<STR_LIT>' ) <EOL> def clean_Discount ( self ) : <EOL> discount = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> f = Document . _clean_float ( discount ) <EOL> setattr ( self , '<STR_LIT>' , f ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_FLOAT , '<STR_LIT>' ) <EOL> def clean_Commit ( self ) : <EOL> commit = getattr ( self , '<STR_LIT>' , None ) <EOL> if commit is not None : <EOL> if commit is not True and commit is not False : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_BOOL , '<STR_LIT>' ) <EOL> def clean_PaymentDate ( self ) : <EOL> pay_date = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> date = Document . _clean_date ( pay_date ) <EOL> setattr ( self , '<STR_LIT>' , date ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DATE , '<STR_LIT>' ) <EOL> def set_detail_level ( self , detail_level = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> detail_level = DetailLevel ( ** kwargs ) <EOL> if isinstance ( detail_level , DetailLevel ) : <EOL> setattr ( self , '<STR_LIT>' , detail_level ) <EOL> else : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_DETAIL , '<STR_LIT>' % ( detail_level , DetailLevel ) ) <EOL> def add_override ( self , override = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> override = TaxOverride ( ** kwargs ) <EOL> if not isinstance ( override , TaxOverride ) : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_OVERRIDE , '<STR_LIT>' % ( override , TaxOverride ) ) <EOL> self . TaxOverride = override <EOL> def add_line ( self , line = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> line = Line ( ** kwargs ) <EOL> if not isinstance ( line , Line ) : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_LINE , '<STR_LIT>' % ( line , Line ) ) <EOL> if not hasattr ( line , '<STR_LIT>' ) : <EOL> count = len ( self . Lines ) <EOL> setattr ( line , '<STR_LIT>' , count + <NUM_LIT:1> ) <EOL> Document . logger . debug ( '<STR_LIT>' % ( getattr ( self , '<STR_LIT>' , None ) , line . LineNo ) ) <EOL> self . Lines . append ( line ) <EOL> def add_from_address ( self , address = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraException ( AvalaraException . CODE_HAS_FROM , '<STR_LIT>' ) <EOL> if kwargs : <EOL> address = Address ( ** kwargs ) <EOL> if not isinstance ( address , Address ) : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_ADDRESS , '<STR_LIT>' % ( address , Address ) ) <EOL> if not hasattr ( address , '<STR_LIT>' ) : <EOL> setattr ( address , '<STR_LIT>' , Address . DEFAULT_FROM_ADDRESS_CODE ) <EOL> Document . logger . debug ( '<STR_LIT>' % getattr ( self , '<STR_LIT>' , None ) ) <EOL> self . from_address_code = getattr ( address , '<STR_LIT>' ) <EOL> self . Addresses . append ( address ) <EOL> def add_to_address ( self , address = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraException ( AvalaraException . CODE_HAS_TO , '<STR_LIT>' ) <EOL> if kwargs : <EOL> address = Address ( ** kwargs ) <EOL> if not isinstance ( address , Address ) : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_ADDRESS , '<STR_LIT>' % ( address , Address ) ) <EOL> if not hasattr ( address , '<STR_LIT>' ) : <EOL> setattr ( address , '<STR_LIT>' , Address . DEFAULT_TO_ADDRESS_CODE ) <EOL> Document . logger . debug ( '<STR_LIT>' % getattr ( self , '<STR_LIT>' , None ) ) <EOL> self . to_address_code = getattr ( address , '<STR_LIT>' ) <EOL> self . Addresses . append ( address ) <EOL> def add_address ( self , address = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs : <EOL> address = Address ( ** kwargs ) <EOL> if not isinstance ( address , Address ) : <EOL> raise AvalaraTypeException ( AvalaraException . CODE_BAD_ADDRESS , '<STR_LIT>' % ( address , Address ) ) <EOL> self . Addresses . append ( address ) <EOL> def validate_codes ( self ) : <EOL> """<STR_LIT>""" <EOL> for l in self . Lines : <EOL> if not hasattr ( l , '<STR_LIT>' ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_ORIGIN , '<STR_LIT>' % l . LineNo ) <EOL> l . OriginCode = self . from_address_code <EOL> Document . logger . debug ( '<STR_LIT>' % ( getattr ( self , '<STR_LIT>' , None ) , l . OriginCode ) ) <EOL> if not hasattr ( l , '<STR_LIT>' ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DEST , '<STR_LIT>' % l . LineNo ) <EOL> l . DestinationCode = self . to_address_code <EOL> Document . logger . debug ( '<STR_LIT>' % ( getattr ( self , '<STR_LIT>' , None ) , l . DestinationCode ) ) <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DOCTYPE , '<STR_LIT>' ) <EOL> if len ( self . Addresses ) == <NUM_LIT:0> : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_ADDRESS , '<STR_LIT>' ) <EOL> if len ( self . Lines ) == <NUM_LIT:0> : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_LINE , '<STR_LIT>' ) <EOL> self . validate_codes ( ) <EOL> @ property <EOL> def total ( self ) : <EOL> """<STR_LIT>""" <EOL> return sum ( [ getattr ( line , '<STR_LIT>' , <NUM_LIT:0> ) for line in self . Lines ] ) <EOL> def update_doc_code_from_response ( self , post_tax_response ) : <EOL> """<STR_LIT>""" <EOL> from pyavatax . api import PostTaxResponse <EOL> if not isinstance ( post_tax_response , PostTaxResponse ) : <EOL> raise AvalaraTypeException ( '<STR_LIT>' % type ( PostTaxResponse ) ) <EOL> setattr ( self , '<STR_LIT>' , getattr ( post_tax_response , '<STR_LIT>' ) ) <EOL> Document . logger . debug ( '<STR_LIT>' % getattr ( self , '<STR_LIT>' , None ) ) <EOL> class TaxOverride ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> OVERRIDE_NONE = '<STR_LIT:None>' <EOL> OVERRIDE_AMOUNT = '<STR_LIT>' <EOL> OVERRIDE_DATE = '<STR_LIT>' <EOL> OVERRIDE_EXEMPT = '<STR_LIT>' <EOL> OVERRIDE_TYPES = ( OVERRIDE_NONE , OVERRIDE_AMOUNT , OVERRIDE_DATE , OVERRIDE_EXEMPT ) <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def from_data ( data ) : <EOL> return TaxOverride ( ** data ) <EOL> def clean_me ( self ) : <EOL> if self . TaxOverrideType == TaxOverride . OVERRIDE_AMOUNT : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_REQD , '<STR_LIT>' ) <EOL> elif self . TaxOverrideType == TaxOverride . OVERRIDE_DATE : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_REQD , '<STR_LIT>' ) <EOL> def clean_TaxOverrideType ( self ) : <EOL> otype = getattr ( self , '<STR_LIT>' , None ) <EOL> if otype is None : <EOL> otype = '<STR_LIT:None>' <EOL> if otype not in TaxOverride . OVERRIDE_TYPES : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_OTYPE , '<STR_LIT>' ) <EOL> setattr ( self , '<STR_LIT>' , otype ) <EOL> def clean_Reason ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_REQD , '<STR_LIT>' ) <EOL> def clean_TaxDate ( self ) : <EOL> tax_date = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> date = Document . _clean_date ( tax_date ) <EOL> setattr ( self , '<STR_LIT>' , date ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_DATE , '<STR_LIT>' ) <EOL> def clean_TaxAmount ( self ) : <EOL> amount = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> f = Document . _clean_float ( amount ) <EOL> setattr ( self , '<STR_LIT>' , f ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_FLOAT , '<STR_LIT>' ) <EOL> class Line ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs . update ( { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> return super ( Line , self ) . __init__ ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def from_data ( data ) : <EOL> return Line ( ** data ) <EOL> def clean_Qty ( self ) : <EOL> qty = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> i = Document . _clean_int ( qty ) <EOL> setattr ( self , '<STR_LIT>' , i ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_FLOAT , '<STR_LIT>' ) <EOL> def clean_Amount ( self ) : <EOL> amount = getattr ( self , '<STR_LIT>' , None ) <EOL> try : <EOL> f = Document . _clean_float ( amount ) <EOL> setattr ( self , '<STR_LIT>' , f ) <EOL> except ValueError : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_BAD_FLOAT , '<STR_LIT>' ) <EOL> def clean_ItemCode ( self ) : <EOL> code = getattr ( self , '<STR_LIT>' , None ) <EOL> if code and len ( code ) > <NUM_LIT:50> : <EOL> raise AvalaraValidationException ( AvalaraException . CODE_TOO_LONG , '<STR_LIT>' ) <EOL> class Address ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> DEFAULT_FROM_ADDRESS_CODE = "<STR_LIT:1>" <EOL> DEFAULT_TO_ADDRESS_CODE = "<STR_LIT:2>" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def from_data ( data ) : <EOL> return Address ( ** data ) <EOL> @ property <EOL> def describe_address_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:F>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:H>' : "<STR_LIT>" , <EOL> '<STR_LIT:P>' : "<STR_LIT>" , <EOL> '<STR_LIT:R>' : "<STR_LIT>" , <EOL> '<STR_LIT:S>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } . get ( getattr ( self , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> @ property <EOL> def describe_fips_code ( self ) : <EOL> """<STR_LIT>""" <EOL> fips = len ( getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if fips == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif fips >= <NUM_LIT:1> and fips <= <NUM_LIT:2> : <EOL> return '<STR_LIT>' <EOL> elif fips >= <NUM_LIT:3> and fips <= <NUM_LIT:5> : <EOL> return '<STR_LIT>' <EOL> elif fips >= <NUM_LIT:6> and fips <= <NUM_LIT:10> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def describe_carrier_route ( self ) : <EOL> """<STR_LIT>""" <EOL> return { <EOL> '<STR_LIT:B>' : "<STR_LIT>" , <EOL> '<STR_LIT:C>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT:H>' : "<STR_LIT>" , <EOL> '<STR_LIT:R>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } . get ( getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ property <EOL> def describe_post_net ( self ) : <EOL> """<STR_LIT>""" <EOL> post = len ( getattr ( self , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if post == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif post >= <NUM_LIT:1> and post <= <NUM_LIT:5> : <EOL> return '<STR_LIT>' <EOL> elif post >= <NUM_LIT:6> and post <= <NUM_LIT:9> : <EOL> return '<STR_LIT>' <EOL> elif post >= <NUM_LIT:10> and post >= <NUM_LIT:11> : <EOL> return '<STR_LIT>' <EOL> elif post == <NUM_LIT:12> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> return '<STR_LIT>' <EOL> class Messages ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class DetailLevel ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class TaxAddresses ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' ] <EOL> class TaxDetails ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class TaxLines ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' ] <EOL> class CancelTaxResult ( AvalaraBase ) : <EOL> """<STR_LIT>""" <EOL> _fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _contains = [ '<STR_LIT>' ] </s>
