Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(time-entries): move custom_query to a function #192
  • Loading branch information
Angeluz-07 committed Jul 21, 2020
commit 31a198e120a72dfb87ef04374b273bb4e6293aa6
39 changes: 16 additions & 23 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,12 +402,10 @@ def stop_time_entry_if_was_left_running(
)
raise CosmosResourceNotFoundError()

def get_all(self, conditions: dict = None, **kwargs) -> list:
event_ctx = self.create_event_context("read-many")
conditions.update({"owner_id": event_ctx.user_id})
def build_custom_query(self, is_admin: bool, conditions: dict = None):
custom_query = []
if "user_id" in conditions:
if event_ctx.is_admin:
if is_admin:
conditions.pop("owner_id")
custom_query = (
[]
Expand All @@ -423,6 +421,16 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
abort(
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
)
return custom_query

def get_all(self, conditions: dict = None, **kwargs) -> list:
event_ctx = self.create_event_context("read-many")
conditions.update({"owner_id": event_ctx.user_id})

custom_query = self.build_custom_query(
is_admin=event_ctx.is_admin, conditions=conditions,
)

date_range = self.handle_date_filter_args(args=conditions)
limit = conditions.get("limit", None)
conditions.pop("limit", None)
Expand All @@ -438,25 +446,10 @@ def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
event_ctx = self.create_event_context("read-many")
conditions.update({"owner_id": event_ctx.user_id})

# TODO: abstract this method with a function or a class method
custom_query = []
if "user_id" in conditions:
if event_ctx.is_admin:
conditions.pop("owner_id")
custom_query = (
[]
if conditions.get("user_id") == "*"
else [
create_custom_query_from_str(
conditions.get("user_id"), "c.owner_id"
)
]
)
conditions.pop("user_id")
else:
abort(
HTTPStatus.FORBIDDEN, "You don't have enough permissions."
)
custom_query = self.build_custom_query(
is_admin=event_ctx.is_admin, conditions=conditions,
)

date_range = self.handle_date_filter_args(args=conditions)

length = conditions.get("length", None)
Expand Down