From 03f5450414155483fff26d5c82f54f5eabd2225c Mon Sep 17 00:00:00 2001 From: Deimos Date: Thu, 2 Aug 2018 23:58:30 -0600 Subject: [PATCH] PaginatedResults: add props for before/after IDs This adds two new properties to PaginatedResults - .next_page_after_id and .prev_page_before_id. These can be used when creating the links to the before/after pages, instead of needing to access the right element/property "manually". This will be most useful in listings that contain items of multiple types (such as comments and topics), since we won't necessarily know which type the first/last elements are. --- tildes/tildes/models/pagination.py | 18 +++++++++++++++++- tildes/tildes/templates/topic_listing.jinja2 | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tildes/tildes/models/pagination.py b/tildes/tildes/models/pagination.py index c4c6d8c..d80ff64 100644 --- a/tildes/tildes/models/pagination.py +++ b/tildes/tildes/models/pagination.py @@ -3,7 +3,7 @@ from typing import Any, Iterator, List, Optional, TypeVar from pyramid.request import Request -from sqlalchemy import Column, func +from sqlalchemy import Column, func, inspect from tildes.lib.id import id36_to_id from .model_query import ModelQuery @@ -204,3 +204,19 @@ class PaginatedResults: def __len__(self) -> int: """Return the number of results.""" return len(self.results) + + @property + def next_page_after_id(self) -> int: + """Return "after" ID that should be used to fetch the next page.""" + if not self.has_next_page: + raise AttributeError + + return inspect(self.results[-1]).identity[0] + + @property + def prev_page_before_id(self) -> int: + """Return "before" ID that should be used to fetch the prev page.""" + if not self.has_prev_page: + raise AttributeError + + return inspect(self.results[0]).identity[0] diff --git a/tildes/tildes/templates/topic_listing.jinja2 b/tildes/tildes/templates/topic_listing.jinja2 index 611fed2..2439d8d8 100644 --- a/tildes/tildes/templates/topic_listing.jinja2 +++ b/tildes/tildes/templates/topic_listing.jinja2 @@ -154,13 +154,13 @@