<s> from functools import partial <EOL> from stdnet . utils import zip , JSPLITTER , EMPTYJSON , iteritems <EOL> from stdnet . utils . exceptions import * <EOL> from . base import ModelType , ModelBase , raise_kwargs <EOL> from . session import Manager <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class StdModel ( ModelBase ) : <EOL> '''<STR_LIT>''' <EOL> _model_type = '<STR_LIT:object>' <EOL> abstract = True <EOL> _loadedfields = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> meta = self . _meta <EOL> pkname = meta . pk . name <EOL> setattr ( self , pkname , kwargs . pop ( pkname , None ) ) <EOL> kwargs . pop ( meta . pk . name , None ) <EOL> for field in meta . scalarfields : <EOL> field . set_value ( self , kwargs . pop ( field . name , None ) ) <EOL> attributes = meta . attributes <EOL> if args : <EOL> N = len ( args ) <EOL> if N > len ( attributes ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> attrs , attributes = attributes [ : N ] , attributes [ N : ] <EOL> for name , value in zip ( attrs , args ) : <EOL> setattr ( self , name , value ) <EOL> for name in attributes : <EOL> setattr ( self , name , kwargs . pop ( name , None ) ) <EOL> if kwargs : <EOL> raise_kwargs ( self , kwargs ) <EOL> @ property <EOL> def has_all_data ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . get_state ( ) . persistent and self . _loadedfields is None <EOL> def set ( self , name , value ) : <EOL> meta = self . _meta <EOL> if name in meta . dfields : <EOL> meta . dfields [ name ] . set_value ( self , value ) <EOL> elif name in meta . attributes : <EOL> setattr ( self , name , value ) <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' % name ) <EOL> def loadedfields ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . _loadedfields is None : <EOL> for field in self . _meta . scalarfields : <EOL> yield field <EOL> else : <EOL> fields = self . _meta . dfields <EOL> processed = set ( ) <EOL> for name in self . _loadedfields : <EOL> if name in processed : <EOL> continue <EOL> if name in fields : <EOL> processed . add ( name ) <EOL> yield fields [ name ] <EOL> else : <EOL> name = name . split ( JSPLITTER ) [ <NUM_LIT:0> ] <EOL> if name in fields and name not in processed : <EOL> field = fields [ name ] <EOL> if field . type == '<STR_LIT>' : <EOL> processed . add ( name ) <EOL> yield field <EOL> def fieldvalue_pairs ( self , exclude_cache = False ) : <EOL> '''<STR_LIT>''' <EOL> for field in self . _meta . scalarfields : <EOL> if exclude_cache and field . as_cache : <EOL> continue <EOL> name = field . attname <EOL> if hasattr ( self , name ) : <EOL> yield field , getattr ( self , name ) <EOL> def clear_cache_fields ( self ) : <EOL> '''<STR_LIT>''' <EOL> for field in self . _meta . scalarfields : <EOL> if field . as_cache : <EOL> setattr ( self , field . name , None ) <EOL> def get_attr_value ( self , name ) : <EOL> '''<STR_LIT>''' <EOL> if name in self . _meta . dfields : <EOL> return self . _meta . dfields [ name ] . get_value ( self ) <EOL> elif not name . startswith ( '<STR_LIT>' ) and JSPLITTER in name : <EOL> bits = name . split ( JSPLITTER ) <EOL> fname = bits [ <NUM_LIT:0> ] <EOL> if fname in self . _meta . dfields : <EOL> return self . _meta . dfields [ fname ] . get_value ( self , * bits [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> return getattr ( self , name ) <EOL> else : <EOL> return getattr ( self , name ) <EOL> def clone ( self , ** data ) : <EOL> '''<STR_LIT>''' <EOL> meta = self . _meta <EOL> session = self . session <EOL> pkname = meta . pkname ( ) <EOL> pkvalue = data . pop ( pkname , None ) <EOL> fields = self . todict ( exclude_cache = True ) <EOL> fields . update ( data ) <EOL> fields . pop ( '<STR_LIT>' , None ) <EOL> obj = self . _meta . make_object ( ( pkvalue , None , fields ) ) <EOL> obj . session = session <EOL> return obj <EOL> def is_valid ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . _meta . is_valid ( self ) <EOL> def todict ( self , exclude_cache = False ) : <EOL> '''<STR_LIT>''' <EOL> odict = { } <EOL> for field , value in self . fieldvalue_pairs ( exclude_cache = exclude_cache ) : <EOL> value = field . serialise ( value ) <EOL> if value : <EOL> odict [ field . name ] = value <EOL> if self . _dbdata and '<STR_LIT:id>' in self . _dbdata : <EOL> odict [ '<STR_LIT>' ] = { '<STR_LIT:id>' : self . _dbdata [ '<STR_LIT:id>' ] } <EOL> return odict <EOL> def _to_json ( self , exclude_cache ) : <EOL> pk = self . pkvalue ( ) <EOL> if pk : <EOL> yield self . _meta . pkname ( ) , pk <EOL> for field , value in self . fieldvalue_pairs ( exclude_cache = <EOL> exclude_cache ) : <EOL> value = field . json_serialise ( value ) <EOL> if value not in EMPTYJSON : <EOL> yield field . name , value <EOL> def tojson ( self , exclude_cache = True ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( self . _to_json ( exclude_cache ) ) <EOL> def load_fields ( self , * fields ) : <EOL> '''<STR_LIT>''' <EOL> if self . _loadedfields is not None : <EOL> if self . session is None : <EOL> raise SessionNotAvailable ( '<STR_LIT>' ) <EOL> meta = self . _meta <EOL> kwargs = { meta . pkname ( ) : self . pkvalue ( ) } <EOL> obj = session . query ( self ) . load_only ( fields ) . get ( ** kwargs ) <EOL> for name in fields : <EOL> field = meta . dfields . get ( name ) <EOL> if field is not None : <EOL> setattr ( self , field . attname , <EOL> getattr ( obj , field . attname , None ) ) <EOL> def get_state_action ( self ) : <EOL> return '<STR_LIT>' if self . _loadedfields is None else '<STR_LIT>' <EOL> def load_related_model ( self , name , load_only = None , dont_load = None ) : <EOL> '''<STR_LIT>''' <EOL> field = self . _meta . dfields . get ( name ) <EOL> if not field : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> elif not field . type == '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' % name ) <EOL> return self . _load_related_model ( field , load_only , dont_load ) <EOL> @ classmethod <EOL> def get_field ( cls , name ) : <EOL> '''<STR_LIT>''' <EOL> return cls . _meta . dfields . get ( name ) <EOL> @ classmethod <EOL> def from_base64_data ( cls , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> o = cls ( ) <EOL> meta = cls . _meta <EOL> pkname = meta . pkname ( ) <EOL> for name , value in iteritems ( kwargs ) : <EOL> if name == pkname : <EOL> field = meta . pk <EOL> elif name in meta . dfields : <EOL> field = meta . dfields [ name ] <EOL> else : <EOL> continue <EOL> value = field . to_python ( value ) <EOL> setattr ( o , field . attname , value ) <EOL> return o <EOL> @ classmethod <EOL> def pk ( cls ) : <EOL> '''<STR_LIT>''' <EOL> return cls . _meta . pk <EOL> @ classmethod <EOL> def get_unique_instance ( cls , items ) : <EOL> if items : <EOL> if len ( items ) == <NUM_LIT:1> : <EOL> return items [ <NUM_LIT:0> ] <EOL> else : <EOL> raise QuerySetError ( '<STR_LIT>' ) <EOL> else : <EOL> raise cls . DoesNotExist ( ) <EOL> def __getstate__ ( self ) : <EOL> return ( self . id , self . _loadedfields , self . todict ( ) ) <EOL> def __setstate__ ( self , state ) : <EOL> self . _meta . load_state ( self , state ) <EOL> def _load_related_model ( self , field , load_only = None , dont_load = None ) : <EOL> cache_name = field . get_cache_name ( ) <EOL> if hasattr ( self , cache_name ) : <EOL> return getattr ( self , cache_name ) <EOL> else : <EOL> val = getattr ( self , field . attname ) <EOL> if val is None : <EOL> return self . __set_related_value ( field ) <EOL> else : <EOL> pkname = field . relmodel . _meta . pkname ( ) <EOL> qs = self . session . query ( field . relmodel ) <EOL> if load_only : <EOL> qs = qs . load_only ( * load_only ) <EOL> if dont_load : <EOL> qs = qs . dont_load ( * dont_load ) <EOL> callback = partial ( self . __set_related_value , field ) <EOL> return qs . filter ( ** { pkname : val } ) . items ( callback = callback ) <EOL> def __set_related_value ( self , field , items = None ) : <EOL> try : <EOL> rel_obj = self . get_unique_instance ( items ) <EOL> except self . DoesNotExist : <EOL> if field . required : <EOL> raise <EOL> else : <EOL> rel_obj = None <EOL> setattr ( self , field . attname , None ) <EOL> setattr ( self , field . get_cache_name ( ) , rel_obj ) <EOL> return rel_obj <EOL> def create_model ( name , * attributes , ** params ) : <EOL> '''<STR_LIT>''' <EOL> params [ '<STR_LIT>' ] = False <EOL> params [ '<STR_LIT>' ] = attributes <EOL> kwargs = { '<STR_LIT>' : params . pop ( '<STR_LIT>' , Manager ) , <EOL> '<STR_LIT:Meta>' : params } <EOL> return ModelType ( name , ( StdModel , ) , kwargs ) <EOL> def model_to_dict ( instance , fields = None , exclude = None ) : <EOL> if isinstance ( instance , StdModel ) : <EOL> return instance . todict ( ) <EOL> else : <EOL> d = { } <EOL> for field in instance . _meta . fields : <EOL> default = field . get_default ( ) <EOL> if default : <EOL> d [ field . name ] = default <EOL> return d </s>
