Browse Source

PaginatedQuery: set a default for _sort_column

Previously, _sort_column wasn't set by default and needed to be set by
the query, but some of the other methods would fail if it hadn't been
set. This just defaults it to use the created_time column as the default
sort, which should always be present on models being queried by this
class (for now, at least).
merge-requests/22/head
Deimos 6 years ago
parent
commit
f95a504ca4
  1. 22
      tildes/tildes/models/pagination.py

22
tildes/tildes/models/pagination.py

@ -22,7 +22,8 @@ class PaginatedQuery(ModelQuery):
super().__init__(model_cls, request)
self._sort_column: Optional[Column] = None
# default to sorting by created_time descending (newest first)
self._sort_column = model_cls.created_time
self.sort_desc = True
self.after_id: Optional[int] = None
@ -135,17 +136,16 @@ class PaginatedQuery(ModelQuery):
"""Finalize the query before execution."""
query = super()._finalize()
if self._sort_column:
# if the query is reversed, we need to sort in the opposite dir
# (basically self.sort_desc XOR self.is_reversed)
desc = self.sort_desc
if self.is_reversed:
desc = not desc
# if the query is reversed, we need to sort in the opposite dir
# (basically self.sort_desc XOR self.is_reversed)
desc = self.sort_desc
if self.is_reversed:
desc = not desc
if desc:
query = query.order_by(*self.sorting_columns_desc)
else:
query = query.order_by(*self.sorting_columns)
if desc:
query = query.order_by(*self.sorting_columns_desc)
else:
query = query.order_by(*self.sorting_columns)
# pylint: disable=protected-access
query = query._apply_before_or_after()

Loading…
Cancel
Save