10
10
from flask import Flask
11
11
from werkzeug .exceptions import HTTPException
12
12
13
- from commons .data_access_layer .database import CRUDDao
14
- from time_tracker_api .security import current_user_tenant_id
13
+ from commons .data_access_layer .database import CRUDDao , EventContext
15
14
16
15
17
16
class CosmosDBFacade :
@@ -140,23 +139,35 @@ def replace_empty_value_per_none(item_data: dict) -> dict:
140
139
if isinstance (v , str ) and len (v ) == 0 :
141
140
item_data [k ] = None
142
141
143
- def create (self , data : dict , mapper : Callable = None ):
144
- self .on_create (data )
142
+ def attach_context (data : dict , event_context : EventContext ):
143
+ data ["_last_event_ctx" ] = {
144
+ "user_id" : event_context .user_id ,
145
+ "tenant_id" : event_context .tenant_id ,
146
+ "action" : event_context .action ,
147
+ "description" : event_context .description ,
148
+ "container_id" : event_context .container_id ,
149
+ "session_id" : event_context .session_id ,
150
+ }
151
+
152
+ def create (self , data : dict , event_context : EventContext , mapper : Callable = None ):
153
+ self .on_create (data , event_context )
145
154
function_mapper = self .get_mapper_or_dict (mapper )
155
+ self .attach_context (data , event_context )
146
156
return function_mapper (self .container .create_item (body = data ))
147
157
148
- def find (self , id : str , partition_key_value , peeker : 'function' = None ,
158
+ def find (self , id : str , event_context : EventContext , peeker : 'function' = None ,
149
159
visible_only = True , mapper : Callable = None ):
160
+ partition_key_value = self .find_partition_key_value (event_context )
150
161
found_item = self .container .read_item (id , partition_key_value )
151
162
if peeker :
152
163
peeker (found_item )
153
164
154
165
function_mapper = self .get_mapper_or_dict (mapper )
155
166
return function_mapper (self .check_visibility (found_item , visible_only ))
156
167
157
- def find_all (self , partition_key_value : str , conditions : dict = {}, max_count = None , offset = 0 ,
158
- visible_only = True , mapper : Callable = None ):
159
- # TODO Use the tenant_id param and change container alias
168
+ def find_all (self , event_context : EventContext , conditions : dict = {}, max_count = None ,
169
+ offset = 0 , visible_only = True , mapper : Callable = None ):
170
+ partition_key_value = self . find_partition_key_value ( event_context )
160
171
max_count = self .get_page_size_or (max_count )
161
172
params = [
162
173
{"name" : "@partition_key_value" , "value" : partition_key_value },
@@ -179,27 +190,32 @@ def find_all(self, partition_key_value: str, conditions: dict = {}, max_count=No
179
190
function_mapper = self .get_mapper_or_dict (mapper )
180
191
return list (map (function_mapper , result ))
181
192
182
- def partial_update (self , id : str , changes : dict , partition_key_value : str ,
193
+ def partial_update (self , id : str , changes : dict , event_context : EventContext ,
183
194
peeker : 'function' = None , visible_only = True , mapper : Callable = None ):
184
- item_data = self .find (id , partition_key_value , peeker = peeker ,
185
- visible_only = visible_only , mapper = dict )
195
+ item_data = self .find (id , event_context , peeker = peeker , visible_only = visible_only , mapper = dict )
186
196
item_data .update (changes )
187
- return self .update (id , item_data , mapper = mapper )
197
+ return self .update (id , item_data , event_context = event_context , mapper = mapper )
188
198
189
- def update (self , id : str , item_data : dict , mapper : Callable = None ):
199
+ def update (self , id : str , item_data : dict , event_context : EventContext ,
200
+ mapper : Callable = None ):
190
201
self .on_update (item_data )
191
202
function_mapper = self .get_mapper_or_dict (mapper )
203
+ self .attach_context (item_data , event_context )
192
204
return function_mapper (self .container .replace_item (id , body = item_data ))
193
205
194
- def delete (self , id : str , partition_key_value : str ,
206
+ def delete (self , id : str , event_context : EventContext ,
195
207
peeker : 'function' = None , mapper : Callable = None ):
208
+ partition_key_value = self .find_partition_key_value (event_context )
196
209
return self .partial_update (id , {
197
210
'deleted' : str (uuid .uuid4 ())
198
211
}, partition_key_value , peeker = peeker , visible_only = True , mapper = mapper )
199
212
200
213
def delete_permanently (self , id : str , partition_key_value : str ) -> None :
201
214
self .container .delete_item (id , partition_key_value )
202
215
216
+ def find_partition_key_value (self , event_context : EventContext ):
217
+ return getattr (event_context , self .partition_key_attribute )
218
+
203
219
def get_mapper_or_dict (self , alternative_mapper : Callable ) -> Callable :
204
220
return alternative_mapper or self .mapper or dict
205
221
@@ -208,10 +224,12 @@ def get_page_size_or(self, custom_page_size: int) -> int:
208
224
# or any other repository for the settings
209
225
return custom_page_size or 100
210
226
211
- def on_create (self , new_item_data : dict ):
227
+ def on_create (self , new_item_data : dict , event_context : EventContext ):
212
228
if new_item_data .get ('id' ) is None :
213
229
new_item_data ['id' ] = generate_uuid4 ()
214
230
231
+ new_item_data [self .partition_key_attribute ] = self .find_partition_key_value (event_context )
232
+
215
233
self .replace_empty_value_per_none (new_item_data )
216
234
217
235
def on_update (self , update_item_data : dict ):
@@ -228,27 +246,35 @@ def __init__(self, repository: CosmosDBRepository):
228
246
self .repository = repository
229
247
230
248
def get_all (self , conditions : dict = {}) -> list :
231
- return self .repository .find_all (partition_key_value = self .partition_key_value ,
249
+ event_ctx = self .create_event_context ("read-many" )
250
+ return self .repository .find_all (event_context = event_ctx ,
232
251
conditions = conditions )
233
252
234
253
def get (self , id ):
235
- return self .repository .find (id , partition_key_value = self .partition_key_value )
254
+ event_ctx = self .create_event_context ("read" )
255
+ return self .repository .find (id , event_context = event_ctx )
236
256
237
257
def create (self , data : dict ):
238
- data [ self . repository . partition_key_attribute ] = self .partition_key_value
239
- return self .repository .create (data )
258
+ event_ctx = self .create_event_context ( "create" )
259
+ return self .repository .create (data , event_context = event_ctx )
240
260
241
261
def update (self , id , data : dict ):
242
- return self .repository .partial_update (id ,
243
- changes = data ,
244
- partition_key_value = self .partition_key_value )
262
+ event_ctx = self .create_event_context ("update" )
263
+ return self .repository .partial_update (id , changes = data , event_context = event_ctx )
245
264
246
265
def delete (self , id ):
247
- self .repository .delete (id , partition_key_value = self .partition_key_value )
266
+ event_ctx = self .create_event_context ("update" )
267
+ self .repository .delete (id , event_context = event_ctx )
248
268
249
269
@property
250
- def partition_key_value (self ):
251
- return current_user_tenant_id ()
270
+ def find_partition_key_value (self , event_context : EventContext ):
271
+ return event_context .tenant_id
272
+
273
+ # Replace by decorator and put it in the repository
274
+ def create_event_context (self , action : str = None , description : str = None ):
275
+ return EventContext (self .repository .container .id ,
276
+ action ,
277
+ description = description )
252
278
253
279
254
280
class CustomError (HTTPException ):
0 commit comments