1
1
import abc
2
2
from dataclasses import dataclass , field
3
3
from typing import List , Callable
4
-
5
4
from azure .cosmos import PartitionKey
6
5
from azure .cosmos .exceptions import CosmosResourceNotFoundError
7
-
6
+ from flask_restplus import abort
8
7
from flask_restplus ._http import HTTPStatus
9
8
10
9
from commons .data_access_layer .cosmos_db import (
22
21
from commons .data_access_layer .database import EventContext
23
22
from time_tracker_api .activities import activities_model
24
23
25
- from utils .extend_model import add_project_name_to_time_entries , add_activity_name_to_time_entries
24
+ from utils .extend_model import (
25
+ add_project_name_to_time_entries ,
26
+ add_activity_name_to_time_entries ,
27
+ )
26
28
from utils import worked_time
27
29
from utils .worked_time import str_to_datetime
28
-
30
+ from utils .extend_model import (
31
+ create_in_condition ,
32
+ create_custom_query_from_str ,
33
+ )
29
34
from time_tracker_api .projects .projects_model import ProjectCosmosDBModel
30
35
from time_tracker_api .projects import projects_model
31
36
from time_tracker_api .database import CRUDDao , APICosmosDBDao
@@ -144,13 +149,12 @@ def find_all(
144
149
self ,
145
150
event_context : EventContext ,
146
151
conditions : dict = {},
152
+ custom_sql_conditions : List [str ] = [],
147
153
date_range : dict = {},
148
154
):
149
- custom_sql_conditions = [self .create_sql_date_range_filter (date_range )]
150
-
151
- if event_context .is_admin :
152
- conditions .pop ("owner_id" )
153
- # TODO should be removed when implementing a role-based permission module ↑
155
+ custom_sql_conditions .append (
156
+ self .create_sql_date_range_filter (date_range )
157
+ )
154
158
155
159
custom_params = self .generate_params (date_range )
156
160
time_entries = CosmosDBRepository .find_all (
@@ -162,14 +166,8 @@ def find_all(
162
166
)
163
167
164
168
if time_entries :
165
- projects_id = [str (project .project_id ) for project in time_entries ]
166
- p_ids = (
167
- str (tuple (projects_id )).replace ("," , "" )
168
- if len (projects_id ) == 1
169
- else str (tuple (projects_id ))
170
- )
171
- custom_conditions = "c.id IN {}" .format (p_ids )
172
- # TODO this must be refactored to be used from the utils module ↑
169
+ custom_conditions = create_in_condition (time_entries , "project_id" )
170
+
173
171
project_dao = projects_model .create_dao ()
174
172
projects = project_dao .get_all (
175
173
custom_sql_conditions = [custom_conditions ]
@@ -343,10 +341,30 @@ def stop_time_entry_if_was_left_running(
343
341
def get_all (self , conditions : dict = None , ** kwargs ) -> list :
344
342
event_ctx = self .create_event_context ("read-many" )
345
343
conditions .update ({"owner_id" : event_ctx .user_id })
346
-
344
+ custom_query = []
345
+ if "user_id" in conditions :
346
+ if event_ctx .is_admin :
347
+ conditions .pop ("owner_id" )
348
+ custom_query = (
349
+ []
350
+ if conditions .get ("user_id" ) == "*"
351
+ else [
352
+ create_custom_query_from_str (
353
+ conditions .get ("user_id" ), "c.owner_id"
354
+ )
355
+ ]
356
+ )
357
+ conditions .pop ("user_id" )
358
+ else :
359
+ abort (
360
+ HTTPStatus .FORBIDDEN , "You don't have enough permissions."
361
+ )
347
362
date_range = self .handle_date_filter_args (args = conditions )
348
363
return self .repository .find_all (
349
- event_ctx , conditions = conditions , date_range = date_range
364
+ event_ctx ,
365
+ conditions = conditions ,
366
+ custom_sql_conditions = custom_query ,
367
+ date_range = date_range ,
350
368
)
351
369
352
370
def get (self , id ):
@@ -432,6 +450,8 @@ def get_worked_time(self, conditions: dict = {}):
432
450
@staticmethod
433
451
def handle_date_filter_args (args : dict ) -> dict :
434
452
date_range = None
453
+ year = None
454
+ month = None
435
455
if 'month' and 'year' in args :
436
456
month = int (args .get ("month" ))
437
457
year = int (args .get ("year" ))
0 commit comments