From 615354d17e567bd5a16d021de2a5951e581dfe0f Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 27 Nov 2026 20:51:30 -0700 Subject: [PATCH] feat: v2.21.0 paging for homepage, likes and admin dashboard The homepage showed only the 20 newest notes and /likes the 50 newest, with no way back. The admin dashboard paged list_notes()'s default 50 newest, so it always said 'Page 1 of 2'. All three now count with count_notes() and fetch one page with limit/offset. Public pages get '<- Newer / Page N of M / Older ->' with rel=prev/next (h-feed paging), ?page=1 redirects to the bare URL, bad pages 404; the dashboard falls back to the nearest valid page. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 12 ++ docs/design/v2.21.0/2026-11-28-paging.md | 55 +++++++ starpunk/__init__.py | 4 +- starpunk/notes.py | 38 +++-- starpunk/routes/admin.py | 22 ++- starpunk/routes/public.py | 89 +++++++---- static/css/style.css | 4 + templates/index.html | 5 +- templates/likes.html | 5 +- templates/partials/pager.html | 15 ++ tests/test_paging.py | 184 +++++++++++++++++++++++ 11 files changed, 373 insertions(+), 60 deletions(-) create mode 100644 docs/design/v2.21.0/2026-11-28-paging.md create mode 100644 templates/partials/pager.html create mode 100644 tests/test_paging.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7293398..235263b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.21.0] - 2026-11-28 + +### Added + +- The homepage pages through every note: 20 per page, with "← Newer" and "Older →" links and "Page N of M" at the bottom. Before, it showed only the 20 newest notes and older ones could only be found through tags, search or the feeds. Page 1 stays at `/`; later pages are `/?page=2` and on (`/?page=1` redirects to `/`), and their title starts "Page N ·". The links carry `rel="prev"`/`rel="next"`, so IndieWeb readers can page back through the h-feed +- The Likes page pages the same way, 50 per page (it showed only the 50 newest likes) + +### Fixed + +- The admin dashboard only ever listed the 50 newest notes ("Page 1 of 2" whatever the real count), because it paged the default 50-note result of `list_notes()`. It now counts all notes and fetches just the requested page. A bad `?page=` no longer raises a server error; it goes to the nearest valid page +- Public list pages answer a page number that isn't a positive whole number, or is past the last page, with 404 + ## [2.20.3] - 2026-11-27 ### Changed diff --git a/docs/design/v2.21.0/2026-11-28-paging.md b/docs/design/v2.21.0/2026-11-28-paging.md new file mode 100644 index 0000000..9decb12 --- /dev/null +++ b/docs/design/v2.21.0/2026-11-28-paging.md @@ -0,0 +1,55 @@ +# v2.21.0: Paging for the homepage, likes and admin dashboard + +## Why + +The production site has 141 notes (140 published), but: + +| Page | Showed | Cause | +|---|---|---| +| Homepage `/` | the 20 newest | `list_notes(..., limit=20)`, no paging | +| `/likes` | the 50 newest likes | `list_notes(..., limit=50)`, no paging | +| Admin dashboard | the 50 newest, as "Page 1 of 2" | it paged the result of `list_notes()`, whose default `limit` is 50 (since v1.6.0) | + +So 120 published notes couldn't be reached from the homepage, and 91 notes +couldn't be reached from the admin list. + +## How the homepage displays it + +- The same note previews as before, 20 per page, newest first. Likes stay on + `/likes`, drafts are never shown. +- Below the last note: `← Newer` · `Page 2 of 7` · `Older →`. Page 1 has no + "Newer" link, the last page has no "Older" link, and a site with one page + of notes shows no pager at all. +- URLs: page 1 is `/` (and `/?page=1` redirects there with a 301). Later + pages are `/?page=N`. Their browser title is `Page N · `. +- `/likes` works the same way with 50 per page (`Likes · Page N · …`). +- The links are `
diff --git a/templates/partials/pager.html b/templates/partials/pager.html new file mode 100644 index 0000000..984ad89 --- /dev/null +++ b/templates/partials/pager.html @@ -0,0 +1,15 @@ +{# Newer/older links for paged public lists (v2.21.0). Page 1 is the bare + URL. rel=prev/next let IndieWeb readers page through the h-feed. #} +{% macro pager(endpoint, page, total_pages) %} +{% if total_pages > 1 %} + +{% endif %} +{% endmacro %} diff --git a/tests/test_paging.py b/tests/test_paging.py new file mode 100644 index 0000000..d1e8818 --- /dev/null +++ b/tests/test_paging.py @@ -0,0 +1,184 @@ +""" +Paging of the homepage, likes page and admin dashboard (v2.21.0) + +Before v2.21.0 the homepage showed only the 20 newest notes, the likes page +the 50 newest likes, and the admin dashboard paged list_notes()'s default +50 newest notes, so older notes were unreachable from all three. +""" + +import re +from datetime import datetime, timedelta, timezone + +import pytest + +from starpunk import create_app +from starpunk.auth import create_session +from starpunk.notes import count_notes, create_note + +START = datetime(2026, 1, 1, tzinfo=timezone.utc) + + +@pytest.fixture +def app(tmp_path): + data = tmp_path / "data" + data.mkdir(parents=True, exist_ok=True) + return create_app(config={ + "TESTING": True, + "DATABASE_PATH": data / "starpunk.db", + "DATA_PATH": data, + "NOTES_PATH": data / "notes", + "SESSION_SECRET": "test-secret-key", + "ADMIN_ME": "https://test.example.com", + "SITE_URL": "http://localhost:5000", + "SITE_NAME": "Test Site", + "DEV_MODE": False, + }) + + +@pytest.fixture +def client(app): + return app.test_client() + + +def make_notes(app, published=0, drafts=0, likes=0): + """Notes one minute apart, oldest first; content says which is which""" + n = 0 + with app.app_context(): + for i in range(published): + create_note(content=f"Published note {i:03d}", published=True, + created_at=START + timedelta(minutes=n)) + n += 1 + for i in range(drafts): + create_note(content=f"Draft note {i:03d}", published=False, + created_at=START + timedelta(minutes=n)) + n += 1 + for i in range(likes): + create_note(content=f"Liked thing {i:03d}", published=True, + like_of=f"https://example.com/liked/{i}", + created_at=START + timedelta(minutes=n)) + n += 1 + + +def shown(html, prefix): + return sorted(set(re.findall(rf"{prefix} (\d{{3}})", html))) + + +def rel_link(html, rel): + m = re.search(rf'Page 2 · Test Site" in html + + def test_last_page_has_the_oldest(self, client): + html = client.get("/?page=3").get_data(as_text=True) + assert shown(html, "Published note") == [f"{i:03d}" for i in range(0, 5)] + assert rel_link(html, "next") is None + assert rel_link(html, "prev") == "/?page=2" + + def test_every_published_note_reachable_once(self, client): + seen = [] + for p in ("/", "/?page=2", "/?page=3"): + seen += re.findall(r"Published note (\d{3})", client.get(p).get_data(as_text=True)) + assert sorted(set(seen)) == [f"{i:03d}" for i in range(45)] + + def test_drafts_and_likes_never_listed(self, client): + for p in ("/", "/?page=2", "/?page=3"): + html = client.get(p).get_data(as_text=True) + assert "Draft note" not in html + assert "Liked thing" not in html + + def test_page_one_redirects_to_bare_url(self, client): + r = client.get("/?page=1") + assert r.status_code == 301 + assert r.headers["Location"].endswith("/") + assert "page=" not in r.headers["Location"] + + @pytest.mark.parametrize("page", ["4", "0", "-1", "abc", "2.5", ""]) + def test_bad_or_out_of_range_page_is_404(self, client, page): + assert client.get(f"/?page={page}").status_code == 404 + + +class TestHomepageSmall: + def test_no_pager_when_one_page(self, app, client): + make_notes(app, published=5) + html = client.get("/").get_data(as_text=True) + assert 'class="pager"' not in html + assert "Test Site" in html + + def test_empty_site_still_renders(self, client): + r = client.get("/") + assert r.status_code == 200 + assert "No notes published yet" in r.get_data(as_text=True) + + +class TestLikesPaging: + def test_likes_page_two(self, app, client): + make_notes(app, published=2, likes=55) # 50 per page + first = client.get("/likes").get_data(as_text=True) + assert len(shown(first, "Liked thing")) == 50 + assert rel_link(first, "next") == "/likes?page=2" + second = client.get("/likes?page=2").get_data(as_text=True) + assert shown(second, "Liked thing") == [f"{i:03d}" for i in range(0, 5)] + assert rel_link(second, "prev") == "/likes" + assert "Published note" not in first + second + assert client.get("/likes?page=3").status_code == 404 + + +class TestAdminDashboardPaging: + @pytest.fixture + def admin(self, app, client): + with app.test_request_context(): + token = create_session("https://test.example.com") + client.set_cookie("starpunk_session", token) + return client + + def test_all_notes_reachable_past_fifty(self, app, admin): + # 60 published + 5 drafts = 65 notes = 3 pages of 25 + make_notes(app, published=60, drafts=5) + html = admin.get("/admin/").get_data(as_text=True) + assert "Page 1 of 3" in html + last = admin.get("/admin/?page=3").get_data(as_text=True) + # the 15 oldest notes, which the old 50-note cap hid + assert shown(last, "Published note") == [f"{i:03d}" for i in range(0, 15)] + + def test_drafts_listed(self, app, admin): + make_notes(app, published=3, drafts=2) + assert "Draft note 000" in admin.get("/admin/").get_data(as_text=True) + + @pytest.mark.parametrize("page,expected", [("99", "Page 3 of 3"), ("abc", "Page 1 of 3"), + ("0", "Page 1 of 3")]) + def test_bad_page_falls_back(self, app, admin, page, expected): + make_notes(app, published=60) + r = admin.get(f"/admin/?page={page}") + assert r.status_code == 200 + assert expected in r.get_data(as_text=True)