11
11
from werkzeug .exceptions import HTTPException
12
12
13
13
from commons .data_access_layer .database import CRUDDao
14
- from time_tracker_api .security import current_user_tenant_id , current_user_id
14
+ from time_tracker_api .security import current_user_tenant_id
15
15
16
16
17
17
class CosmosDBFacade :
@@ -105,10 +105,26 @@ def create_sql_condition_for_visibility(visible_only: bool, container_name='c')
105
105
return ''
106
106
107
107
@staticmethod
108
- def create_sql_condition_for_owner_id (owner_id : str , container_name = 'c' ) -> str :
109
- if owner_id :
110
- return 'AND %s.owner_id=@owner_id' % container_name
111
- return ''
108
+ def create_sql_where_conditions (conditions : dict , container_name = 'c' ) -> str :
109
+ where_conditions = []
110
+ for k in conditions .keys ():
111
+ where_conditions .append ('{c}.{var} = @{var}' .format (c = container_name , var = k ))
112
+
113
+ if len (where_conditions ) > 0 :
114
+ return "AND {where_conditions_clause}" .format (
115
+ where_conditions_clause = " AND " .join (where_conditions ))
116
+ else :
117
+ return ""
118
+
119
+ @staticmethod
120
+ def append_conditions_values (params : list , conditions : dict ) -> dict :
121
+ for k , v in conditions .items ():
122
+ params .append ({
123
+ "name" : "@%s" % k ,
124
+ "value" : v
125
+ })
126
+
127
+ return params
112
128
113
129
@staticmethod
114
130
def check_visibility (item , throw_not_found_if_deleted ):
@@ -123,39 +139,41 @@ def create(self, data: dict, mapper: Callable = None):
123
139
function_mapper = self .get_mapper_or_dict (mapper )
124
140
return function_mapper (self .container .create_item (body = data ))
125
141
126
- def find (self , id : str , partition_key_value , visible_only = True , mapper : Callable = None ):
142
+ def find (self , id : str , partition_key_value , peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
127
143
found_item = self .container .read_item (id , partition_key_value )
144
+ if peeker :
145
+ peeker (found_item )
146
+
128
147
function_mapper = self .get_mapper_or_dict (mapper )
129
148
return function_mapper (self .check_visibility (found_item , visible_only ))
130
149
131
- def find_all (self , partition_key_value : str , owner_id = None , max_count = None , offset = 0 ,
150
+ def find_all (self , partition_key_value : str , conditions : dict = {} , max_count = None , offset = 0 ,
132
151
visible_only = True , mapper : Callable = None ):
133
152
# TODO Use the tenant_id param and change container alias
134
153
max_count = self .get_page_size_or (max_count )
135
- result = self .container .query_items (
136
- query = """
154
+ params = self .append_conditions_values ([
155
+ {"name" : "@partition_key_value" , "value" : partition_key_value },
156
+ {"name" : "@offset" , "value" : offset },
157
+ {"name" : "@max_count" , "value" : max_count },
158
+ ], conditions )
159
+ result = self .container .query_items (query = """
137
160
SELECT * FROM c WHERE c.{partition_key_attribute}=@partition_key_value
138
- {owner_condition } {visibility_condition} {order_clause}
161
+ {conditions_clause } {visibility_condition} {order_clause}
139
162
OFFSET @offset LIMIT @max_count
140
163
""" .format (partition_key_attribute = self .partition_key_attribute ,
141
164
visibility_condition = self .create_sql_condition_for_visibility (visible_only ),
142
- owner_condition = self .create_sql_condition_for_owner_id ( owner_id ),
165
+ conditions_clause = self .create_sql_where_conditions ( conditions ),
143
166
order_clause = self .create_sql_order_clause ()),
144
- parameters = [
145
- {"name" : "@partition_key_value" , "value" : partition_key_value },
146
- {"name" : "@offset" , "value" : offset },
147
- {"name" : "@max_count" , "value" : max_count },
148
- {"name" : "@owner_id" , "value" : owner_id },
149
- ],
150
- partition_key = partition_key_value ,
151
- max_item_count = max_count )
167
+ parameters = params ,
168
+ partition_key = partition_key_value ,
169
+ max_item_count = max_count )
152
170
153
171
function_mapper = self .get_mapper_or_dict (mapper )
154
172
return list (map (function_mapper , result ))
155
173
156
174
def partial_update (self , id : str , changes : dict , partition_key_value : str ,
157
- visible_only = True , mapper : Callable = None ):
158
- item_data = self .find (id , partition_key_value , visible_only = visible_only , mapper = dict )
175
+ peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
176
+ item_data = self .find (id , partition_key_value , peeker = peeker , visible_only = visible_only , mapper = dict )
159
177
item_data .update (changes )
160
178
return self .update (id , item_data , mapper = mapper )
161
179
@@ -164,10 +182,11 @@ def update(self, id: str, item_data: dict, mapper: Callable = None):
164
182
function_mapper = self .get_mapper_or_dict (mapper )
165
183
return function_mapper (self .container .replace_item (id , body = item_data ))
166
184
167
- def delete (self , id : str , partition_key_value : str , mapper : Callable = None ):
185
+ def delete (self , id : str , partition_key_value : str ,
186
+ peeker : 'function' = None , mapper : Callable = None ):
168
187
return self .partial_update (id , {
169
188
'deleted' : str (uuid .uuid4 ())
170
- }, partition_key_value , visible_only = True , mapper = mapper )
189
+ }, partition_key_value , peeker = peeker , visible_only = True , mapper = mapper )
171
190
172
191
def delete_permanently (self , id : str , partition_key_value : str ) -> None :
173
192
self .container .delete_item (id , partition_key_value )
@@ -190,30 +209,21 @@ def on_update(self, update_item_data: dict):
190
209
def create_sql_order_clause (self ):
191
210
if len (self .order_fields ) > 0 :
192
211
return "ORDER BY c.{}" .format (", c." .join (self .order_fields ))
193
- else :
194
- return ""
212
+ return ""
195
213
196
214
197
215
class CosmosDBDao (CRUDDao ):
198
216
def __init__ (self , repository : CosmosDBRepository ):
199
217
self .repository = repository
200
218
201
- @property
202
- def partition_key_value (self ):
203
- return current_user_tenant_id ()
204
-
205
219
def get_all (self ) -> list :
206
- tenant_id : str = self .partition_key_value
207
- owner_id = current_user_id ()
208
- return self .repository .find_all (partition_key_value = tenant_id , owner_id = owner_id )
220
+ return self .repository .find_all (partition_key_value = self .partition_key_value )
209
221
210
222
def get (self , id ):
211
- tenant_id : str = self .partition_key_value
212
- return self .repository .find (id , partition_key_value = tenant_id )
223
+ return self .repository .find (id , partition_key_value = self .partition_key_value )
213
224
214
225
def create (self , data : dict ):
215
226
data [self .repository .partition_key_attribute ] = self .partition_key_value
216
- data ['owner_id' ] = current_user_id ()
217
227
return self .repository .create (data )
218
228
219
229
def update (self , id , data : dict ):
@@ -222,8 +232,11 @@ def update(self, id, data: dict):
222
232
partition_key_value = self .partition_key_value )
223
233
224
234
def delete (self , id ):
225
- tenant_id : str = current_user_tenant_id ()
226
- self .repository .delete (id , partition_key_value = tenant_id )
235
+ self .repository .delete (id , partition_key_value = self .partition_key_value )
236
+
237
+ @property
238
+ def partition_key_value (self ):
239
+ return current_user_tenant_id ()
227
240
228
241
229
242
class CustomError (HTTPException ):
0 commit comments