<s> import logging <EOL> import re <EOL> import six <EOL> from warnings import warn <EOL> from cassandra . cqlengine import CQLEngineException , ValidationError <EOL> from cassandra . cqlengine import columns <EOL> from cassandra . cqlengine import connection <EOL> from cassandra . cqlengine import query <EOL> from cassandra . cqlengine . query import DoesNotExist as _DoesNotExist <EOL> from cassandra . cqlengine . query import MultipleObjectsReturned as _MultipleObjectsReturned <EOL> from cassandra . metadata import protect_name <EOL> from cassandra . util import OrderedDict <EOL> log = logging . getLogger ( __name__ ) <EOL> class ModelException ( CQLEngineException ) : <EOL> pass <EOL> class ModelDefinitionException ( ModelException ) : <EOL> pass <EOL> class PolymorphicModelException ( ModelException ) : <EOL> pass <EOL> class UndefinedKeyspaceWarning ( Warning ) : <EOL> pass <EOL> DEFAULT_KEYSPACE = None <EOL> class hybrid_classmethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , clsmethod , instmethod ) : <EOL> self . clsmethod = clsmethod <EOL> self . instmethod = instmethod <EOL> def __get__ ( self , instance , owner ) : <EOL> if instance is None : <EOL> return self . clsmethod . __get__ ( owner , owner ) <EOL> else : <EOL> return self . instmethod . __get__ ( instance , owner ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class QuerySetDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , obj , model ) : <EOL> """<STR_LIT>""" <EOL> if model . __abstract__ : <EOL> raise CQLEngineException ( '<STR_LIT>' ) <EOL> queryset = model . __queryset__ ( model ) <EOL> if model . _is_polymorphic and not model . _is_polymorphic_base : <EOL> name , column = model . _discriminator_column_name , model . _discriminator_column <EOL> if column . partition_key or column . index : <EOL> return queryset . filter ( ** { name : model . __discriminator_value__ } ) <EOL> return queryset <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class ConditionalDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def conditional_setter ( * prepared_conditional , ** unprepared_conditionals ) : <EOL> if len ( prepared_conditional ) > <NUM_LIT:0> : <EOL> conditionals = prepared_conditional [ <NUM_LIT:0> ] <EOL> else : <EOL> conditionals = instance . objects . iff ( ** unprepared_conditionals ) . _conditional <EOL> instance . _conditional = conditionals <EOL> return instance <EOL> return conditional_setter <EOL> qs = model . __queryset__ ( model ) <EOL> def conditional_setter ( ** unprepared_conditionals ) : <EOL> conditionals = model . objects . iff ( ** unprepared_conditionals ) . _conditional <EOL> qs . _conditional = conditionals <EOL> return qs <EOL> return conditional_setter <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class TTLDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def ttl_setter ( ts ) : <EOL> instance . _ttl = ts <EOL> return instance <EOL> return ttl_setter <EOL> qs = model . __queryset__ ( model ) <EOL> def ttl_setter ( ts ) : <EOL> qs . _ttl = ts <EOL> return qs <EOL> return ttl_setter <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class TimestampDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def timestamp_setter ( ts ) : <EOL> instance . _timestamp = ts <EOL> return instance <EOL> return timestamp_setter <EOL> return model . objects . timestamp <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class IfNotExistsDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def ifnotexists_setter ( ife = True ) : <EOL> instance . _if_not_exists = ife <EOL> return instance <EOL> return ifnotexists_setter <EOL> return model . objects . if_not_exists <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class IfExistsDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def ifexists_setter ( ife = True ) : <EOL> instance . _if_exists = ife <EOL> return instance <EOL> return ifexists_setter <EOL> return model . objects . if_exists <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class ConsistencyDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __get__ ( self , instance , model ) : <EOL> if instance : <EOL> def consistency_setter ( consistency ) : <EOL> instance . __consistency__ = consistency <EOL> return instance <EOL> return consistency_setter <EOL> qs = model . __queryset__ ( model ) <EOL> def consistency_setter ( consistency ) : <EOL> qs . _consistency = consistency <EOL> return qs <EOL> return consistency_setter <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError <EOL> class ColumnQueryEvaluator ( query . AbstractQueryableColumn ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , column ) : <EOL> self . column = column <EOL> def __unicode__ ( self ) : <EOL> return self . column . db_field_name <EOL> def _get_column ( self ) : <EOL> return self . column <EOL> class ColumnDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , column ) : <EOL> """<STR_LIT>""" <EOL> self . column = column <EOL> self . query_evaluator = ColumnQueryEvaluator ( self . column ) <EOL> def __get__ ( self , instance , owner ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return instance . _values [ self . column . column_name ] . getval ( ) <EOL> except AttributeError : <EOL> return self . query_evaluator <EOL> def __set__ ( self , instance , value ) : <EOL> """<STR_LIT>""" <EOL> if instance : <EOL> return instance . _values [ self . column . column_name ] . setval ( value ) <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> def __delete__ ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance : <EOL> if self . column . can_delete : <EOL> instance . _values [ self . column . column_name ] . delval ( ) <EOL> else : <EOL> raise AttributeError ( '<STR_LIT>' . format ( self . column . column_name ) ) <EOL> class BaseModel ( object ) : <EOL> """<STR_LIT>""" <EOL> class DoesNotExist ( _DoesNotExist ) : <EOL> pass <EOL> class MultipleObjectsReturned ( _MultipleObjectsReturned ) : <EOL> pass <EOL> objects = QuerySetDescriptor ( ) <EOL> ttl = TTLDescriptor ( ) <EOL> consistency = ConsistencyDescriptor ( ) <EOL> iff = ConditionalDescriptor ( ) <EOL> timestamp = TimestampDescriptor ( ) <EOL> if_not_exists = IfNotExistsDescriptor ( ) <EOL> if_exists = IfExistsDescriptor ( ) <EOL> __table_name__ = None <EOL> __table_name_case_sensitive__ = False <EOL> __keyspace__ = None <EOL> __discriminator_value__ = None <EOL> __options__ = None <EOL> __compute_routing_key__ = True <EOL> __queryset__ = query . ModelQuerySet <EOL> __dmlquery__ = query . DMLQuery <EOL> __consistency__ = None <EOL> _timestamp = None <EOL> _if_not_exists = False <EOL> _if_exists = False <EOL> _table_name = None <EOL> def __init__ ( self , ** values ) : <EOL> self . _ttl = self . __default_ttl__ <EOL> self . _timestamp = None <EOL> self . _conditional = None <EOL> self . _batch = None <EOL> self . _timeout = connection . NOT_SET <EOL> self . _is_persisted = False <EOL> self . _values = { } <EOL> for name , column in self . _columns . items ( ) : <EOL> value = values . get ( name ) <EOL> if value is not None or isinstance ( column , columns . BaseContainerColumn ) : <EOL> value = column . to_python ( value ) <EOL> value_mngr = column . value_manager ( self , column , value ) <EOL> value_mngr . explicit = name in values <EOL> self . _values [ name ] = value_mngr <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( k , getattr ( self , k ) ) <EOL> for k in self . _defined_columns . keys ( ) <EOL> if k != self . _discriminator_column_name ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( self . __class__ . __name__ , <EOL> '<STR_LIT:U+002CU+0020>' . join ( '<STR_LIT>' . format ( k , getattr ( self , k ) ) for k in self . _primary_keys . keys ( ) ) ) <EOL> @ classmethod <EOL> def _routing_key_from_values ( cls , pk_values , protocol_version ) : <EOL> return cls . _key_serializer ( pk_values , protocol_version ) <EOL> @ classmethod <EOL> def _discover_polymorphic_submodels ( cls ) : <EOL> if not cls . _is_polymorphic_base : <EOL> raise ModelException ( '<STR_LIT>' ) <EOL> def _discover ( klass ) : <EOL> if not klass . _is_polymorphic_base and klass . __discriminator_value__ is not None : <EOL> cls . _discriminator_map [ klass . __discriminator_value__ ] = klass <EOL> for subklass in klass . __subclasses__ ( ) : <EOL> _discover ( subklass ) <EOL> _discover ( cls ) <EOL> @ classmethod <EOL> def _get_model_by_discriminator_value ( cls , key ) : <EOL> if not cls . _is_polymorphic_base : <EOL> raise ModelException ( '<STR_LIT>' ) <EOL> return cls . _discriminator_map . get ( key ) <EOL> @ classmethod <EOL> def _construct_instance ( cls , values ) : <EOL> """<STR_LIT>""" <EOL> if cls . _db_map : <EOL> values = dict ( ( cls . _db_map . get ( k , k ) , v ) for k , v in values . items ( ) ) <EOL> if cls . _is_polymorphic : <EOL> disc_key = values . get ( cls . _discriminator_column_name ) <EOL> if disc_key is None : <EOL> raise PolymorphicModelException ( '<STR_LIT>' ) <EOL> poly_base = cls if cls . _is_polymorphic_base else cls . _polymorphic_base <EOL> klass = poly_base . _get_model_by_discriminator_value ( disc_key ) <EOL> if klass is None : <EOL> poly_base . _discover_polymorphic_submodels ( ) <EOL> klass = poly_base . _get_model_by_discriminator_value ( disc_key ) <EOL> if klass is None : <EOL> raise PolymorphicModelException ( <EOL> '<STR_LIT>' . format ( disc_key , poly_base . __name__ ) <EOL> ) <EOL> if not issubclass ( klass , cls ) : <EOL> raise PolymorphicModelException ( <EOL> '<STR_LIT>' . format ( klass . __name__ , cls . __name__ ) <EOL> ) <EOL> values = dict ( ( k , v ) for k , v in values . items ( ) if k in klass . _columns . keys ( ) ) <EOL> else : <EOL> klass = cls <EOL> instance = klass ( ** values ) <EOL> instance . _set_persisted ( ) <EOL> return instance <EOL> def _set_persisted ( self ) : <EOL> for v in self . _values . values ( ) : <EOL> v . reset_previous_value ( ) <EOL> self . _is_persisted = True <EOL> def _can_update ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _is_persisted : <EOL> return False <EOL> return all ( [ not self . _values [ k ] . changed for k in self . _primary_keys ] ) <EOL> @ classmethod <EOL> def _get_keyspace ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . __keyspace__ or DEFAULT_KEYSPACE <EOL> @ classmethod <EOL> def _get_column ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> return cls . _columns [ name ] <EOL> @ classmethod <EOL> def _get_column_by_db_name ( cls , name ) : <EOL> """<STR_LIT>""" <EOL> return cls . _columns . get ( cls . _db_map . get ( name , name ) ) <EOL> def __eq__ ( self , other ) : <EOL> if self . __class__ != other . __class__ : <EOL> return False <EOL> keys = set ( self . _columns . keys ( ) ) <EOL> other_keys = set ( other . _columns . keys ( ) ) <EOL> if keys != other_keys : <EOL> return False <EOL> return all ( getattr ( self , key , None ) == getattr ( other , key , None ) for key in other_keys ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ classmethod <EOL> def column_family_name ( cls , include_keyspace = True ) : <EOL> """<STR_LIT>""" <EOL> cf_name = protect_name ( cls . _raw_column_family_name ( ) ) <EOL> if include_keyspace : <EOL> keyspace = cls . _get_keyspace ( ) <EOL> if not keyspace : <EOL> raise CQLEngineException ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' . format ( protect_name ( keyspace ) , cf_name ) <EOL> return cf_name <EOL> @ classmethod <EOL> def _raw_column_family_name ( cls ) : <EOL> if not cls . _table_name : <EOL> if cls . __table_name__ : <EOL> if cls . __table_name_case_sensitive__ : <EOL> cls . _table_name = cls . __table_name__ <EOL> else : <EOL> table_name = cls . __table_name__ . lower ( ) <EOL> if cls . __table_name__ != table_name : <EOL> warn ( ( "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( cls . __name__ ) ) <EOL> cls . _table_name = table_name <EOL> else : <EOL> if cls . _is_polymorphic and not cls . _is_polymorphic_base : <EOL> cls . _table_name = cls . _polymorphic_base . _raw_column_family_name ( ) <EOL> else : <EOL> camelcase = re . compile ( r'<STR_LIT>' ) <EOL> ccase = lambda s : camelcase . sub ( lambda v : '<STR_LIT>' . format ( v . group ( <NUM_LIT:1> ) , v . group ( <NUM_LIT:2> ) . lower ( ) ) , s ) <EOL> cf_name = ccase ( cls . __name__ ) <EOL> cf_name = cf_name [ - <NUM_LIT> : ] <EOL> cf_name = cf_name . lower ( ) <EOL> cf_name = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , cf_name ) <EOL> cls . _table_name = cf_name <EOL> return cls . _table_name <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> for name , col in self . _columns . items ( ) : <EOL> v = getattr ( self , name ) <EOL> if v is None and not self . _values [ name ] . explicit and col . has_default : <EOL> v = col . get_default ( ) <EOL> val = col . validate ( v ) <EOL> setattr ( self , name , val ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for column_id in self . _columns . keys ( ) : <EOL> yield column_id <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( key , six . string_types ) : <EOL> raise TypeError <EOL> if key not in self . _columns . keys ( ) : <EOL> raise KeyError <EOL> return getattr ( self , key ) <EOL> def __setitem__ ( self , key , val ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( key , six . string_types ) : <EOL> raise TypeError <EOL> if key not in self . _columns . keys ( ) : <EOL> raise KeyError <EOL> return setattr ( self , key , val ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _len <EOL> except : <EOL> self . _len = len ( self . _columns . keys ( ) ) <EOL> return self . _len <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ k for k in self ] <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self [ k ] for k in self ] <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ( k , self [ k ] ) for k in self ] <EOL> def _as_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> values = self . _dynamic_columns or { } <EOL> for name , col in self . _columns . items ( ) : <EOL> values [ name ] = col . to_database ( getattr ( self , name , None ) ) <EOL> return values <EOL> @ classmethod <EOL> def create ( cls , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> extra_columns = set ( kwargs . keys ( ) ) - set ( cls . _columns . keys ( ) ) <EOL> if extra_columns : <EOL> raise ValidationError ( "<STR_LIT>" . format ( extra_columns ) ) <EOL> return cls . objects . create ( ** kwargs ) <EOL> @ classmethod <EOL> def all ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . objects . all ( ) <EOL> @ classmethod <EOL> def filter ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return cls . objects . filter ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def get ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return cls . objects . get ( * args , ** kwargs ) <EOL> def timeout ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> assert self . _batch is None , '<STR_LIT>' <EOL> self . _timeout = timeout <EOL> return self <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _is_polymorphic : <EOL> if self . _is_polymorphic_base : <EOL> raise PolymorphicModelException ( '<STR_LIT>' ) <EOL> else : <EOL> setattr ( self , self . _discriminator_column_name , self . __discriminator_value__ ) <EOL> self . validate ( ) <EOL> self . __dmlquery__ ( self . __class__ , self , <EOL> batch = self . _batch , <EOL> ttl = self . _ttl , <EOL> timestamp = self . _timestamp , <EOL> consistency = self . __consistency__ , <EOL> if_not_exists = self . _if_not_exists , <EOL> conditional = self . _conditional , <EOL> timeout = self . _timeout , <EOL> if_exists = self . _if_exists ) . save ( ) <EOL> self . _set_persisted ( ) <EOL> self . _ttl = self . __default_ttl__ <EOL> self . _timestamp = None <EOL> return self <EOL> def update ( self , ** values ) : <EOL> """<STR_LIT>""" <EOL> for k , v in values . items ( ) : <EOL> col = self . _columns . get ( k ) <EOL> if col is None : <EOL> raise ValidationError ( "<STR_LIT>" . format ( self . __module__ , self . __class__ . __name__ , k ) ) <EOL> if col . is_primary_key : <EOL> raise ValidationError ( "<STR_LIT>" . format ( k , self . __module__ , self . __class__ . __name__ ) ) <EOL> setattr ( self , k , v ) <EOL> if self . _is_polymorphic : <EOL> if self . _is_polymorphic_base : <EOL> raise PolymorphicModelException ( '<STR_LIT>' ) <EOL> else : <EOL> setattr ( self , self . _discriminator_column_name , self . __discriminator_value__ ) <EOL> self . validate ( ) <EOL> self . __dmlquery__ ( self . __class__ , self , <EOL> batch = self . _batch , <EOL> ttl = self . _ttl , <EOL> timestamp = self . _timestamp , <EOL> consistency = self . __consistency__ , <EOL> conditional = self . _conditional , <EOL> timeout = self . _timeout , <EOL> if_exists = self . _if_exists ) . update ( ) <EOL> self . _set_persisted ( ) <EOL> self . _ttl = self . __default_ttl__ <EOL> self . _timestamp = None <EOL> return self <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __dmlquery__ ( self . __class__ , self , <EOL> batch = self . _batch , <EOL> timestamp = self . _timestamp , <EOL> consistency = self . __consistency__ , <EOL> timeout = self . _timeout , <EOL> conditional = self . _conditional , <EOL> if_exists = self . _if_exists ) . delete ( ) <EOL> def get_changed_columns ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ k for k , v in self . _values . items ( ) if v . changed ] <EOL> @ classmethod <EOL> def _class_batch ( cls , batch ) : <EOL> return cls . objects . batch ( batch ) <EOL> def _inst_batch ( self , batch ) : <EOL> assert self . _timeout is connection . NOT_SET , '<STR_LIT>' <EOL> self . _batch = batch <EOL> return self <EOL> batch = hybrid_classmethod ( _class_batch , _inst_batch ) <EOL> class ModelMetaClass ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> column_dict = OrderedDict ( ) <EOL> primary_keys = OrderedDict ( ) <EOL> pk_name = None <EOL> inherited_columns = OrderedDict ( ) <EOL> for base in bases : <EOL> for k , v in getattr ( base , '<STR_LIT>' , { } ) . items ( ) : <EOL> inherited_columns . setdefault ( k , v ) <EOL> is_abstract = attrs [ '<STR_LIT>' ] = attrs . get ( '<STR_LIT>' , False ) <EOL> attrs [ '<STR_LIT>' ] = attrs . get ( '<STR_LIT>' ) <EOL> options = attrs . get ( '<STR_LIT>' ) or { } <EOL> attrs [ '<STR_LIT>' ] = options . get ( '<STR_LIT>' ) <EOL> def _transform_column ( col_name , col_obj ) : <EOL> column_dict [ col_name ] = col_obj <EOL> if col_obj . primary_key : <EOL> primary_keys [ col_name ] = col_obj <EOL> col_obj . set_column_name ( col_name ) <EOL> attrs [ col_name ] = ColumnDescriptor ( col_obj ) <EOL> column_definitions = [ ( k , v ) for k , v in attrs . items ( ) if isinstance ( v , columns . Column ) ] <EOL> column_definitions = sorted ( column_definitions , key = lambda x : x [ <NUM_LIT:1> ] . position ) <EOL> is_polymorphic_base = any ( [ c [ <NUM_LIT:1> ] . discriminator_column for c in column_definitions ] ) <EOL> column_definitions = [ x for x in inherited_columns . items ( ) ] + column_definitions <EOL> discriminator_columns = [ c for c in column_definitions if c [ <NUM_LIT:1> ] . discriminator_column ] <EOL> is_polymorphic = len ( discriminator_columns ) > <NUM_LIT:0> <EOL> if len ( discriminator_columns ) > <NUM_LIT:1> : <EOL> raise ModelDefinitionException ( '<STR_LIT>' . format ( len ( discriminator_columns ) ) ) <EOL> if attrs [ '<STR_LIT>' ] and not is_polymorphic : <EOL> raise ModelDefinitionException ( '<STR_LIT>' ) <EOL> discriminator_column_name , discriminator_column = discriminator_columns [ <NUM_LIT:0> ] if discriminator_columns else ( None , None ) <EOL> if isinstance ( discriminator_column , ( columns . BaseContainerColumn , columns . Counter ) ) : <EOL> raise ModelDefinitionException ( '<STR_LIT>' ) <EOL> polymorphic_base = None <EOL> if is_polymorphic and not is_polymorphic_base : <EOL> def _get_polymorphic_base ( bases ) : <EOL> for base in bases : <EOL> if getattr ( base , '<STR_LIT>' , False ) : <EOL> return base <EOL> klass = _get_polymorphic_base ( base . __bases__ ) <EOL> if klass : <EOL> return klass <EOL> polymorphic_base = _get_polymorphic_base ( bases ) <EOL> defined_columns = OrderedDict ( column_definitions ) <EOL> if not is_abstract and not any ( [ v . primary_key for k , v in column_definitions ] ) : <EOL> raise ModelDefinitionException ( "<STR_LIT>" ) <EOL> counter_columns = [ c for c in defined_columns . values ( ) if isinstance ( c , columns . Counter ) ] <EOL> data_columns = [ c for c in defined_columns . values ( ) if not c . primary_key and not isinstance ( c , columns . Counter ) ] <EOL> if counter_columns and data_columns : <EOL> raise ModelDefinitionException ( '<STR_LIT>' ) <EOL> has_partition_keys = any ( v . partition_key for ( k , v ) in column_definitions ) <EOL> partition_key_index = <NUM_LIT:0> <EOL> for k , v in column_definitions : <EOL> if k in BaseModel . __dict__ : <EOL> raise ModelDefinitionException ( "<STR_LIT>" . format ( k ) ) <EOL> if ( v . primary_key or v . partition_key ) and isinstance ( v , columns . Counter ) : <EOL> raise ModelDefinitionException ( '<STR_LIT>' ) <EOL> if not has_partition_keys and v . primary_key : <EOL> v . partition_key = True <EOL> has_partition_keys = True <EOL> if v . partition_key : <EOL> v . _partition_key_index = partition_key_index <EOL> partition_key_index += <NUM_LIT:1> <EOL> _transform_column ( k , v ) <EOL> partition_keys = OrderedDict ( k for k in primary_keys . items ( ) if k [ <NUM_LIT:1> ] . partition_key ) <EOL> clustering_keys = OrderedDict ( k for k in primary_keys . items ( ) if not k [ <NUM_LIT:1> ] . partition_key ) <EOL> if attrs . get ( '<STR_LIT>' , True ) : <EOL> key_cols = [ c for c in partition_keys . values ( ) ] <EOL> partition_key_index = dict ( ( col . db_field_name , col . _partition_key_index ) for col in key_cols ) <EOL> key_cql_types = [ c . cql_type for c in key_cols ] <EOL> key_serializer = staticmethod ( lambda parts , proto_version : [ t . to_binary ( p , proto_version ) for t , p in zip ( key_cql_types , parts ) ] ) <EOL> else : <EOL> partition_key_index = { } <EOL> key_serializer = staticmethod ( lambda parts , proto_version : None ) <EOL> if len ( partition_keys ) == <NUM_LIT:0> : <EOL> if not is_abstract : <EOL> raise ModelException ( "<STR_LIT>" ) <EOL> if len ( partition_keys ) == <NUM_LIT:1> : <EOL> pk_name = [ x for x in partition_keys . keys ( ) ] [ <NUM_LIT:0> ] <EOL> attrs [ '<STR_LIT>' ] = attrs [ pk_name ] <EOL> else : <EOL> _get = lambda self : tuple ( self . _values [ c ] . getval ( ) for c in partition_keys . keys ( ) ) <EOL> _set = lambda self , val : tuple ( self . _values [ c ] . setval ( v ) for ( c , v ) in zip ( partition_keys . keys ( ) , val ) ) <EOL> attrs [ '<STR_LIT>' ] = property ( _get , _set ) <EOL> col_names = set ( ) <EOL> for v in column_dict . values ( ) : <EOL> if v . db_field_name in col_names : <EOL> raise ModelException ( "<STR_LIT>" . format ( name , v . db_field_name ) ) <EOL> if v . clustering_order and not ( v . primary_key and not v . partition_key ) : <EOL> raise ModelException ( "<STR_LIT>" ) <EOL> if v . clustering_order and v . clustering_order . lower ( ) not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise ModelException ( "<STR_LIT>" . format ( repr ( v . clustering_order ) , v . db_field_name ) ) <EOL> col_names . add ( v . db_field_name ) <EOL> db_map = { } <EOL> for col_name , field in column_dict . items ( ) : <EOL> db_field = field . db_field_name <EOL> if db_field != col_name : <EOL> db_map [ db_field ] = col_name <EOL> attrs [ '<STR_LIT>' ] = column_dict <EOL> attrs [ '<STR_LIT>' ] = primary_keys <EOL> attrs [ '<STR_LIT>' ] = defined_columns <EOL> attrs [ '<STR_LIT>' ] = db_map <EOL> attrs [ '<STR_LIT>' ] = pk_name <EOL> attrs [ '<STR_LIT>' ] = { } <EOL> attrs [ '<STR_LIT>' ] = partition_keys <EOL> attrs [ '<STR_LIT>' ] = partition_key_index <EOL> attrs [ '<STR_LIT>' ] = key_serializer <EOL> attrs [ '<STR_LIT>' ] = clustering_keys <EOL> attrs [ '<STR_LIT>' ] = len ( counter_columns ) > <NUM_LIT:0> <EOL> attrs [ '<STR_LIT>' ] = is_polymorphic_base <EOL> attrs [ '<STR_LIT>' ] = is_polymorphic <EOL> attrs [ '<STR_LIT>' ] = polymorphic_base <EOL> attrs [ '<STR_LIT>' ] = discriminator_column <EOL> attrs [ '<STR_LIT>' ] = discriminator_column_name <EOL> attrs [ '<STR_LIT>' ] = { } if is_polymorphic_base else None <EOL> DoesNotExistBase = None <EOL> for base in bases : <EOL> DoesNotExistBase = getattr ( base , '<STR_LIT>' , None ) <EOL> if DoesNotExistBase is not None : <EOL> break <EOL> DoesNotExistBase = DoesNotExistBase or attrs . pop ( '<STR_LIT>' , BaseModel . DoesNotExist ) <EOL> attrs [ '<STR_LIT>' ] = type ( '<STR_LIT>' , ( DoesNotExistBase , ) , { } ) <EOL> MultipleObjectsReturnedBase = None <EOL> for base in bases : <EOL> MultipleObjectsReturnedBase = getattr ( base , '<STR_LIT>' , None ) <EOL> if MultipleObjectsReturnedBase is not None : <EOL> break <EOL> MultipleObjectsReturnedBase = MultipleObjectsReturnedBase or attrs . pop ( '<STR_LIT>' , BaseModel . MultipleObjectsReturned ) <EOL> attrs [ '<STR_LIT>' ] = type ( '<STR_LIT>' , ( MultipleObjectsReturnedBase , ) , { } ) <EOL> klass = super ( ModelMetaClass , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> udts = [ ] <EOL> for col in column_dict . values ( ) : <EOL> columns . resolve_udts ( col , udts ) <EOL> for user_type in set ( udts ) : <EOL> user_type . register_for_keyspace ( klass . _get_keyspace ( ) ) <EOL> return klass <EOL> @ six . add_metaclass ( ModelMetaClass ) <EOL> class Model ( BaseModel ) : <EOL> __abstract__ = True <EOL> """<STR_LIT>""" <EOL> __table_name__ = None <EOL> """<STR_LIT>""" <EOL> __table_name_case_sensitive__ = False <EOL> """<STR_LIT>""" <EOL> __keyspace__ = None <EOL> """<STR_LIT>""" <EOL> __options__ = None <EOL> """<STR_LIT>""" <EOL> __discriminator_value__ = None <EOL> """<STR_LIT>""" <EOL> __compute_routing_key__ = True <EOL> """<STR_LIT>""" </s>
