Skip to content

Commit 31a198e

Browse files
committed
fix(time-entries): move custom_query to a function #192
1 parent 676b933 commit 31a198e

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,10 @@ def stop_time_entry_if_was_left_running(
402402
)
403403
raise CosmosResourceNotFoundError()
404404

405-
def get_all(self, conditions: dict = None, **kwargs) -> list:
406-
event_ctx = self.create_event_context("read-many")
407-
conditions.update({"owner_id": event_ctx.user_id})
405+
def build_custom_query(self, is_admin: bool, conditions: dict = None):
408406
custom_query = []
409407
if "user_id" in conditions:
410-
if event_ctx.is_admin:
408+
if is_admin:
411409
conditions.pop("owner_id")
412410
custom_query = (
413411
[]
@@ -423,6 +421,16 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
423421
abort(
424422
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
425423
)
424+
return custom_query
425+
426+
def get_all(self, conditions: dict = None, **kwargs) -> list:
427+
event_ctx = self.create_event_context("read-many")
428+
conditions.update({"owner_id": event_ctx.user_id})
429+
430+
custom_query = self.build_custom_query(
431+
is_admin=event_ctx.is_admin, conditions=conditions,
432+
)
433+
426434
date_range = self.handle_date_filter_args(args=conditions)
427435
limit = conditions.get("limit", None)
428436
conditions.pop("limit", None)
@@ -438,25 +446,10 @@ def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
438446
event_ctx = self.create_event_context("read-many")
439447
conditions.update({"owner_id": event_ctx.user_id})
440448

441-
# TODO: abstract this method with a function or a class method
442-
custom_query = []
443-
if "user_id" in conditions:
444-
if event_ctx.is_admin:
445-
conditions.pop("owner_id")
446-
custom_query = (
447-
[]
448-
if conditions.get("user_id") == "*"
449-
else [
450-
create_custom_query_from_str(
451-
conditions.get("user_id"), "c.owner_id"
452-
)
453-
]
454-
)
455-
conditions.pop("user_id")
456-
else:
457-
abort(
458-
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
459-
)
449+
custom_query = self.build_custom_query(
450+
is_admin=event_ctx.is_admin, conditions=conditions,
451+
)
452+
460453
date_range = self.handle_date_filter_args(args=conditions)
461454

462455
length = conditions.get("length", None)

0 commit comments

Comments
 (0)