<s> """<STR_LIT>""" <EOL> import datetime <EOL> import logging <EOL> import time <EOL> import urlparse <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_errors <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . api import users <EOL> Error = datastore_errors . Error <EOL> BadValueError = datastore_errors . BadValueError <EOL> BadPropertyError = datastore_errors . BadPropertyError <EOL> BadRequestError = datastore_errors . BadRequestError <EOL> EntityNotFoundError = datastore_errors . EntityNotFoundError <EOL> BadArgumentError = datastore_errors . BadArgumentError <EOL> QueryNotFoundError = datastore_errors . QueryNotFoundError <EOL> TransactionNotFoundError = datastore_errors . TransactionNotFoundError <EOL> Rollback = datastore_errors . Rollback <EOL> TransactionFailedError = datastore_errors . TransactionFailedError <EOL> BadFilterError = datastore_errors . BadFilterError <EOL> BadQueryError = datastore_errors . BadQueryError <EOL> BadKeyError = datastore_errors . BadKeyError <EOL> InternalError = datastore_errors . InternalError <EOL> ValidationError = BadValueError <EOL> Key = datastore_types . Key <EOL> Category = datastore_types . Category <EOL> Link = datastore_types . Link <EOL> Email = datastore_types . Email <EOL> GeoPt = datastore_types . GeoPt <EOL> IM = datastore_types . IM <EOL> PhoneNumber = datastore_types . PhoneNumber <EOL> PostalAddress = datastore_types . PostalAddress <EOL> Rating = datastore_types . Rating <EOL> Text = datastore_types . Text <EOL> Blob = datastore_types . Blob <EOL> _kind_map = { } <EOL> _SELF_REFERENCE = object ( ) <EOL> _RESERVED_WORDS = set ( [ '<STR_LIT>' ] ) <EOL> class NotSavedError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class KindError ( BadValueError ) : <EOL> """<STR_LIT>""" <EOL> class PropertyError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class DuplicatePropertyError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class ConfigurationError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class ReservedWordError ( Error ) : <EOL> """<STR_LIT>""" <EOL> _ALLOWED_PROPERTY_TYPES = set ( [ <EOL> basestring , <EOL> str , <EOL> unicode , <EOL> bool , <EOL> int , <EOL> long , <EOL> float , <EOL> Key , <EOL> datetime . datetime , <EOL> datetime . date , <EOL> datetime . time , <EOL> Blob , <EOL> Text , <EOL> users . User , <EOL> Category , <EOL> Link , <EOL> Email , <EOL> GeoPt , <EOL> IM , <EOL> PhoneNumber , <EOL> PostalAddress , <EOL> Rating , <EOL> ] ) <EOL> _ALLOWED_EXPANDO_PROPERTY_TYPES = set ( _ALLOWED_PROPERTY_TYPES ) <EOL> _ALLOWED_EXPANDO_PROPERTY_TYPES . update ( ( list , tuple , type ( None ) ) ) <EOL> def class_for_kind ( kind ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _kind_map [ kind ] <EOL> except KeyError : <EOL> raise KindError ( '<STR_LIT>' % kind ) <EOL> def check_reserved_word ( attr_name ) : <EOL> """<STR_LIT>""" <EOL> if datastore_types . RESERVED_PROPERTY_NAME . match ( attr_name ) : <EOL> raise ReservedWordError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if attr_name in _RESERVED_WORDS or attr_name in dir ( Model ) : <EOL> raise ReservedWordError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % locals ( ) ) <EOL> class PropertiedClass ( type ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> """<STR_LIT>""" <EOL> super ( PropertiedClass , cls ) . __init__ ( name , bases , dct ) <EOL> cls . _properties = { } <EOL> defined = set ( ) <EOL> for base in bases : <EOL> if hasattr ( base , '<STR_LIT>' ) : <EOL> property_keys = base . _properties . keys ( ) <EOL> duplicate_properties = defined . intersection ( property_keys ) <EOL> if duplicate_properties : <EOL> raise DuplicatePropertyError ( <EOL> '<STR_LIT>' % <EOL> ( base . __name__ , list ( duplicate_properties ) ) ) <EOL> defined . update ( property_keys ) <EOL> cls . _properties . update ( base . _properties ) <EOL> for attr_name in dct . keys ( ) : <EOL> attr = dct [ attr_name ] <EOL> if isinstance ( attr , Property ) : <EOL> check_reserved_word ( attr_name ) <EOL> if attr_name in defined : <EOL> raise DuplicatePropertyError ( '<STR_LIT>' % attr_name ) <EOL> defined . add ( attr_name ) <EOL> cls . _properties [ attr_name ] = attr <EOL> attr . __property_config__ ( cls , attr_name ) <EOL> _kind_map [ cls . kind ( ) ] = cls <EOL> class Property ( object ) : <EOL> """<STR_LIT>""" <EOL> creation_counter = <NUM_LIT:0> <EOL> def __init__ ( self , verbose_name = None , name = None , default = None , <EOL> required = False , validator = None , choices = None ) : <EOL> """<STR_LIT>""" <EOL> self . verbose_name = verbose_name <EOL> self . name = name <EOL> self . default = default <EOL> self . required = required <EOL> self . validator = validator <EOL> self . choices = choices <EOL> self . creation_counter = Property . creation_counter <EOL> Property . creation_counter += <NUM_LIT:1> <EOL> def __property_config__ ( self , model_class , property_name ) : <EOL> """<STR_LIT>""" <EOL> self . model_class = model_class <EOL> if self . name is None : <EOL> self . name = property_name <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> """<STR_LIT>""" <EOL> if model_instance is None : <EOL> return self <EOL> try : <EOL> return getattr ( model_instance , self . _attr_name ( ) ) <EOL> except AttributeError : <EOL> return None <EOL> def __set__ ( self , model_instance , value ) : <EOL> """<STR_LIT>""" <EOL> value = self . validate ( value ) <EOL> setattr ( model_instance , self . _attr_name ( ) , value ) <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . default <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if self . empty ( value ) : <EOL> if self . required : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> else : <EOL> if self . choices : <EOL> match = False <EOL> for choice in self . choices : <EOL> if choice == value : <EOL> match = True <EOL> if not match : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , str ( [ str ( c ) for c in self . choices ] ) ) ) <EOL> if self . validator is not None : <EOL> self . validator ( value ) <EOL> return value <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return not value <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> return self . __get__ ( model_instance , model_instance . __class__ ) <EOL> def make_value_from_datastore ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def _attr_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:_>' + self . name <EOL> data_type = str <EOL> def datastore_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data_type <EOL> class Model ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = PropertiedClass <EOL> def __init__ ( self , parent = None , key_name = None , _app = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if key_name == '<STR_LIT>' : <EOL> raise BadKeyError ( '<STR_LIT>' ) <EOL> elif key_name is not None and not isinstance ( key_name , basestring ) : <EOL> raise BadKeyError ( '<STR_LIT>' % <EOL> key_name . __class__ . __name__ ) <EOL> if parent is not None : <EOL> if not isinstance ( parent , Model ) : <EOL> raise TypeError ( '<STR_LIT>' % <EOL> ( parent , parent . __class__ . __name__ ) ) <EOL> if not parent . is_saved ( ) : <EOL> raise BadValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % parent . kind ( ) ) <EOL> self . _parent = parent <EOL> self . _entity = None <EOL> self . _key_name = key_name <EOL> self . _app = _app <EOL> properties = self . properties ( ) <EOL> for prop in self . properties ( ) . values ( ) : <EOL> if kwds . has_key ( prop . name ) : <EOL> value = kwds [ prop . name ] <EOL> else : <EOL> value = prop . default_value ( ) <EOL> prop . __set__ ( self , value ) <EOL> def key ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . is_saved ( ) : <EOL> return self . _entity . key ( ) <EOL> else : <EOL> raise NotSavedError ( ) <EOL> def _store_to_entity ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> for prop in self . properties ( ) . values ( ) : <EOL> datastore_value = prop . get_value_for_datastore ( self ) <EOL> if datastore_value == [ ] : <EOL> try : <EOL> del entity [ prop . name ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> entity [ prop . name ] = datastore_value <EOL> def put ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _save_to_entity ( ) <EOL> return datastore . Put ( self . _entity ) <EOL> save = put <EOL> def _save_to_entity ( self , _entity_class = datastore . Entity ) : <EOL> """<STR_LIT>""" <EOL> if self . _entity is None : <EOL> if self . _parent is not None : <EOL> self . _entity = _entity_class ( self . kind ( ) , <EOL> parent = self . _parent . _entity , <EOL> name = self . _key_name , <EOL> _app = self . _app ) <EOL> else : <EOL> self . _entity = _entity_class ( self . kind ( ) , <EOL> name = self . _key_name , <EOL> _app = self . _app ) <EOL> del self . _key_name <EOL> self . _store_to_entity ( self . _entity ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> datastore . Delete ( self . key ( ) ) <EOL> self . _entity = None <EOL> def is_saved ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _entity is not None <EOL> def dynamic_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def instance_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . dynamic_properties ( ) <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . _parent is None and <EOL> self . _entity is not None and <EOL> self . _entity . parent ( ) is not None ) : <EOL> self . _parent = get ( self . _entity . parent ( ) ) <EOL> return self . _parent <EOL> def parent_key ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _parent is not None : <EOL> return self . _parent . key ( ) <EOL> elif self . _entity is not None : <EOL> return self . _entity . parent ( ) <EOL> else : <EOL> return None <EOL> def to_xml ( self , _entity_class = datastore . Entity ) : <EOL> """<STR_LIT>""" <EOL> self . _save_to_entity ( _entity_class ) <EOL> return self . _entity . ToXml ( ) <EOL> @ classmethod <EOL> def get ( cls , keys ) : <EOL> """<STR_LIT>""" <EOL> results = get ( keys ) <EOL> if results is None : <EOL> return None <EOL> if isinstance ( results , Model ) : <EOL> instances = [ results ] <EOL> else : <EOL> instances = results <EOL> for instance in instances : <EOL> if not ( instance is None or isinstance ( instance , cls ) ) : <EOL> raise KindError ( '<STR_LIT>' % <EOL> ( instance . kind ( ) , cls . kind ( ) ) ) <EOL> return results <EOL> @ classmethod <EOL> def get_by_key_name ( cls , key_names , parent = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( parent , Model ) : <EOL> parent = parent . key ( ) <EOL> key_names , multiple = datastore . NormalizeAndTypeCheck ( key_names , basestring ) <EOL> keys = [ datastore . Key . from_path ( cls . kind ( ) , name , parent = parent ) <EOL> for name in key_names ] <EOL> if multiple : <EOL> return get ( keys ) <EOL> else : <EOL> return get ( * keys ) <EOL> @ classmethod <EOL> def get_by_id ( cls , ids , parent = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( parent , Model ) : <EOL> parent = parent . key ( ) <EOL> ids , multiple = datastore . NormalizeAndTypeCheck ( ids , int ) <EOL> keys = [ datastore . Key . from_path ( cls . kind ( ) , id , parent = parent ) <EOL> for id in ids ] <EOL> if multiple : <EOL> return get ( keys ) <EOL> else : <EOL> return get ( * keys ) <EOL> @ classmethod <EOL> def get_or_insert ( cls , key_name , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> def txn ( ) : <EOL> entity = cls . get_by_key_name ( key_name , parent = kwds . get ( '<STR_LIT>' ) ) <EOL> if entity is None : <EOL> entity = cls ( key_name = key_name , ** kwds ) <EOL> entity . put ( ) <EOL> return entity <EOL> return run_in_transaction ( txn ) <EOL> @ classmethod <EOL> def all ( cls ) : <EOL> """<STR_LIT>""" <EOL> return Query ( cls ) <EOL> @ classmethod <EOL> def gql ( cls , query_string , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return GqlQuery ( '<STR_LIT>' % ( cls . kind ( ) , query_string ) , <EOL> * args , ** kwds ) <EOL> @ classmethod <EOL> def _load_entity_values ( cls , entity ) : <EOL> """<STR_LIT>""" <EOL> entity_values = { } <EOL> for prop in cls . properties ( ) . values ( ) : <EOL> if entity . has_key ( prop . name ) : <EOL> try : <EOL> value = prop . make_value_from_datastore ( entity [ prop . name ] ) <EOL> entity_values [ prop . name ] = value <EOL> except KeyError : <EOL> entity_values [ prop . name ] = [ ] <EOL> return entity_values <EOL> @ classmethod <EOL> def from_entity ( cls , entity ) : <EOL> """<STR_LIT>""" <EOL> if cls . kind ( ) != entity . kind ( ) : <EOL> raise KindError ( '<STR_LIT>' % <EOL> ( repr ( cls ) , entity . kind ( ) ) ) <EOL> entity_values = cls . _load_entity_values ( entity ) <EOL> instance = cls ( None , ** entity_values ) <EOL> instance . _entity = entity <EOL> del instance . _key_name <EOL> return instance <EOL> @ classmethod <EOL> def kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . __name__ <EOL> @ classmethod <EOL> def entity_type ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . kind ( ) <EOL> @ classmethod <EOL> def properties ( cls ) : <EOL> """<STR_LIT>""" <EOL> return dict ( cls . _properties ) <EOL> @ classmethod <EOL> def fields ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . properties ( ) <EOL> def get ( keys ) : <EOL> """<STR_LIT>""" <EOL> keys , multiple = datastore . NormalizeAndTypeCheckKeys ( keys ) <EOL> try : <EOL> entities = datastore . Get ( keys ) <EOL> except datastore_errors . EntityNotFoundError : <EOL> assert not multiple <EOL> return None <EOL> models = [ ] <EOL> for entity in entities : <EOL> if entity is None : <EOL> model = None <EOL> else : <EOL> cls1 = class_for_kind ( entity . kind ( ) ) <EOL> model = cls1 . from_entity ( entity ) <EOL> models . append ( model ) <EOL> if multiple : <EOL> return models <EOL> assert len ( models ) == <NUM_LIT:1> <EOL> return models [ <NUM_LIT:0> ] <EOL> def put ( models ) : <EOL> """<STR_LIT>""" <EOL> models , multiple = datastore . NormalizeAndTypeCheck ( models , Model ) <EOL> for model in models : <EOL> model . _save_to_entity ( ) <EOL> entities = [ model . _entity for model in models ] <EOL> keys = datastore . Put ( entities ) <EOL> if multiple : <EOL> return keys <EOL> assert len ( keys ) == <NUM_LIT:1> <EOL> return keys [ <NUM_LIT:0> ] <EOL> save = put <EOL> def delete ( models ) : <EOL> """<STR_LIT>""" <EOL> models , multiple = datastore . NormalizeAndTypeCheck ( models , Model ) <EOL> entities = [ model . key ( ) for model in models ] <EOL> keys = datastore . Delete ( entities ) <EOL> class Expando ( Model ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent = None , key_name = None , _app = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> super ( Expando , self ) . __init__ ( parent , key_name , _app , ** kwds ) <EOL> self . _dynamic_properties = { } <EOL> for prop , value in kwds . iteritems ( ) : <EOL> if prop not in self . properties ( ) and value is not None : <EOL> setattr ( self , prop , value ) <EOL> def __setattr__ ( self , key , value ) : <EOL> """<STR_LIT>""" <EOL> if value == [ ] : <EOL> raise ValueError ( '<STR_LIT>' % key ) <EOL> check_reserved_word ( key ) <EOL> if key [ : <NUM_LIT:1> ] != '<STR_LIT:_>' and key not in self . properties ( ) : <EOL> if type ( value ) not in _ALLOWED_EXPANDO_PROPERTY_TYPES : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> type ( value ) . __name__ ) <EOL> self . _dynamic_properties [ key ] = value <EOL> else : <EOL> Model . __setattr__ ( self , key , value ) <EOL> def __getattr__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _dynamic_properties : <EOL> return self . _dynamic_properties [ key ] <EOL> else : <EOL> return getattr ( super ( Expando , self ) , key ) <EOL> def __delattr__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key in self . _dynamic_properties : <EOL> del self . _dynamic_properties [ key ] <EOL> else : <EOL> object . __delattr__ ( self , key ) <EOL> def dynamic_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dynamic_properties . keys ( ) <EOL> def _store_to_entity ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> super ( Expando , self ) . _store_to_entity ( entity ) <EOL> for key , value in self . _dynamic_properties . iteritems ( ) : <EOL> entity [ key ] = value <EOL> all_properties = set ( self . _dynamic_properties . iterkeys ( ) ) <EOL> all_properties . update ( self . properties ( ) . iterkeys ( ) ) <EOL> for key in entity . keys ( ) : <EOL> if key not in all_properties : <EOL> del entity [ key ] <EOL> @ classmethod <EOL> def _load_entity_values ( cls , entity ) : <EOL> """<STR_LIT>""" <EOL> entity_values = Model . _load_entity_values ( entity ) <EOL> for key , value in entity . iteritems ( ) : <EOL> if key not in entity_values : <EOL> entity_values [ str ( key ) ] = value <EOL> return entity_values <EOL> class _BaseQuery ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_class ) : <EOL> """<STR_LIT>""" <EOL> self . _model_class = model_class <EOL> def _get_query ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> return _QueryIterator ( self . _model_class , iter ( self . _get_query ( ) . Run ( ) ) ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . run ( ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> iterator = self . run ( ) <EOL> try : <EOL> return iterator . next ( ) <EOL> except StopIteration : <EOL> return None <EOL> def count ( self , limit = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _get_query ( ) . Count ( limit = limit ) <EOL> def fetch ( self , limit , offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> accepted = ( int , long ) <EOL> if not ( isinstance ( limit , accepted ) and isinstance ( offset , accepted ) ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if limit < <NUM_LIT:0> or offset < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if limit == <NUM_LIT:0> : <EOL> return [ ] <EOL> raw = self . _get_query ( ) . Get ( offset + limit ) <EOL> if offset : <EOL> del raw [ : offset ] <EOL> return map ( self . _model_class . from_entity , raw ) <EOL> def __getitem__ ( self , arg ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( arg , slice ) : <EOL> start , stop , step = arg . start , arg . stop , arg . step <EOL> if start is None : <EOL> start = <NUM_LIT:0> <EOL> if stop is None : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if step is None : <EOL> step = <NUM_LIT:1> <EOL> if start < <NUM_LIT:0> or stop < <NUM_LIT:0> or step != <NUM_LIT:1> : <EOL> raise ValueError ( <EOL> '<STR_LIT>' ) <EOL> limit = stop - start <EOL> if limit < <NUM_LIT:0> : <EOL> return [ ] <EOL> return self . fetch ( limit , start ) <EOL> elif isinstance ( arg , ( int , long ) ) : <EOL> if arg < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> results = self . fetch ( <NUM_LIT:1> , arg ) <EOL> if results : <EOL> return results [ <NUM_LIT:0> ] <EOL> else : <EOL> raise IndexError ( '<STR_LIT>' % ( arg + <NUM_LIT:1> ) ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> class _QueryIterator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_class , datastore_iterator ) : <EOL> """<STR_LIT>""" <EOL> self . __model_class = model_class <EOL> self . __iterator = datastore_iterator <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __model_class . from_entity ( self . __iterator . next ( ) ) <EOL> class Query ( _BaseQuery ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_class ) : <EOL> """<STR_LIT>""" <EOL> super ( Query , self ) . __init__ ( model_class ) <EOL> self . __query_set = { } <EOL> self . __orderings = [ ] <EOL> self . __ancestor = None <EOL> def _get_query ( self , _query_class = datastore . Query ) : <EOL> query = _query_class ( self . _model_class . kind ( ) , self . __query_set ) <EOL> if self . __ancestor is not None : <EOL> query . Ancestor ( self . __ancestor ) <EOL> query . Order ( * self . __orderings ) <EOL> return query <EOL> def filter ( self , property_operator , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , Model ) : <EOL> value = value . key ( ) <EOL> self . __query_set [ property_operator ] = value <EOL> return self <EOL> def order ( self , property ) : <EOL> """<STR_LIT>""" <EOL> if property . startswith ( '<STR_LIT:->' ) : <EOL> property = property [ <NUM_LIT:1> : ] <EOL> order = datastore . Query . DESCENDING <EOL> else : <EOL> order = datastore . Query . ASCENDING <EOL> properties = self . _model_class . properties ( ) <EOL> if not properties . has_key ( property ) : <EOL> raise PropertyError ( '<STR_LIT>' % property ) <EOL> prop = properties [ property ] <EOL> self . __orderings . append ( ( property , order ) ) <EOL> return self <EOL> def ancestor ( self , ancestor ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( ancestor , datastore . Key ) : <EOL> if ancestor . has_id_or_name ( ) : <EOL> self . __ancestor = ancestor <EOL> else : <EOL> raise NotSavedError ( ) <EOL> elif isinstance ( ancestor , Model ) : <EOL> if ancestor . is_saved ( ) : <EOL> self . __ancestor = ancestor . key ( ) <EOL> else : <EOL> raise NotSavedError ( ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> return self <EOL> class GqlQuery ( _BaseQuery ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , query_string , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import gql <EOL> app = kwds . pop ( '<STR_LIT>' , None ) <EOL> self . _proto_query = gql . GQL ( query_string , _app = app ) <EOL> super ( GqlQuery , self ) . __init__ ( class_for_kind ( self . _proto_query . _entity ) ) <EOL> self . bind ( * args , ** kwds ) <EOL> def bind ( self , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> self . _args = [ ] <EOL> for arg in args : <EOL> if isinstance ( arg , Model ) : <EOL> arg = arg . key ( ) <EOL> self . _args . append ( arg ) <EOL> self . _kwds = { } <EOL> for name , arg in kwds . iteritems ( ) : <EOL> if isinstance ( arg , Model ) : <EOL> arg = arg . key ( ) <EOL> self . _kwds [ name ] = arg <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> query_run = self . _proto_query . Run ( * self . _args , ** self . _kwds ) <EOL> return _QueryIterator ( self . _model_class , iter ( query_run ) ) <EOL> def _get_query ( self ) : <EOL> return self . _proto_query . Bind ( self . _args , self . _kwds ) <EOL> class TextProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None and not isinstance ( value , Text ) : <EOL> try : <EOL> value = Text ( value ) <EOL> except TypeError , err : <EOL> raise BadValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . name , err ) ) <EOL> value = super ( TextProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , Text ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = Text <EOL> class StringProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , multiline = False , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> super ( StringProperty , self ) . __init__ ( verbose_name , ** kwds ) <EOL> self . multiline = multiline <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( StringProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , basestring ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' <EOL> % ( self . name , type ( value ) . __name__ ) ) <EOL> if not self . multiline and value and value . find ( '<STR_LIT:\n>' ) != - <NUM_LIT:1> : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = basestring <EOL> class _CoercingProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( _CoercingProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , self . data_type ) : <EOL> value = self . data_type ( value ) <EOL> return value <EOL> class CategoryProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = Category <EOL> class LinkProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> value = super ( LinkProperty , self ) . validate ( value ) <EOL> if value is not None : <EOL> scheme , netloc , path , query , fragment = urlparse . urlsplit ( value ) <EOL> if not scheme or not netloc : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , value ) ) <EOL> return value <EOL> data_type = Link <EOL> URLProperty = LinkProperty <EOL> class EmailProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = Email <EOL> class GeoPtProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = GeoPt <EOL> class IMProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = IM <EOL> class PhoneNumberProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = PhoneNumber <EOL> class PostalAddressProperty ( _CoercingProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = PostalAddress <EOL> class BlobProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None and not isinstance ( value , Blob ) : <EOL> try : <EOL> value = Blob ( value ) <EOL> except TypeError , err : <EOL> raise BadValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . name , err ) ) <EOL> value = super ( BlobProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , Blob ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = Blob <EOL> class DateTimeProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , auto_now = False , auto_now_add = False , <EOL> ** kwds ) : <EOL> """<STR_LIT>""" <EOL> super ( DateTimeProperty , self ) . __init__ ( verbose_name , ** kwds ) <EOL> self . auto_now = auto_now <EOL> self . auto_now_add = auto_now_add <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( DateTimeProperty , self ) . validate ( value ) <EOL> if value and not isinstance ( value , self . data_type ) : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , self . data_type . __name__ ) ) <EOL> return value <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . auto_now or self . auto_now_add : <EOL> return self . now ( ) <EOL> return Property . default_value ( self ) <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> if self . auto_now : <EOL> return self . now ( ) <EOL> else : <EOL> return super ( DateTimeProperty , <EOL> self ) . get_value_for_datastore ( model_instance ) <EOL> data_type = datetime . datetime <EOL> @ staticmethod <EOL> def now ( ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . now ( ) <EOL> class DateProperty ( DateTimeProperty ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def now ( ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . now ( ) . date ( ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( DateProperty , self ) . validate ( value ) <EOL> if isinstance ( value , datetime . datetime ) : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , self . data_type . __name__ ) ) <EOL> return value <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> value = super ( DateProperty , self ) . get_value_for_datastore ( model_instance ) <EOL> if value is not None : <EOL> assert isinstance ( value , datetime . date ) <EOL> value = datetime . datetime ( value . year , value . month , value . day ) <EOL> return value <EOL> def make_value_from_datastore ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None : <EOL> assert isinstance ( value , datetime . datetime ) <EOL> value = value . date ( ) <EOL> return value <EOL> data_type = datetime . date <EOL> class TimeProperty ( DateTimeProperty ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def now ( ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . now ( ) . time ( ) <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> value = super ( TimeProperty , self ) . get_value_for_datastore ( model_instance ) <EOL> if value is not None : <EOL> assert isinstance ( value , datetime . time ) , repr ( value ) <EOL> value = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> value . hour , value . minute , value . second , <EOL> value . microsecond ) <EOL> return value <EOL> def make_value_from_datastore ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is not None : <EOL> assert isinstance ( value , datetime . datetime ) <EOL> value = value . time ( ) <EOL> return value <EOL> data_type = datetime . time <EOL> class IntegerProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( IntegerProperty , self ) . validate ( value ) <EOL> if value is None : <EOL> return value <EOL> if not isinstance ( value , ( int , long ) ) or isinstance ( value , bool ) : <EOL> raise BadValueError ( '<STR_LIT>' <EOL> % ( self . name , type ( value ) . __name__ ) ) <EOL> if value < - <NUM_LIT> or value > <NUM_LIT> : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = int <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value is None <EOL> class RatingProperty ( _CoercingProperty , IntegerProperty ) : <EOL> """<STR_LIT>""" <EOL> data_type = Rating <EOL> class FloatProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( FloatProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , float ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = float <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value is None <EOL> class BooleanProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( BooleanProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , bool ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = bool <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value is None <EOL> class UserProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , verbose_name = None , name = None , <EOL> required = False , validator = None , choices = None ) : <EOL> """<STR_LIT>""" <EOL> super ( UserProperty , self ) . __init__ ( verbose_name , name , <EOL> required = required , <EOL> validator = validator , <EOL> choices = choices ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( UserProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , users . User ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> return value <EOL> data_type = users . User <EOL> class ListProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , item_type , verbose_name = None , default = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( item_type , type ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if item_type not in _ALLOWED_PROPERTY_TYPES : <EOL> raise ValueError ( '<STR_LIT>' % item_type . __name__ ) <EOL> if '<STR_LIT>' in kwds and kwds [ '<STR_LIT>' ] is not True : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if default is None : <EOL> default = [ ] <EOL> self . item_type = item_type <EOL> super ( ListProperty , self ) . __init__ ( verbose_name , <EOL> required = True , <EOL> default = default , <EOL> ** kwds ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = super ( ListProperty , self ) . validate ( value ) <EOL> if value is not None : <EOL> if not isinstance ( value , list ) : <EOL> raise BadValueError ( '<STR_LIT>' % self . name ) <EOL> for item in value : <EOL> if not isinstance ( item , self . item_type ) : <EOL> raise BadValueError ( '<STR_LIT>' % <EOL> ( self . name , self . item_type . __name__ ) ) <EOL> return value <EOL> def empty ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value is None <EOL> data_type = list <EOL> def default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( super ( ListProperty , self ) . default_value ( ) ) <EOL> def StringListProperty ( verbose_name = None , default = None , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> return ListProperty ( basestring , verbose_name , default , ** kwds ) <EOL> class ReferenceProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> reference_class = None , <EOL> verbose_name = None , <EOL> collection_name = None , <EOL> ** attrs ) : <EOL> """<STR_LIT>""" <EOL> super ( ReferenceProperty , self ) . __init__ ( verbose_name , ** attrs ) <EOL> self . collection_name = collection_name <EOL> if reference_class is None : <EOL> reference_class = Model <EOL> if not ( ( isinstance ( reference_class , type ) and <EOL> issubclass ( reference_class , Model ) ) or <EOL> reference_class is _SELF_REFERENCE ) : <EOL> raise KindError ( '<STR_LIT>' ) <EOL> self . reference_class = self . data_type = reference_class <EOL> def __property_config__ ( self , model_class , property_name ) : <EOL> """<STR_LIT>""" <EOL> super ( ReferenceProperty , self ) . __property_config__ ( model_class , <EOL> property_name ) <EOL> if self . reference_class is _SELF_REFERENCE : <EOL> self . reference_class = self . data_type = model_class <EOL> if self . collection_name is None : <EOL> self . collection_name = '<STR_LIT>' % ( model_class . __name__ . lower ( ) ) <EOL> if hasattr ( self . reference_class , self . collection_name ) : <EOL> raise DuplicatePropertyError ( '<STR_LIT>' <EOL> % ( self . reference_class . __name__ , <EOL> self . collection_name ) ) <EOL> setattr ( self . reference_class , <EOL> self . collection_name , <EOL> _ReverseReferenceProperty ( model_class , property_name ) ) <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> """<STR_LIT>""" <EOL> if model_instance is None : <EOL> return self <EOL> if hasattr ( model_instance , self . __id_attr_name ( ) ) : <EOL> reference_id = getattr ( model_instance , self . __id_attr_name ( ) ) <EOL> else : <EOL> reference_id = None <EOL> if reference_id is not None : <EOL> resolved = getattr ( model_instance , self . __resolved_attr_name ( ) ) <EOL> if resolved : <EOL> return resolved <EOL> else : <EOL> instance = get ( reference_id ) <EOL> if instance is None : <EOL> raise Error ( '<STR_LIT>' ) <EOL> setattr ( model_instance , self . __resolved_attr_name ( ) , instance ) <EOL> return instance <EOL> else : <EOL> return None <EOL> def __set__ ( self , model_instance , value ) : <EOL> """<STR_LIT>""" <EOL> value = self . validate ( value ) <EOL> if value is not None : <EOL> if isinstance ( value , datastore . Key ) : <EOL> setattr ( model_instance , self . __id_attr_name ( ) , value ) <EOL> setattr ( model_instance , self . __resolved_attr_name ( ) , None ) <EOL> else : <EOL> setattr ( model_instance , self . __id_attr_name ( ) , value . key ( ) ) <EOL> setattr ( model_instance , self . __resolved_attr_name ( ) , value ) <EOL> else : <EOL> setattr ( model_instance , self . __id_attr_name ( ) , None ) <EOL> setattr ( model_instance , self . __resolved_attr_name ( ) , None ) <EOL> def get_value_for_datastore ( self , model_instance ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( model_instance , self . __id_attr_name ( ) ) <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , datastore . Key ) : <EOL> return value <EOL> if value is not None and not value . is_saved ( ) : <EOL> raise BadValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % self . reference_class . kind ( ) ) <EOL> value = super ( ReferenceProperty , self ) . validate ( value ) <EOL> if value is not None and not isinstance ( value , self . reference_class ) : <EOL> raise KindError ( '<STR_LIT>' % <EOL> ( self . name , self . reference_class . kind ( ) ) ) <EOL> return value <EOL> def __id_attr_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _attr_name ( ) <EOL> def __resolved_attr_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' + self . _attr_name ( ) <EOL> Reference = ReferenceProperty <EOL> def SelfReferenceProperty ( verbose_name = None , collection_name = None , ** attrs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in attrs : <EOL> raise ConfigurationError ( <EOL> '<STR_LIT>' ) <EOL> return ReferenceProperty ( _SELF_REFERENCE , <EOL> verbose_name , <EOL> collection_name , <EOL> ** attrs ) <EOL> SelfReference = SelfReferenceProperty <EOL> class _ReverseReferenceProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , prop ) : <EOL> """<STR_LIT>""" <EOL> self . __model = model <EOL> self . __property = prop <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> """<STR_LIT>""" <EOL> if model_instance is not None : <EOL> query = Query ( self . __model ) <EOL> return query . filter ( self . __property + '<STR_LIT>' , model_instance . key ( ) ) <EOL> else : <EOL> return self <EOL> def __set__ ( self , model_instance , value ) : <EOL> """<STR_LIT>""" <EOL> raise BadValueError ( '<STR_LIT>' ) <EOL> run_in_transaction = datastore . RunInTransaction <EOL> RunInTransaction = run_in_transaction </s>
