def _apply_params(self, cursor: Cursor): cursor.skip(self._offset) if self._limit is not None: cursor.limit(self._limit) if len(self._order_by) > 0: order_by = [ (column, DESCENDING if desc else ASCENDING) for column, desc in self._order_by ] cursor.sort(order_by) return cursor
def limit_data(data: Cursor, request_params: dict) -> list: ''' Limits the number of results in a response based on the parameters sent in an HTTP request. --> data : The cursor of data to apply the limit to. --> request_params : The parameters sent with the request (in querystring or body). <-- A queryset containing the limited data. ''' mongo_limit = int(request_params.get('limit', 0)) if mongo_limit: return data.limit(mongo_limit) return data
def paginate(cursor: Cursor, *, offset: int, limit: int) -> Cursor: if offset: cursor = cursor.skip(offset) if limit is not None: cursor = cursor.limit(limit) return cursor