Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pagination to user following/follower lists #9323

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions openlibrary/core/follows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import web
from typing import cast, Any
from typing import cast
from openlibrary.core.bookshelves import Bookshelves

from . import db
Expand Down Expand Up @@ -38,17 +37,21 @@ def is_subscribed(cls, subscriber, publisher):
return len(subscription)

@classmethod
def get_followers(cls, publisher):
def get_followers(cls, publisher, limit=None, offset=0):
"""Get publishers subscribers"""
oldb = db.get_db()
where = 'publisher=$publisher'
subscribers = oldb.select(
cls.TABLENAME, where=where, vars={'publisher': publisher}
cls.TABLENAME,
where=where,
vars={'publisher': publisher},
limit=limit,
offset=offset,
)
return subscribers

@classmethod
def get_following(cls, subscriber, exclude_disabled=False):
def get_following(cls, subscriber, limit=None, offset=0, exclude_disabled=False):
"""Get subscriber's subscriptions"""
oldb = db.get_db()
where = 'subscriber=$subscriber'
Expand All @@ -58,6 +61,8 @@ def get_following(cls, subscriber, exclude_disabled=False):
cls.TABLENAME,
where=where,
vars={'subscriber': subscriber},
limit=limit,
offset=offset,
)
return [dict(s) for s in subscriptions]

Expand Down
23 changes: 19 additions & 4 deletions openlibrary/plugins/upstream/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,14 +1134,29 @@ class my_follows(delegate.page):
path = r"/people/([^/]+)/(followers|following)"

def GET(self, username, key=""):
mb = MyBooksTemplate(username, 'following')
i = web.input(page=1)
page_size = 25

# Force invalid page query params to default to the first page
page = 1 if not i.page.isdigit() else max(1, int(i.page))
offset = (page - 1) * page_size
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the page is also used to calculate the offset, I validate it here and pass it into the template instead of defining in the template. The number of values being passed to the template is quite large, and this might not be totally necessary, but doing it this way lets me force invalid page values in the query param to default to page 1 (i.e. ?page=0, ?page=-1, ?page=aaaa all default to the first page) before using it to calculate the offset.


follows = (
PubSub.get_followers(username)
PubSub.get_followers(username, page_size, offset)
if key == 'followers'
else PubSub.get_following(username)
else PubSub.get_following(username, page_size, offset)
)
follow_count = (
PubSub.count_followers(username)
if key == 'followers'
else PubSub.count_following(username)
)

mb = MyBooksTemplate(username, 'following')
manage = key == 'following' and mb.is_my_page
template = render['account/follows'](mb.user, follows, manage=manage)
template = render['account/follows'](
mb.user, follow_count, page, page_size, follows, manage=manage
)
return mb.render(header_title=_(key.capitalize()), template=template)


Expand Down
13 changes: 8 additions & 5 deletions openlibrary/templates/account/follows.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
$def with (user, follows=None, manage=False)
$def with (user, num_found, page, results_per_page, follows=None, manage=False)

$if follows:
$ username = user.key.split('/')[-1]
$for follow in follows:
<ul>
<li class="follow-row">
<div class="follow-row--name">
<div class="follow-row--name">
$if follow['publisher'] == username:
<a href="/people/$follow['subscriber']/books" class="username-avatar"><img src="/people/$follow['subscriber']/avatar" class="account-avatar">$follow['subscriber']</a>
$else:
<a href="/people/$follow['publisher']/books" class="username-avatar"><img src="/people/$follow['publisher']/avatar" class="account-avatar">$follow['publisher']</a>
</div>
<div>
</div>
<div>
$if manage:
$:macros.Follow(follow['publisher'], following=1)
</div>
</div>
</li>
</ul>
<div class="pager">
$:macros.Pager(page, num_found, results_per_page)
</div>
$else:
<p>$_("There is nothing to see here yet.")</p>
Loading