Skip to content

Commit b0c9dff

Browse files
committed
fix(time-entries): move custom_query to a function #192
1 parent 2dc0782 commit b0c9dff

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
@@ -401,12 +401,10 @@ def stop_time_entry_if_was_left_running(
401401
)
402402
raise CosmosResourceNotFoundError()
403403

404-
def get_all(self, conditions: dict = None, **kwargs) -> list:
405-
event_ctx = self.create_event_context("read-many")
406-
conditions.update({"owner_id": event_ctx.user_id})
404+
def build_custom_query(self, is_admin: bool, conditions: dict = None):
407405
custom_query = []
408406
if "user_id" in conditions:
409-
if event_ctx.is_admin:
407+
if is_admin:
410408
conditions.pop("owner_id")
411409
custom_query = (
412410
[]
@@ -422,6 +420,16 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
422420
abort(
423421
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
424422
)
423+
return custom_query
424+
425+
def get_all(self, conditions: dict = None, **kwargs) -> list:
426+
event_ctx = self.create_event_context("read-many")
427+
conditions.update({"owner_id": event_ctx.user_id})
428+
429+
custom_query = self.build_custom_query(
430+
is_admin=event_ctx.is_admin, conditions=conditions,
431+
)
432+
425433
date_range = self.handle_date_filter_args(args=conditions)
426434
limit = conditions.get("limit", None)
427435
conditions.pop("limit", None)
@@ -437,25 +445,10 @@ def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
437445
event_ctx = self.create_event_context("read-many")
438446
conditions.update({"owner_id": event_ctx.user_id})
439447

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

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

0 commit comments

Comments
 (0)