From f95a504ca4ead4a9093138f8eb598d161c3ea259 Mon Sep 17 00:00:00 2001 From: Deimos Date: Fri, 3 Aug 2018 00:05:10 -0600 Subject: [PATCH] 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). --- tildes/tildes/models/pagination.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tildes/tildes/models/pagination.py b/tildes/tildes/models/pagination.py index d80ff64..9e47562 100644 --- a/tildes/tildes/models/pagination.py +++ b/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()