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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import re
from warnings import warn

from cassandra.cqlengine import CQLEngineException, ValidationError
from cassandra.cqlengine import columns
from cassandra.cqlengine import connection
from cassandra.cqlengine import query
from cassandra.cqlengine.query import DoesNotExist as _DoesNotExist
from cassandra.cqlengine.query import MultipleObjectsReturned as _MultipleObjectsReturned
from cassandra.metadata import protect_name
from cassandra.util import OrderedDict

log = logging.getLogger(__name__)


def _clone_model_class(model, attrs):
    new_type = type(model.__name__, (model,), attrs)
    try:
        new_type.__abstract__ = model.__abstract__
        new_type.__discriminator_value__ = model.__discriminator_value__
        new_type.__default_ttl__ = model.__default_ttl__
    except AttributeError:
        pass
    return new_type


class ModelException(CQLEngineException):
    pass


class ModelDefinitionException(ModelException):
    pass


class PolymorphicModelException(ModelException):
    pass


class UndefinedKeyspaceWarning(Warning):
    pass

DEFAULT_KEYSPACE = None


class hybrid_classmethod(object):
    """

    Allows a method to behave as both a class method and

    normal instance method depending on how it's called

    """
    def __init__(self, clsmethod, instmethod):
        self.clsmethod = clsmethod
        self.instmethod = instmethod

    def __get__(self, instance, owner):
        if instance is None:
            return self.clsmethod.__get__(owner, owner)
        else:
            return self.instmethod.__get__(instance, owner)

    def __call__(self, *args, **kwargs):
        """

        Just a hint to IDEs that it's ok to call this

        """
        raise NotImplementedError


class QuerySetDescriptor(object):
    """

    returns a fresh queryset for the given model

    it's declared on everytime it's accessed

    """

    def __get__(self, obj, model):
        """ :rtype: ModelQuerySet """
        if model.__abstract__:
            raise CQLEngineException('cannot execute queries against abstract models')
        queryset = model.__queryset__(model)

        # if this is a concrete polymorphic model, and the discriminator
        # key is an indexed column, add a filter clause to only return
        # logical rows of the proper type
        if model._is_polymorphic and not model._is_polymorphic_base:
            name, column = model._discriminator_column_name, model._discriminator_column
            if column.partition_key or column.index:
                # look for existing poly types
                return queryset.filter(**{name: model.__discriminator_value__})

        return queryset

    def __call__(self, *args, **kwargs):
        """

        Just a hint to IDEs that it's ok to call this



        :rtype: ModelQuerySet

        """
        raise NotImplementedError


class ConditionalDescriptor(object):
    """

    returns a query set descriptor

    """
    def __get__(self, instance, model):
        if instance:
            def conditional_setter(*prepared_conditional, **unprepared_conditionals):
                if len(prepared_conditional) > 0:
                    conditionals = prepared_conditional[0]
                else:
                    conditionals = instance.objects.iff(**unprepared_conditionals)._conditional
                instance._conditional = conditionals
                return instance

            return conditional_setter
        qs = model.__queryset__(model)

        def conditional_setter(**unprepared_conditionals):
            conditionals = model.objects.iff(**unprepared_conditionals)._conditional
            qs._conditional = conditionals
            return qs
        return conditional_setter

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class TTLDescriptor(object):
    """

    returns a query set descriptor

    """
    def __get__(self, instance, model):
        if instance:
            # instance = copy.deepcopy(instance)
            # instance method
            def ttl_setter(ts):
                instance._ttl = ts
                return instance
            return ttl_setter

        qs = model.__queryset__(model)

        def ttl_setter(ts):
            qs._ttl = ts
            return qs

        return ttl_setter

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class TimestampDescriptor(object):
    """

    returns a query set descriptor with a timestamp specified

    """
    def __get__(self, instance, model):
        if instance:
            # instance method
            def timestamp_setter(ts):
                instance._timestamp = ts
                return instance
            return timestamp_setter

        return model.objects.timestamp

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class IfNotExistsDescriptor(object):
    """

    return a query set descriptor with a if_not_exists flag specified

    """
    def __get__(self, instance, model):
        if instance:
            # instance method
            def ifnotexists_setter(ife=True):
                instance._if_not_exists = ife
                return instance
            return ifnotexists_setter

        return model.objects.if_not_exists

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class IfExistsDescriptor(object):
    """

    return a query set descriptor with a if_exists flag specified

    """
    def __get__(self, instance, model):
        if instance:
            # instance method
            def ifexists_setter(ife=True):
                instance._if_exists = ife
                return instance
            return ifexists_setter

        return model.objects.if_exists

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class ConsistencyDescriptor(object):
    """

    returns a query set descriptor if called on Class, instance if it was an instance call

    """
    def __get__(self, instance, model):
        if instance:
            # instance = copy.deepcopy(instance)
            def consistency_setter(consistency):
                instance.__consistency__ = consistency
                return instance
            return consistency_setter

        qs = model.__queryset__(model)

        def consistency_setter(consistency):
            qs._consistency = consistency
            return qs

        return consistency_setter

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class UsingDescriptor(object):
    """

    return a query set descriptor with a connection context specified

    """
    def __get__(self, instance, model):
        if instance:
            # instance method
            def using_setter(connection=None):
                if connection:
                    instance._connection = connection
                return instance
            return using_setter

        return model.objects.using

    def __call__(self, *args, **kwargs):
        raise NotImplementedError


class ColumnQueryEvaluator(query.AbstractQueryableColumn):
    """

    Wraps a column and allows it to be used in comparator

    expressions, returning query operators



    ie:

    Model.column == 5

    """

    def __init__(self, column):
        self.column = column

    def __unicode__(self):
        return self.column.db_field_name

    def _get_column(self):
        return self.column


class ColumnDescriptor(object):
    """

    Handles the reading and writing of column values to and from

    a model instance's value manager, as well as creating

    comparator queries

    """

    def __init__(self, column):
        """

        :param column:

        :type column: columns.Column

        :return:

        """
        self.column = column
        self.query_evaluator = ColumnQueryEvaluator(self.column)

    def __get__(self, instance, owner):
        """

        Returns either the value or column, depending

        on if an instance is provided or not



        :param instance: the model instance

        :type instance: Model

        """
        try:
            return instance._values[self.column.column_name].getval()
        except AttributeError:
            return self.query_evaluator

    def __set__(self, instance, value):
        """

        Sets the value on an instance, raises an exception with classes

        TODO: use None instance to create update statements

        """
        if instance:
            return instance._values[self.column.column_name].setval(value)
        else:
            raise AttributeError('cannot reassign column values')

    def __delete__(self, instance):
        """

        Sets the column value to None, if possible

        """
        if instance:
            if self.column.can_delete:
                instance._values[self.column.column_name].delval()
            else:
                raise AttributeError('cannot delete {0} columns'.format(self.column.column_name))


class BaseModel(object):
    """

    The base model class, don't inherit from this, inherit from Model, defined below

    """

    class DoesNotExist(_DoesNotExist):
        pass

    class MultipleObjectsReturned(_MultipleObjectsReturned):
        pass

    objects = QuerySetDescriptor()
    ttl = TTLDescriptor()
    consistency = ConsistencyDescriptor()
    iff = ConditionalDescriptor()

    # custom timestamps, see USING TIMESTAMP X
    timestamp = TimestampDescriptor()

    if_not_exists = IfNotExistsDescriptor()

    if_exists = IfExistsDescriptor()

    using = UsingDescriptor()

    # _len is lazily created by __len__

    __table_name__ = None

    __table_name_case_sensitive__ = False

    __keyspace__ = None

    __connection__ = None

    __discriminator_value__ = None

    __options__ = None

    __compute_routing_key__ = True

    # the queryset class used for this class
    __queryset__ = query.ModelQuerySet
    __dmlquery__ = query.DMLQuery

    __consistency__ = None  # can be set per query

    _timestamp = None  # optional timestamp to include with the operation (USING TIMESTAMP)

    _if_not_exists = False  # optional if_not_exists flag to check existence before insertion

    _if_exists = False  # optional if_exists flag to check existence before update

    _table_name = None  # used internally to cache a derived table name

    _connection = None

    def __init__(self, **values):
        self._ttl = None
        self._timestamp = None
        self._conditional = None
        self._batch = None
        self._timeout = connection.NOT_SET
        self._is_persisted = False
        self._connection = None

        self._values = {}
        for name, column in self._columns.items():
            # Set default values on instantiation. Thanks to this, we don't have
            # to wait anylonger for a call to validate() to have CQLengine set
            # default columns values.
            column_default = column.get_default() if column.has_default else None
            value = values.get(name, column_default)
            if value is not None or isinstance(column, columns.BaseContainerColumn):
                value = column.to_python(value)
            value_mngr = column.value_manager(self, column, value)
            value_mngr.explicit = name in values
            self._values[name] = value_mngr

    def __repr__(self):
        return '{0}({1})'.format(self.__class__.__name__,
                               ', '.join('{0}={1!r}'.format(k, getattr(self, k))
                                         for k in self._defined_columns.keys()
                                         if k != self._discriminator_column_name))

    def __str__(self):
        """

        Pretty printing of models by their primary key

        """
        return '{0} <{1}>'.format(self.__class__.__name__,
                                ', '.join('{0}={1}'.format(k, getattr(self, k)) for k in self._primary_keys.keys()))

    @classmethod
    def _routing_key_from_values(cls, pk_values, protocol_version):
        return cls._key_serializer(pk_values, protocol_version)

    @classmethod
    def _discover_polymorphic_submodels(cls):
        if not cls._is_polymorphic_base:
            raise ModelException('_discover_polymorphic_submodels can only be called on polymorphic base classes')

        def _discover(klass):
            if not klass._is_polymorphic_base and klass.__discriminator_value__ is not None:
                cls._discriminator_map[klass.__discriminator_value__] = klass
            for subklass in klass.__subclasses__():
                _discover(subklass)
        _discover(cls)

    @classmethod
    def _get_model_by_discriminator_value(cls, key):
        if not cls._is_polymorphic_base:
            raise ModelException('_get_model_by_discriminator_value can only be called on polymorphic base classes')
        return cls._discriminator_map.get(key)

    @classmethod
    def _construct_instance(cls, values):
        """

        method used to construct instances from query results

        this is where polymorphic deserialization occurs

        """
        # we're going to take the values, which is from the DB as a dict
        # and translate that into our local fields
        # the db_map is a db_field -> model field map
        if cls._db_map:
            values = dict((cls._db_map.get(k, k), v) for k, v in values.items())

        if cls._is_polymorphic:
            disc_key = values.get(cls._discriminator_column_name)

            if disc_key is None:
                raise PolymorphicModelException('discriminator value was not found in values')

            poly_base = cls if cls._is_polymorphic_base else cls._polymorphic_base

            klass = poly_base._get_model_by_discriminator_value(disc_key)
            if klass is None:
                poly_base._discover_polymorphic_submodels()
                klass = poly_base._get_model_by_discriminator_value(disc_key)
                if klass is None:
                    raise PolymorphicModelException(
                        'unrecognized discriminator column {0} for class {1}'.format(disc_key, poly_base.__name__)
                    )

            if not issubclass(klass, cls):
                raise PolymorphicModelException(
                    '{0} is not a subclass of {1}'.format(klass.__name__, cls.__name__)
                )

            values = dict((k, v) for k, v in values.items() if k in klass._columns.keys())

        else:
            klass = cls

        instance = klass(**values)
        instance._set_persisted(force=True)
        return instance

    def _set_persisted(self, force=False):
        # ensure we don't modify to any values not affected by the last save/update
        for v in [v for v in self._values.values() if v.changed or force]:
            v.reset_previous_value()
            v.explicit = False
        self._is_persisted = True

    def _can_update(self):
        """

        Called by the save function to check if this should be

        persisted with update or insert



        :return:

        """
        if not self._is_persisted:
            return False

        return all([not self._values[k].changed for k in self._primary_keys])

    @classmethod
    def _get_keyspace(cls):
        """

        Returns the manual keyspace, if set, otherwise the default keyspace

        """
        return cls.__keyspace__ or DEFAULT_KEYSPACE

    @classmethod
    def _get_column(cls, name):
        """

        Returns the column matching the given name, raising a key error if

        it doesn't exist



        :param name: the name of the column to return

        :rtype: Column

        """
        return cls._columns[name]

    @classmethod
    def _get_column_by_db_name(cls, name):
        """

        Returns the column, mapped by db_field name

        """
        return cls._columns.get(cls._db_map.get(name, name))

    def __eq__(self, other):
        if self.__class__ != other.__class__:
            return False

        # check attribute keys
        keys = set(self._columns.keys())
        other_keys = set(other._columns.keys())
        if keys != other_keys:
            return False

        return all(getattr(self, key, None) == getattr(other, key, None) for key in other_keys)

    def __ne__(self, other):
        return not self.__eq__(other)

    @classmethod
    def column_family_name(cls, include_keyspace=True):
        """

        Returns the column family name if it's been defined

        otherwise, it creates it from the module and class name

        """
        cf_name = protect_name(cls._raw_column_family_name())
        if include_keyspace:
            keyspace = cls._get_keyspace()
            if not keyspace:
                raise CQLEngineException("Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.")
            return '{0}.{1}'.format(protect_name(keyspace), cf_name)

        return cf_name


    @classmethod
    def _raw_column_family_name(cls):
        if not cls._table_name:
            if cls.__table_name__:
                if cls.__table_name_case_sensitive__:
                    warn("Model __table_name_case_sensitive__ will be removed in 4.0.", PendingDeprecationWarning)
                    cls._table_name = cls.__table_name__
                else:
                    table_name = cls.__table_name__.lower()
                    if cls.__table_name__ != table_name:
                        warn(("Model __table_name__ will be case sensitive by default in 4.0. "
                        "You should fix the __table_name__ value of the '{0}' model.").format(cls.__name__))
                    cls._table_name = table_name
            else:
                if cls._is_polymorphic and not cls._is_polymorphic_base:
                    cls._table_name = cls._polymorphic_base._raw_column_family_name()
                else:
                    camelcase = re.compile(r'([a-z])([A-Z])')
                    ccase = lambda s: camelcase.sub(lambda v: '{0}_{1}'.format(v.group(1), v.group(2).lower()), s)

                    cf_name = ccase(cls.__name__)
                    # trim to less than 48 characters or cassandra will complain
                    cf_name = cf_name[-48:]
                    cf_name = cf_name.lower()
                    cf_name = re.sub(r'^_+', '', cf_name)
                    cls._table_name = cf_name

        return cls._table_name

    def _set_column_value(self, name, value):
        """Function to change a column value without changing the value manager states"""
        self._values[name].value = value  # internal assignement, skip the main setter

    def validate(self):
        """

        Cleans and validates the field values

        """
        for name, col in self._columns.items():
            v = getattr(self, name)
            if v is None and not self._values[name].explicit and col.has_default:
                v = col.get_default()
            val = col.validate(v)
            self._set_column_value(name, val)

    # Let an instance be used like a dict of its columns keys/values
    def __iter__(self):
        """ Iterate over column ids. """
        for column_id in self._columns.keys():
            yield column_id

    def __getitem__(self, key):
        """ Returns column's value. """
        if not isinstance(key, str):
            raise TypeError
        if key not in self._columns.keys():
            raise KeyError
        return getattr(self, key)

    def __setitem__(self, key, val):
        """ Sets a column's value. """
        if not isinstance(key, str):
            raise TypeError
        if key not in self._columns.keys():
            raise KeyError
        return setattr(self, key, val)

    def __len__(self):
        """

        Returns the number of columns defined on that model.

        """
        try:
            return self._len
        except:
            self._len = len(self._columns.keys())
            return self._len

    def keys(self):
        """ Returns a list of column IDs. """
        return [k for k in self]

    def values(self):
        """ Returns list of column values. """
        return [self[k] for k in self]

    def items(self):
        """ Returns a list of column ID/value tuples. """
        return [(k, self[k]) for k in self]

    def _as_dict(self):
        """ Returns a map of column names to cleaned values """
        values = self._dynamic_columns or {}
        for name, col in self._columns.items():
            values[name] = col.to_database(getattr(self, name, None))
        return values

    @classmethod
    def create(cls, **kwargs):
        """

        Create an instance of this model in the database.



        Takes the model column values as keyword arguments. Setting a value to

        `None` is equivalent to running a CQL `DELETE` on that column.



        Returns the instance.

        """
        extra_columns = set(kwargs.keys()) - set(cls._columns.keys())
        if extra_columns:
            raise ValidationError("Incorrect columns passed: {0}".format(extra_columns))
        return cls.objects.create(**kwargs)

    @classmethod
    def all(cls):
        """

        Returns a queryset representing all stored objects



        This is a pass-through to the model objects().all()

        """
        return cls.objects.all()

    @classmethod
    def filter(cls, *args, **kwargs):
        """

        Returns a queryset based on filter parameters.



        This is a pass-through to the model objects().:method:`~cqlengine.queries.filter`.

        """
        return cls.objects.filter(*args, **kwargs)

    @classmethod
    def get(cls, *args, **kwargs):
        """

        Returns a single object based on the passed filter constraints.



        This is a pass-through to the model objects().:method:`~cqlengine.queries.get`.

        """
        return cls.objects.get(*args, **kwargs)

    def timeout(self, timeout):
        """

        Sets a timeout for use in :meth:`~.save`, :meth:`~.update`, and :meth:`~.delete`

        operations

        """
        assert self._batch is None, 'Setting both timeout and batch is not supported'
        self._timeout = timeout
        return self

    def save(self):
        """

        Saves an object to the database.



        .. code-block:: python



            #create a person instance

            person = Person(first_name='Kimberly', last_name='Eggleston')

            #saves it to Cassandra

            person.save()

        """

        # handle polymorphic models
        if self._is_polymorphic:
            if self._is_polymorphic_base:
                raise PolymorphicModelException('cannot save polymorphic base model')
            else:
                setattr(self, self._discriminator_column_name, self.__discriminator_value__)

        self.validate()
        self.__dmlquery__(self.__class__, self,
                          batch=self._batch,
                          ttl=self._ttl,
                          timestamp=self._timestamp,
                          consistency=self.__consistency__,
                          if_not_exists=self._if_not_exists,
                          conditional=self._conditional,
                          timeout=self._timeout,
                          if_exists=self._if_exists).save()

        self._set_persisted()

        self._timestamp = None

        return self

    def update(self, **values):
        """

        Performs an update on the model instance. You can pass in values to set on the model

        for updating, or you can call without values to execute an update against any modified

        fields. If no fields on the model have been modified since loading, no query will be

        performed. Model validation is performed normally. Setting a value to `None` is

        equivalent to running a CQL `DELETE` on that column.



        It is possible to do a blind update, that is, to update a field without having first selected the object out of the database.

        See :ref:`Blind Updates <blind_updates>`

        """
        for column_id, v in values.items():
            col = self._columns.get(column_id)

            # check for nonexistant columns
            if col is None:
                raise ValidationError(
                    "{0}.{1} has no column named: {2}".format(
                        self.__module__, self.__class__.__name__, column_id))

            # check for primary key update attempts
            if col.is_primary_key:
                current_value = getattr(self, column_id)
                if v != current_value:
                    raise ValidationError(
                        "Cannot apply update to primary key '{0}' for {1}.{2}".format(
                            column_id, self.__module__, self.__class__.__name__))

            setattr(self, column_id, v)

        # handle polymorphic models
        if self._is_polymorphic:
            if self._is_polymorphic_base:
                raise PolymorphicModelException('cannot update polymorphic base model')
            else:
                setattr(self, self._discriminator_column_name, self.__discriminator_value__)

        self.validate()
        self.__dmlquery__(self.__class__, self,
                          batch=self._batch,
                          ttl=self._ttl,
                          timestamp=self._timestamp,
                          consistency=self.__consistency__,
                          conditional=self._conditional,
                          timeout=self._timeout,
                          if_exists=self._if_exists).update()

        self._set_persisted()

        self._timestamp = None

        return self

    def delete(self):
        """

        Deletes the object from the database

        """
        self.__dmlquery__(self.__class__, self,
                          batch=self._batch,
                          timestamp=self._timestamp,
                          consistency=self.__consistency__,
                          timeout=self._timeout,
                          conditional=self._conditional,
                          if_exists=self._if_exists).delete()

    def get_changed_columns(self):
        """

        Returns a list of the columns that have been updated since instantiation or save

        """
        return [k for k, v in self._values.items() if v.changed]

    @classmethod
    def _class_batch(cls, batch):
        return cls.objects.batch(batch)

    def _inst_batch(self, batch):
        assert self._timeout is connection.NOT_SET, 'Setting both timeout and batch is not supported'
        if self._connection:
            raise CQLEngineException("Cannot specify a connection on model in batch mode.")
        self._batch = batch
        return self

    batch = hybrid_classmethod(_class_batch, _inst_batch)

    @classmethod
    def _class_get_connection(cls):
        return cls.__connection__

    def _inst_get_connection(self):
        return self._connection or self.__connection__

    _get_connection = hybrid_classmethod(_class_get_connection, _inst_get_connection)


class ModelMetaClass(type):

    def __new__(cls, name, bases, attrs):
        # move column definitions into columns dict
        # and set default column names
        column_dict = OrderedDict()
        primary_keys = OrderedDict()
        pk_name = None

        # get inherited properties
        inherited_columns = OrderedDict()
        for base in bases:
            for k, v in getattr(base, '_defined_columns', {}).items():
                inherited_columns.setdefault(k, v)

        # short circuit __abstract__ inheritance
        is_abstract = attrs['__abstract__'] = attrs.get('__abstract__', False)

        # short circuit __discriminator_value__ inheritance
        attrs['__discriminator_value__'] = attrs.get('__discriminator_value__')

        # TODO __default__ttl__ should be removed in the next major release
        options = attrs.get('__options__') or {}
        attrs['__default_ttl__'] = options.get('default_time_to_live')

        column_definitions = [(k, v) for k, v in attrs.items() if isinstance(v, columns.Column)]
        column_definitions = sorted(column_definitions, key=lambda x: x[1].position)

        is_polymorphic_base = any([c[1].discriminator_column for c in column_definitions])

        column_definitions = [x for x in inherited_columns.items()] + column_definitions
        discriminator_columns = [c for c in column_definitions if c[1].discriminator_column]
        is_polymorphic = len(discriminator_columns) > 0
        if len(discriminator_columns) > 1:
            raise ModelDefinitionException('only one discriminator_column can be defined in a model, {0} found'.format(len(discriminator_columns)))

        if attrs['__discriminator_value__'] and not is_polymorphic:
            raise ModelDefinitionException('__discriminator_value__ specified, but no base columns defined with discriminator_column=True')

        discriminator_column_name, discriminator_column = discriminator_columns[0] if discriminator_columns else (None, None)

        if isinstance(discriminator_column, (columns.BaseContainerColumn, columns.Counter)):
            raise ModelDefinitionException('counter and container columns cannot be used as discriminator columns')

        # find polymorphic base class
        polymorphic_base = None
        if is_polymorphic and not is_polymorphic_base:
            def _get_polymorphic_base(bases):
                for base in bases:
                    if getattr(base, '_is_polymorphic_base', False):
                        return base
                    klass = _get_polymorphic_base(base.__bases__)
                    if klass:
                        return klass
            polymorphic_base = _get_polymorphic_base(bases)

        defined_columns = OrderedDict(column_definitions)

        # check for primary key
        if not is_abstract and not any([v.primary_key for k, v in column_definitions]):
            raise ModelDefinitionException("At least 1 primary key is required.")

        counter_columns = [c for c in defined_columns.values() if isinstance(c, columns.Counter)]
        data_columns = [c for c in defined_columns.values() if not c.primary_key and not isinstance(c, columns.Counter)]
        if counter_columns and data_columns:
            raise ModelDefinitionException('counter models may not have data columns')

        has_partition_keys = any(v.partition_key for (k, v) in column_definitions)

        def _transform_column(col_name, col_obj):
            column_dict[col_name] = col_obj
            if col_obj.primary_key:
                primary_keys[col_name] = col_obj
            col_obj.set_column_name(col_name)
            # set properties
            attrs[col_name] = ColumnDescriptor(col_obj)

        partition_key_index = 0
        # transform column definitions
        for k, v in column_definitions:
            # don't allow a column with the same name as a built-in attribute or method
            if k in BaseModel.__dict__:
                raise ModelDefinitionException("column '{0}' conflicts with built-in attribute/method".format(k))

            # counter column primary keys are not allowed
            if (v.primary_key or v.partition_key) and isinstance(v, columns.Counter):
                raise ModelDefinitionException('counter columns cannot be used as primary keys')

            # this will mark the first primary key column as a partition
            # key, if one hasn't been set already
            if not has_partition_keys and v.primary_key:
                v.partition_key = True
                has_partition_keys = True
            if v.partition_key:
                v._partition_key_index = partition_key_index
                partition_key_index += 1

            overriding = column_dict.get(k)
            if overriding:
                v.position = overriding.position
                v.partition_key = overriding.partition_key
                v._partition_key_index = overriding._partition_key_index
            _transform_column(k, v)

        partition_keys = OrderedDict(k for k in primary_keys.items() if k[1].partition_key)
        clustering_keys = OrderedDict(k for k in primary_keys.items() if not k[1].partition_key)

        if attrs.get('__compute_routing_key__', True):
            key_cols = [c for c in partition_keys.values()]
            partition_key_index = dict((col.db_field_name, col._partition_key_index) for col in key_cols)
            key_cql_types = [c.cql_type for c in key_cols]
            key_serializer = staticmethod(lambda parts, proto_version: [t.to_binary(p, proto_version) for t, p in zip(key_cql_types, parts)])
        else:
            partition_key_index = {}
            key_serializer = staticmethod(lambda parts, proto_version: None)

        # setup partition key shortcut
        if len(partition_keys) == 0:
            if not is_abstract:
                raise ModelException("at least one partition key must be defined")
        if len(partition_keys) == 1:
            pk_name = [x for x in partition_keys.keys()][0]
            attrs['pk'] = attrs[pk_name]
        else:
            # composite partition key case, get/set a tuple of values
            _get = lambda self: tuple(self._values[c].getval() for c in partition_keys.keys())
            _set = lambda self, val: tuple(self._values[c].setval(v) for (c, v) in zip(partition_keys.keys(), val))
            attrs['pk'] = property(_get, _set)

        # some validation
        col_names = set()
        for v in column_dict.values():
            # check for duplicate column names
            if v.db_field_name in col_names:
                raise ModelException("{0} defines the column '{1}' more than once".format(name, v.db_field_name))
            if v.clustering_order and not (v.primary_key and not v.partition_key):
                raise ModelException("clustering_order may be specified only for clustering primary keys")
            if v.clustering_order and v.clustering_order.lower() not in ('asc', 'desc'):
                raise ModelException("invalid clustering order '{0}' for column '{1}'".format(repr(v.clustering_order), v.db_field_name))
            col_names.add(v.db_field_name)

        # create db_name -> model name map for loading
        db_map = {}
        for col_name, field in column_dict.items():
            db_field = field.db_field_name
            if db_field != col_name:
                db_map[db_field] = col_name

        # add management members to the class
        attrs['_columns'] = column_dict
        attrs['_primary_keys'] = primary_keys
        attrs['_defined_columns'] = defined_columns

        # maps the database field to the models key
        attrs['_db_map'] = db_map
        attrs['_pk_name'] = pk_name
        attrs['_dynamic_columns'] = {}

        attrs['_partition_keys'] = partition_keys
        attrs['_partition_key_index'] = partition_key_index
        attrs['_key_serializer'] = key_serializer
        attrs['_clustering_keys'] = clustering_keys
        attrs['_has_counter'] = len(counter_columns) > 0

        # add polymorphic management attributes
        attrs['_is_polymorphic_base'] = is_polymorphic_base
        attrs['_is_polymorphic'] = is_polymorphic
        attrs['_polymorphic_base'] = polymorphic_base
        attrs['_discriminator_column'] = discriminator_column
        attrs['_discriminator_column_name'] = discriminator_column_name
        attrs['_discriminator_map'] = {} if is_polymorphic_base else None

        # setup class exceptions
        DoesNotExistBase = None
        for base in bases:
            DoesNotExistBase = getattr(base, 'DoesNotExist', None)
            if DoesNotExistBase is not None:
                break

        DoesNotExistBase = DoesNotExistBase or attrs.pop('DoesNotExist', BaseModel.DoesNotExist)
        attrs['DoesNotExist'] = type('DoesNotExist', (DoesNotExistBase,), {})

        MultipleObjectsReturnedBase = None
        for base in bases:
            MultipleObjectsReturnedBase = getattr(base, 'MultipleObjectsReturned', None)
            if MultipleObjectsReturnedBase is not None:
                break

        MultipleObjectsReturnedBase = MultipleObjectsReturnedBase or attrs.pop('MultipleObjectsReturned', BaseModel.MultipleObjectsReturned)
        attrs['MultipleObjectsReturned'] = type('MultipleObjectsReturned', (MultipleObjectsReturnedBase,), {})

        # create the class and add a QuerySet to it
        klass = super(ModelMetaClass, cls).__new__(cls, name, bases, attrs)

        udts = []
        for col in column_dict.values():
            columns.resolve_udts(col, udts)

        for user_type in set(udts):
            user_type.register_for_keyspace(klass._get_keyspace())

        return klass


class Model(BaseModel, metaclass=ModelMetaClass):
    __abstract__ = True
    """

    *Optional.* Indicates that this model is only intended to be used as a base class for other models.

    You can't create tables for abstract models, but checks around schema validity are skipped during class construction.

    """

    __table_name__ = None
    """

    *Optional.* Sets the name of the CQL table for this model. If left blank, the table name will be the name of the model, with it's module name as it's prefix. Manually defined table names are not inherited.

    """

    __table_name_case_sensitive__ = False
    """

    *Optional.* By default, __table_name__ is case insensitive. Set this to True if you want to preserve the case sensitivity.

    """

    __keyspace__ = None
    """

    Sets the name of the keyspace used by this model.

    """

    __connection__ = None
    """

    Sets the name of the default connection used by this model.

    """

    __options__ = None
    """

    *Optional* Table options applied with this model



    (e.g. compaction, default ttl, cache settings, tec.)

    """

    __discriminator_value__ = None
    """

    *Optional* Specifies a value for the discriminator column when using model inheritance.

    """

    __compute_routing_key__ = True
    """

    *Optional* Setting False disables computing the routing key for TokenAwareRouting

    """