Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
feat: 🚧 make paginated endpoint with parameters length and start #192
  • Loading branch information
Angeluz-07 committed Jul 21, 2020
commit 5a851e71bf33d977f3b2b16bf7e3c84238bc1173
41 changes: 41 additions & 0 deletions time_tracker_api/time_entries/time_entries_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ def find_all(
custom_sql_conditions=custom_sql_conditions,
custom_params=custom_params,
max_count=kwargs.get("max_count", None),
offset=kwargs.get("offset", 0),
)

if time_entries:
Expand Down Expand Up @@ -430,6 +431,46 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
max_count=limit,
)

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."
)
date_range = self.handle_date_filter_args(args=conditions)

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

start = conditions.get("start", None)
conditions.pop("start", None)

return self.repository.find_all(
event_ctx,
conditions=conditions,
custom_sql_conditions=custom_query,
date_range=date_range,
max_count=length,
offset=start,
)

def get(self, id):
event_ctx = self.create_event_context("read")

Expand Down
31 changes: 31 additions & 0 deletions time_tracker_api/time_entries/time_entries_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,34 @@ def get(self):
"""Find the summary of worked time"""
conditions = summary_attribs_parser.parse_args()
return time_entries_dao.get_worked_time(conditions)


paginated_attribs_parser = ns.parser()
paginated_attribs_parser.add_argument(
'length',
required=True,
type=int,
help="(Filter) The number of rows the endpoint should return.",
location='args',
)

paginated_attribs_parser.add_argument(
'start',
required=True,
type=int,
help="(Filter) The number of rows to be removed from the query. (aka offset)",
location='args',
)


@ns.route('/paginated')
@ns.response(HTTPStatus.OK, 'Time Entries paginated')
@ns.response(HTTPStatus.NOT_FOUND, 'Time entry not found')
class PaginatedTimeEntry(Resource):
@ns.expect(paginated_attribs_parser)
@ns.doc('list_time_entries_paginated')
@ns.marshal_list_with(time_entry)
def get(self):
"""List all time entries paginated"""
conditions = paginated_attribs_parser.parse_args()
return time_entries_dao.get_all_paginated(conditions)