8
8
import random
9
9
import time
10
10
import uuid
11
+ import warnings
11
12
from base64 import b64decode
12
13
13
14
import six
42
43
TableError , QueryError , PutError , DeleteError , UpdateError , GetError , ScanError , TableDoesNotExist ,
43
44
VerboseClientError
44
45
)
45
- from pynamodb .expressions .condition import Path
46
+ from pynamodb .expressions .condition import Condition , Path
46
47
from pynamodb .expressions .projection import create_projection_expression
47
48
from pynamodb .settings import get_settings_value
48
49
from pynamodb .signals import pre_dynamodb_send , post_dynamodb_send
@@ -773,6 +774,7 @@ def delete_item(self,
773
774
table_name ,
774
775
hash_key ,
775
776
range_key = None ,
777
+ condition = None ,
776
778
expected = None ,
777
779
conditional_operator = None ,
778
780
return_values = None ,
@@ -781,11 +783,16 @@ def delete_item(self,
781
783
"""
782
784
Performs the DeleteItem operation and returns the result
783
785
"""
786
+ self ._check_condition ('condition' , condition , expected , conditional_operator )
787
+
784
788
operation_kwargs = {TABLE_NAME : table_name }
785
789
operation_kwargs .update (self .get_identifier_map (table_name , hash_key , range_key ))
786
790
name_placeholders = {}
787
791
expression_attribute_values = {}
788
792
793
+ if condition is not None :
794
+ condition_expression = condition .serialize (name_placeholders , expression_attribute_values )
795
+ operation_kwargs [CONDITION_EXPRESSION ] = condition_expression
789
796
if return_values :
790
797
operation_kwargs .update (self .get_return_values_map (return_values ))
791
798
if return_consumed_capacity :
@@ -813,6 +820,7 @@ def update_item(self,
813
820
hash_key ,
814
821
range_key = None ,
815
822
attribute_updates = None ,
823
+ condition = None ,
816
824
expected = None ,
817
825
return_consumed_capacity = None ,
818
826
conditional_operator = None ,
@@ -821,11 +829,16 @@ def update_item(self,
821
829
"""
822
830
Performs the UpdateItem operation
823
831
"""
832
+ self ._check_condition ('condition' , condition , expected , conditional_operator )
833
+
824
834
operation_kwargs = {TABLE_NAME : table_name }
825
835
operation_kwargs .update (self .get_identifier_map (table_name , hash_key , range_key ))
826
836
name_placeholders = {}
827
837
expression_attribute_values = {}
828
838
839
+ if condition is not None :
840
+ condition_expression = condition .serialize (name_placeholders , expression_attribute_values )
841
+ operation_kwargs [CONDITION_EXPRESSION ] = condition_expression
829
842
if return_consumed_capacity :
830
843
operation_kwargs .update (self .get_consumed_capacity_map (return_consumed_capacity ))
831
844
if return_item_collection_metrics :
@@ -871,6 +884,7 @@ def put_item(self,
871
884
hash_key ,
872
885
range_key = None ,
873
886
attributes = None ,
887
+ condition = None ,
874
888
expected = None ,
875
889
conditional_operator = None ,
876
890
return_values = None ,
@@ -879,6 +893,8 @@ def put_item(self,
879
893
"""
880
894
Performs the PutItem operation and returns the result
881
895
"""
896
+ self ._check_condition ('condition' , condition , expected , conditional_operator )
897
+
882
898
operation_kwargs = {TABLE_NAME : table_name }
883
899
operation_kwargs .update (self .get_identifier_map (table_name , hash_key , range_key , key = ITEM ))
884
900
name_placeholders = {}
@@ -887,6 +903,9 @@ def put_item(self,
887
903
if attributes :
888
904
attrs = self .get_item_attribute_map (table_name , attributes )
889
905
operation_kwargs [ITEM ].update (attrs [ITEM ])
906
+ if condition is not None :
907
+ condition_expression = condition .serialize (name_placeholders , expression_attribute_values )
908
+ operation_kwargs [CONDITION_EXPRESSION ] = condition_expression
890
909
if return_consumed_capacity :
891
910
operation_kwargs .update (self .get_consumed_capacity_map (return_consumed_capacity ))
892
911
if return_item_collection_metrics :
@@ -1360,6 +1379,13 @@ def _get_condition(self, table_name, attribute_name, operator, *values):
1360
1379
]
1361
1380
return getattr (Path (attribute_name , attribute_name = True ), operator )(* values )
1362
1381
1382
+ def _check_condition (self , name , condition , expected_or_filter , conditional_operator ):
1383
+ if condition is not None :
1384
+ if not isinstance (condition , Condition ):
1385
+ raise ValueError ("'{0}' must be an instance of Condition" .format (name ))
1386
+ if expected_or_filter is not None or conditional_operator is not None :
1387
+ raise ValueError ("Legacy conditional parameters cannot be used with condition expressions" )
1388
+
1363
1389
@staticmethod
1364
1390
def _reverse_dict (d ):
1365
1391
return dict ((v , k ) for k , v in six .iteritems (d ))
0 commit comments