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 sort_data(data: Cursor, request_params: dict) -> list: ''' Sorts a data according to the parameters sent in an HTTP request. --> data : The cursor of data to apply the sort to. --> request_params : The parameters sent with the request (in querystring or body). <-- A queryset containing the sorted data. ''' mongo_sort = request_params.get('sort', {}) if mongo_sort != {}: return data.sort([(x, int(y)) for x, y in mongo_sort.items()]) return data