Browse Source

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.
merge-requests/22/head
Deimos 6 years ago
parent
commit
03f5450414
  1. 18
      tildes/tildes/models/pagination.py
  2. 4
      tildes/tildes/templates/topic_listing.jinja2

18
tildes/tildes/models/pagination.py

@ -3,7 +3,7 @@
from typing import Any, Iterator, List, Optional, TypeVar from typing import Any, Iterator, List, Optional, TypeVar
from pyramid.request import Request 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 tildes.lib.id import id36_to_id
from .model_query import ModelQuery from .model_query import ModelQuery
@ -204,3 +204,19 @@ class PaginatedResults:
def __len__(self) -> int: def __len__(self) -> int:
"""Return the number of results.""" """Return the number of results."""
return len(self.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]

4
tildes/tildes/templates/topic_listing.jinja2

@ -154,13 +154,13 @@
<div class="pagination"> <div class="pagination">
{% if topics.has_prev_page %} {% if topics.has_prev_page %}
<a class="page-item btn" id="prev-page" <a class="page-item btn" id="prev-page"
href="{{ request.current_listing_base_url({'before': topics[0].topic_id36}) }}"
href="{{ request.current_listing_base_url({'before': topics.prev_page_before_id}) }}"
>Prev</a> >Prev</a>
{% endif %} {% endif %}
{% if topics.has_next_page %} {% if topics.has_next_page %}
<a class="page-item btn" id="next-page" <a class="page-item btn" id="next-page"
href="{{ request.current_listing_base_url({'after': topics[-1].topic_id36}) }}"
href="{{ request.current_listing_base_url({'after': topics.next_page_after_id}) }}"
>Next</a> >Next</a>
{% endif %} {% endif %}
</div> </div>

Loading…
Cancel
Save