<s> import os <EOL> import datetime <EOL> from new import classobj <EOL> from django . template . loader import Context , render_to_string <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> import xml_models <EOL> import xml_models . xpath_twister as xpath <EOL> import rest_client <EOL> from saasu_client import DEFAULT_POST_URL <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class CollectionList ( list ) : <EOL> def __init__ ( self , type ) : <EOL> self . type = type <EOL> def append ( self , item ) : <EOL> if not isinstance ( item , self . type ) : <EOL> raise TypeError , '<STR_LIT>' % self . type <EOL> super ( CollectionList , self ) . append ( self . type ( xml = item . _xml ) ) <EOL> class Collection ( xml_models . BaseField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_type , order_by = None , ** kw ) : <EOL> self . field_type = field_type <EOL> self . order_by = order_by <EOL> xml_models . BaseField . __init__ ( self , ** kw ) <EOL> def parse ( self , xml , namespace ) : <EOL> matches = xpath . find_all ( xml , self . xpath , namespace ) <EOL> if not xml_models . BaseField in self . field_type . __bases__ : <EOL> results = CollectionList ( type = self . field_type ) <EOL> for match in matches : <EOL> results . append ( self . field_type ( xml = match ) ) <EOL> else : <EOL> field = self . field_type ( xpath = '<STR_LIT:.>' ) <EOL> results = [ field . parse ( xpath . domify ( match ) , namespace ) for match in matches ] <EOL> if self . order_by : <EOL> results . sort ( lambda a , b : cmp ( getattr ( a , self . order_by ) , getattr ( b , self . order_by ) ) ) <EOL> return results <EOL> CollectionField = Collection <EOL> class Error ( xml_models . Model ) : <EOL> """<STR_LIT>""" <EOL> error_type = xml_models . CharField ( xpath = "<STR_LIT>" ) <EOL> error_message = xml_models . CharField ( xpath = "<STR_LIT>" ) <EOL> class GETResponseBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , object = None , xml = None , dom = None ) : <EOL> """<STR_LIT>""" <EOL> self . __model__ = getattr ( object , '<STR_LIT>' , None ) <EOL> self . object = object <EOL> self . xml = xml <EOL> self . dom = dom <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> __model__ = self . __model__ [ <NUM_LIT:0> ] . lower ( ) + self . __model__ [ <NUM_LIT:1> : ] <EOL> errors = xml_models . Collection ( Error , order_by = "<STR_LIT>" , xpath = "<STR_LIT>" % __model__ ) <EOL> def validate_on_load ( cls ) : <EOL> for e in cls . errors : <EOL> exception = classobj ( str ( e . error_type ) , ( Exception , ) , { } ) <EOL> raise exception , e . error_message <EOL> cls = classobj ( '<STR_LIT>' , ( xml_models . Model , ) , { <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT>' : validate_on_load , <EOL> } ) <EOL> return cls ( xml = self . xml , dom = self . dom ) <EOL> class POSTResponseBase ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , object = None , xml = None , dom = None ) : <EOL> """<STR_LIT>""" <EOL> self . __model__ = getattr ( object , '<STR_LIT>' , None ) <EOL> self . object = object <EOL> self . xml = xml <EOL> self . dom = dom <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . object , '<STR_LIT>' ) and self . object . uid : <EOL> cmd = '<STR_LIT>' <EOL> else : <EOL> cmd = '<STR_LIT>' <EOL> uid = xml_models . IntField ( xpath = "<STR_LIT>" % ( <EOL> cmd , self . __model__ , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ cmd ] ) , default = None ) <EOL> lastUpdatedUid = xml_models . CharField ( xpath = "<STR_LIT>" % ( cmd , self . __model__ ) ) <EOL> errors = xml_models . Collection ( Error , order_by = "<STR_LIT>" , xpath = "<STR_LIT>" % ( cmd , self . __model__ ) ) <EOL> other_errors = xml_models . Collection ( Error , order_by = "<STR_LIT>" , xpath = "<STR_LIT>" ) <EOL> def validate_on_load ( cls ) : <EOL> for e in cls . errors : <EOL> exception = classobj ( str ( e . error_type ) , ( Exception , ) , { } ) <EOL> raise exception , e . error_message <EOL> for e in cls . other_errors : <EOL> exception = classobj ( str ( e . error_type ) , ( Exception , ) , { } ) <EOL> raise exception , e . error_message <EOL> cls = classobj ( '<STR_LIT>' , ( xml_models . Model , ) , { <EOL> '<STR_LIT>' : uid , <EOL> '<STR_LIT>' : lastUpdatedUid , <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT>' : other_errors , <EOL> '<STR_LIT>' : validate_on_load , <EOL> } ) <EOL> return cls ( xml = self . xml , dom = self . dom ) <EOL> class XmlModelManager ( xml_models . XmlModelManager ) : <EOL> def create ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> query = XmlModelQuery ( self , self . model , headers = self . headers ) . create ( ** kw ) <EOL> return query <EOL> class XmlModelQuery ( xml_models . XmlModelQuery ) : <EOL> def create ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> object = classobj ( '<STR_LIT>' , ( BaseModel , ) , { } ) <EOL> for key in kw . keys ( ) : <EOL> setattr ( object , key , kw [ key ] ) <EOL> ( filepath , filename ) = os . path . split ( self . model . template_name ) <EOL> ( shortname , extension ) = os . path . splitext ( filename ) <EOL> template_name = os . path . join ( filepath , '<STR_LIT>' % ( shortname , extension ) ) <EOL> context = Context ( object ) <EOL> xml_response = render_to_string ( template_name , dictionary = kw , context_instance = context ) <EOL> return self . model ( xml = xml_response ) <EOL> class ModelBase ( xml_models . ModelBase ) : <EOL> def __init__ ( cls , name , bases , attrs ) : <EOL> super ( ModelBase , cls ) . __init__ ( name , bases , attrs ) <EOL> if not hasattr ( cls , "<STR_LIT>" ) : <EOL> cls . _fields = { } <EOL> for field_name in attrs . keys ( ) : <EOL> if isinstance ( attrs [ field_name ] , xml_models . BaseField ) : <EOL> cls . _fields [ field_name ] = attrs [ field_name ] <EOL> if attrs . has_key ( "<STR_LIT>" ) : <EOL> setattr ( cls , "<STR_LIT>" , XmlModelManager ( cls , attrs [ "<STR_LIT>" ] ) ) <EOL> else : <EOL> setattr ( cls , "<STR_LIT>" , XmlModelManager ( cls , { } ) ) <EOL> def __setattr__ ( cls , attr , value ) : <EOL> if isinstance ( value , ( datetime . datetime , datetime . date ) ) and cls . _fields . has_key ( attr ) : <EOL> value = value . strftime ( cls . _fields [ attr ] . date_format ) <EOL> super ( ModelBase , cls ) . __setattr__ ( attr , value ) <EOL> class Model : <EOL> __metaclass__ = ModelBase <EOL> __doc__ = """<STR_LIT>""" <EOL> def __init__ ( self , xml = None , dom = None ) : <EOL> self . _xml = xml <EOL> self . _dom = dom <EOL> self . _cache = { } <EOL> self . validate_on_load ( ) <EOL> """<STR_LIT>""" <EOL> def validate_on_load ( self ) : <EOL> pass <EOL> def _get_xml ( self ) : <EOL> if self . _dom is None : <EOL> try : <EOL> self . _dom = xpath . domify ( self . _xml ) <EOL> except Exception , e : <EOL> print self . _xml <EOL> print str ( e ) <EOL> raise e <EOL> return self . _dom <EOL> def _set_value ( self , field , value ) : <EOL> self . _cache [ field ] = value <EOL> def _parse_field ( self , field ) : <EOL> if not self . _cache . has_key ( field ) : <EOL> namespace = None <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> namespace = self . namespace <EOL> self . _cache [ field ] = field . parse ( self . _get_xml ( ) , namespace ) <EOL> return self . _cache [ field ] <EOL> class BaseModel ( Model ) : <EOL> """<STR_LIT>""" <EOL> __model__ = '<STR_LIT>' <EOL> template_name = '<STR_LIT>' <EOL> def process_response ( self , payload = None , headers = { } , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> response = rest_client . Client ( "<STR_LIT>" ) . POST ( url = DEFAULT_POST_URL % prefix , payload = payload , headers = headers ) <EOL> if not response . content : <EOL> raise ObjectDoesNotExist , self <EOL> if response . response_code == <NUM_LIT> : <EOL> raise ObjectDoesNotExist , self <EOL> content = response . content . read ( ) <EOL> if not content : <EOL> raise ObjectDoesNotExist , self <EOL> return content <EOL> def validate_on_load ( self ) : <EOL> """<STR_LIT>""" <EOL> GETResponseBase ( object = self , xml = self . _xml ) . register ( ) <EOL> def save ( self , commit = True ) : <EOL> """<STR_LIT>""" <EOL> context = Context ( ) <EOL> context . update ( <EOL> { <EOL> '<STR_LIT:object>' : self , <EOL> '<STR_LIT:date>' : self . date , <EOL> } <EOL> ) <EOL> output = render_to_string ( self . template_name , context_instance = context ) <EOL> if commit == True : <EOL> tasks_response = POSTResponseBase ( object = self , xml = self . process_response ( payload = output ) ) . register ( ) <EOL> self . uid = tasks_response . uid <EOL> self . lastUpdatedUid = tasks_response . lastUpdatedUid <EOL> return self </s>
