From 70448c59a802833dcab4377370a235f16295f31b Mon Sep 17 00:00:00 2001 From: Stef Kischak Date: Wed, 22 May 2024 18:54:54 -0400 Subject: [PATCH 1/9] paginate following page --- openlibrary/core/follows.py | 15 ++++++++++----- openlibrary/plugins/upstream/account.py | 19 +++++++++++++++---- openlibrary/templates/account/follows.html | 14 +++++++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/openlibrary/core/follows.py b/openlibrary/core/follows.py index 646f009c57e..0f8437594c1 100644 --- a/openlibrary/core/follows.py +++ b/openlibrary/core/follows.py @@ -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 @@ -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' @@ -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] diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 513dbf50cd7..3d2907c4873 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -1134,14 +1134,25 @@ class my_follows(delegate.page): path = r"/people/([^/]+)/(followers|following)" def GET(self, username, key=""): - mb = MyBooksTemplate(username, 'following') + i = web.input(page=1, limit=25) + page_size = max(1, i.limit) + offset = (max(0, i.page - 1)) * page_size + follows = ( - PubSub.get_followers(username) + PubSub.get_followers(username, page_size, offset) + if key == 'followers' + else PubSub.get_following(username, page_size, offset) + ) + follow_count = ( + PubSub.count_followers(username) if key == 'followers' - else PubSub.get_following(username) + else PubSub.count_following(username) ) + page_count = max(follow_count / page_size) + + 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, page_count, page_size, follows, manage=manage) return mb.render(header_title=_(key.capitalize()), template=template) diff --git a/openlibrary/templates/account/follows.html b/openlibrary/templates/account/follows.html index 426fa05d4da..23fbd565bb4 100644 --- a/openlibrary/templates/account/follows.html +++ b/openlibrary/templates/account/follows.html @@ -1,21 +1,25 @@ -$def with (user, follows=None, manage=False) +$def with (user, page_count, results_per_page, follows=None, manage=False) $if follows: $ username = user.key.split('/')[-1] $for follow in follows: + $ page = int(input(page=1).page) +
+ $:macros.Pager(page, page_count, results_per_page) +
$else:

$_("There is nothing to see here yet.")

From 378a8edd17fda685eda652ec18f6975cfd4b00f4 Mon Sep 17 00:00:00 2001 From: Stef Kischak Date: Wed, 22 May 2024 19:03:12 -0400 Subject: [PATCH 2/9] don't want to modify limit, right? --- openlibrary/plugins/upstream/account.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 3d2907c4873..586163eea5f 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -1134,8 +1134,8 @@ class my_follows(delegate.page): path = r"/people/([^/]+)/(followers|following)" def GET(self, username, key=""): - i = web.input(page=1, limit=25) - page_size = max(1, i.limit) + i = web.input(page=1) + page_size = 25 offset = (max(0, i.page - 1)) * page_size follows = ( From 9db8efa7374d505c4450a356ee4edfedfc9dfa92 Mon Sep 17 00:00:00 2001 From: Stef Kischak Date: Wed, 22 May 2024 19:04:35 -0400 Subject: [PATCH 3/9] indents --- openlibrary/templates/account/follows.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlibrary/templates/account/follows.html b/openlibrary/templates/account/follows.html index 23fbd565bb4..b481d0652f7 100644 --- a/openlibrary/templates/account/follows.html +++ b/openlibrary/templates/account/follows.html @@ -10,8 +10,8 @@ $follow['subscriber'] $else: $follow['publisher'] - -
+
+
$if manage: $:macros.Follow(follow['publisher'], following=1)
From 4cd4ce6b572f9502e1180c8398e57b7a5717731a Mon Sep 17 00:00:00 2001 From: Stef Kischak Date: Wed, 22 May 2024 19:06:52 -0400 Subject: [PATCH 4/9] Spacing --- openlibrary/templates/account/follows.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openlibrary/templates/account/follows.html b/openlibrary/templates/account/follows.html index b481d0652f7..d4ca98c96a3 100644 --- a/openlibrary/templates/account/follows.html +++ b/openlibrary/templates/account/follows.html @@ -5,7 +5,7 @@ $for follow in follows:
$ page = int(input(page=1).page) From dab37b2e8e9d796b9b7420bdcc59846c988d61a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 23:11:58 +0000 Subject: [PATCH 5/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openlibrary/core/follows.py | 8 ++++---- openlibrary/plugins/upstream/account.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/openlibrary/core/follows.py b/openlibrary/core/follows.py index 0f8437594c1..4fad168d6ce 100644 --- a/openlibrary/core/follows.py +++ b/openlibrary/core/follows.py @@ -42,11 +42,11 @@ def get_followers(cls, publisher, limit=None, offset=0): oldb = db.get_db() where = 'publisher=$publisher' subscribers = oldb.select( - cls.TABLENAME, - where=where, + cls.TABLENAME, + where=where, vars={'publisher': publisher}, limit=limit, - offset=offset + offset=offset, ) return subscribers @@ -62,7 +62,7 @@ def get_following(cls, subscriber, limit=None, offset=0, exclude_disabled=False) where=where, vars={'subscriber': subscriber}, limit=limit, - offset=offset + offset=offset, ) return [dict(s) for s in subscriptions] diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 586163eea5f..9f7e47393b1 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -1152,7 +1152,9 @@ def GET(self, username, key=""): mb = MyBooksTemplate(username, 'following') manage = key == 'following' and mb.is_my_page - template = render['account/follows'](mb.user, page_count, page_size, follows, manage=manage) + template = render['account/follows']( + mb.user, page_count, page_size, follows, manage=manage + ) return mb.render(header_title=_(key.capitalize()), template=template) From df8baa172d618d642299819f3413856588fbde5c Mon Sep 17 00:00:00 2001 From: pidgezero-one Date: Wed, 22 May 2024 20:19:35 -0400 Subject: [PATCH 6/9] fix error --- openlibrary/plugins/upstream/account.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 9f7e47393b1..788d0c7dbb6 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -6,6 +6,7 @@ from typing import Any, TYPE_CHECKING, Final from collections.abc import Callable from collections.abc import Iterable, Mapping +from math import ceil import web @@ -1148,7 +1149,7 @@ def GET(self, username, key=""): if key == 'followers' else PubSub.count_following(username) ) - page_count = max(follow_count / page_size) + page_count = ceil(follow_count / page_size) mb = MyBooksTemplate(username, 'following') manage = key == 'following' and mb.is_my_page From e7c84b55e493a1ba632c87ac8dc0f4c22c709cb2 Mon Sep 17 00:00:00 2001 From: pidgezero-one Date: Fri, 24 May 2024 21:09:00 -0400 Subject: [PATCH 7/9] give the correct number to pagination --- openlibrary/plugins/upstream/account.py | 6 ++---- openlibrary/templates/account/follows.html | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index 788d0c7dbb6..cd767323aa1 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -6,7 +6,6 @@ from typing import Any, TYPE_CHECKING, Final from collections.abc import Callable from collections.abc import Iterable, Mapping -from math import ceil import web @@ -1137,7 +1136,7 @@ class my_follows(delegate.page): def GET(self, username, key=""): i = web.input(page=1) page_size = 25 - offset = (max(0, i.page - 1)) * page_size + offset = (max(0, int(i.page) - 1)) * page_size follows = ( PubSub.get_followers(username, page_size, offset) @@ -1149,12 +1148,11 @@ def GET(self, username, key=""): if key == 'followers' else PubSub.count_following(username) ) - page_count = ceil(follow_count / page_size) mb = MyBooksTemplate(username, 'following') manage = key == 'following' and mb.is_my_page template = render['account/follows']( - mb.user, page_count, page_size, follows, manage=manage + mb.user, follow_count, page_size, follows, manage=manage ) return mb.render(header_title=_(key.capitalize()), template=template) diff --git a/openlibrary/templates/account/follows.html b/openlibrary/templates/account/follows.html index d4ca98c96a3..fcbff63b08e 100644 --- a/openlibrary/templates/account/follows.html +++ b/openlibrary/templates/account/follows.html @@ -1,7 +1,8 @@ -$def with (user, page_count, results_per_page, follows=None, manage=False) +$def with (user, num_found, results_per_page, follows=None, manage=False) $if follows: $ username = user.key.split('/')[-1] + $ page = int(input(page=1).page) $for follow in follows:
- $ page = int(input(page=1).page)
- $:macros.Pager(page, page_count, results_per_page) + $:macros.Pager(page, num_found, results_per_page)
$else:

$_("There is nothing to see here yet.")

From c2b5c9061a6024a2163a1cd641b149a020791f38 Mon Sep 17 00:00:00 2001 From: pidgezero-one Date: Fri, 24 May 2024 21:32:30 -0400 Subject: [PATCH 8/9] prevent invalid pages --- openlibrary/plugins/upstream/account.py | 5 +++-- openlibrary/templates/account/follows.html | 3 +-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index cd767323aa1..9911e2abd84 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -1136,7 +1136,8 @@ class my_follows(delegate.page): def GET(self, username, key=""): i = web.input(page=1) page_size = 25 - offset = (max(0, int(i.page) - 1)) * page_size + page = 1 if not i.page.isdigit() else max(1, int(i.page)) + offset = (page - 1) * page_size follows = ( PubSub.get_followers(username, page_size, offset) @@ -1152,7 +1153,7 @@ def GET(self, username, key=""): mb = MyBooksTemplate(username, 'following') manage = key == 'following' and mb.is_my_page template = render['account/follows']( - mb.user, follow_count, page_size, follows, manage=manage + mb.user, follow_count, page, page_size, follows, manage=manage ) return mb.render(header_title=_(key.capitalize()), template=template) diff --git a/openlibrary/templates/account/follows.html b/openlibrary/templates/account/follows.html index fcbff63b08e..d4dbec6e4d3 100644 --- a/openlibrary/templates/account/follows.html +++ b/openlibrary/templates/account/follows.html @@ -1,8 +1,7 @@ -$def with (user, num_found, results_per_page, follows=None, manage=False) +$def with (user, num_found, page, results_per_page, follows=None, manage=False) $if follows: $ username = user.key.split('/')[-1] - $ page = int(input(page=1).page) $for follow in follows: