<s> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> _class_map = { } <EOL> _CLASS_KEY_PROPERTY = '<STR_LIT:class>' <EOL> class _ClassKeyProperty ( db . ListProperty ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> super ( _ClassKeyProperty , self ) . __init__ ( name = name , <EOL> item_type = str , <EOL> default = None ) <EOL> def __set__ ( self , * args ) : <EOL> raise db . DerivedPropertyError ( <EOL> '<STR_LIT>' ) <EOL> def __get__ ( self , model_instance , model_class ) : <EOL> if model_instance is None : <EOL> return self <EOL> return [ cls . __name__ for cls in model_class . __class_hierarchy__ ] <EOL> class PolymorphicClass ( db . PropertiedClass ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> """<STR_LIT>""" <EOL> if name == '<STR_LIT>' : <EOL> super ( PolymorphicClass , cls ) . __init__ ( name , bases , dct , map_kind = False ) <EOL> return <EOL> elif PolyModel in bases : <EOL> if getattr ( cls , '<STR_LIT>' , None ) : <EOL> raise db . ConfigurationError ( ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % cls . __name__ ) <EOL> cls . __class_hierarchy__ = [ cls ] <EOL> cls . __root_class__ = cls <EOL> super ( PolymorphicClass , cls ) . __init__ ( name , bases , dct ) <EOL> else : <EOL> super ( PolymorphicClass , cls ) . __init__ ( name , bases , dct , map_kind = False ) <EOL> cls . __class_hierarchy__ = [ c for c in reversed ( cls . mro ( ) ) <EOL> if issubclass ( c , PolyModel ) and c != PolyModel ] <EOL> if cls . __class_hierarchy__ [ <NUM_LIT:0> ] != cls . __root_class__ : <EOL> raise db . ConfigurationError ( <EOL> '<STR_LIT>' % <EOL> ( cls . __name__ , <EOL> cls . __class_hierarchy__ [ <NUM_LIT:0> ] . __name__ , <EOL> cls . __root_class__ . __name__ ) ) <EOL> _class_map [ cls . class_key ( ) ] = cls <EOL> class PolyModel ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = PolymorphicClass <EOL> _class = _ClassKeyProperty ( name = _CLASS_KEY_PROPERTY ) <EOL> def __new__ ( cls , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if cls is PolyModel : <EOL> raise NotImplementedError ( ) <EOL> return super ( PolyModel , cls ) . __new__ ( cls , * args , ** kwds ) <EOL> @ classmethod <EOL> def kind ( cls ) : <EOL> """<STR_LIT>""" <EOL> if cls is cls . __root_class__ : <EOL> return super ( PolyModel , cls ) . kind ( ) <EOL> else : <EOL> return cls . __root_class__ . kind ( ) <EOL> @ classmethod <EOL> def class_key ( cls ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( cls , '<STR_LIT>' ) : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' ) <EOL> return tuple ( cls . class_name ( ) for cls in cls . __class_hierarchy__ ) <EOL> @ classmethod <EOL> def class_name ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . __name__ <EOL> @ classmethod <EOL> def from_entity ( cls , entity ) : <EOL> """<STR_LIT>""" <EOL> if ( _CLASS_KEY_PROPERTY in entity and <EOL> tuple ( entity [ _CLASS_KEY_PROPERTY ] ) != cls . class_key ( ) ) : <EOL> key = tuple ( entity [ _CLASS_KEY_PROPERTY ] ) <EOL> try : <EOL> poly_class = _class_map [ key ] <EOL> except KeyError : <EOL> raise db . KindError ( '<STR_LIT>' % key ) <EOL> return poly_class . from_entity ( entity ) <EOL> return super ( PolyModel , cls ) . from_entity ( entity ) <EOL> @ classmethod <EOL> def all ( cls , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> query = super ( PolyModel , cls ) . all ( ** kwds ) <EOL> if cls != cls . __root_class__ : <EOL> query . filter ( _CLASS_KEY_PROPERTY + '<STR_LIT>' , cls . class_name ( ) ) <EOL> return query <EOL> @ classmethod <EOL> def gql ( cls , query_string , * args , ** kwds ) : <EOL> """<STR_LIT>""" <EOL> if cls == cls . __root_class__ : <EOL> return super ( PolyModel , cls ) . gql ( query_string , * args , ** kwds ) <EOL> else : <EOL> from google . appengine . ext import gql <EOL> query = db . GqlQuery ( '<STR_LIT>' % ( cls . kind ( ) , query_string ) ) <EOL> query_filter = [ ( '<STR_LIT>' , <EOL> [ gql . Literal ( cls . class_name ( ) ) ] ) ] <EOL> query . _proto_query . filters ( ) [ ( '<STR_LIT:class>' , '<STR_LIT:=>' ) ] = query_filter <EOL> query . bind ( * args , ** kwds ) <EOL> return query </s>
