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 <noreply@anthropic.com>
This commit is contained in:
1 parent
9074c9a08c
commit
615354d17e
11 files changed
+373
-60
No files matched your search
+10
-12
@@ -14,6 +14,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
from starpunk.auth import require_auth
|
||||
from starpunk.notes import (
|
||||
count_notes,
|
||||
create_note,
|
||||
delete_note,
|
||||
list_notes,
|
||||
@@ -60,21 +61,18 @@ def dashboard():
|
||||
from flask import current_app
|
||||
import math
|
||||
|
||||
# Get pagination params (v1.6.0)
|
||||
page = int(request.args.get('page', 1))
|
||||
# Pagination (v1.6.0). Until v2.21.0 this paged list_notes()'s default
|
||||
# 50 newest notes, so older notes never showed; now it counts all notes
|
||||
# and fetches only the requested page. A bad ?page= falls back to the
|
||||
# nearest valid page instead of a server error.
|
||||
per_page = 25
|
||||
|
||||
# Get all notes (published and drafts)
|
||||
all_notes = list_notes()
|
||||
|
||||
# Calculate pagination
|
||||
total_notes = len(all_notes)
|
||||
total_notes = count_notes()
|
||||
total_pages = math.ceil(total_notes / per_page) if total_notes > 0 else 1
|
||||
raw_page = request.args.get('page', '1')
|
||||
page = int(raw_page) if raw_page.isdigit() else 1
|
||||
page = min(max(page, 1), total_pages)
|
||||
|
||||
# Slice for current page
|
||||
start_idx = (page - 1) * per_page
|
||||
end_idx = start_idx + per_page
|
||||
notes = all_notes[start_idx:end_idx]
|
||||
notes = list_notes(limit=per_page, offset=(page - 1) * per_page)
|
||||
|
||||
# Get pending webmention count (v1.6.0)
|
||||
webmention_pending_count = 0
|
||||
|
||||
+57
-32
@@ -10,7 +10,7 @@ from datetime import datetime, timedelta, timezone
|
||||
|
||||
from flask import Blueprint, abort, redirect, render_template, Response, current_app, request, send_from_directory, url_for
|
||||
|
||||
from starpunk.notes import list_notes, get_note
|
||||
from starpunk.notes import count_notes, list_notes, get_note
|
||||
from starpunk.websub import embed_hub
|
||||
from starpunk.feed import generate_feed_streaming # Legacy RSS
|
||||
from starpunk.feeds import (
|
||||
@@ -279,36 +279,65 @@ def preview(name: str):
|
||||
return response
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def index():
|
||||
"""
|
||||
Homepage displaying recent published notes with media
|
||||
HOME_PER_PAGE = 20
|
||||
LIKES_PER_PAGE = 50
|
||||
|
||||
Returns:
|
||||
Rendered homepage template with note list including media
|
||||
|
||||
Template: templates/index.html
|
||||
Microformats: h-feed containing h-entry items with u-photo
|
||||
def _paged_notes(endpoint: str, per_page: int, likes: bool):
|
||||
"""
|
||||
Published notes for the requested ?page= of a public list (v2.21.0)
|
||||
|
||||
Returns (notes, page, total_pages), or a redirect Response for ?page=1
|
||||
(page 1 lives at the bare URL). A page that isn't a positive whole
|
||||
number, or is past the last page, is a 404.
|
||||
"""
|
||||
raw = request.args.get("page")
|
||||
if raw is None:
|
||||
page = 1
|
||||
elif not raw.isdigit() or int(raw) < 1:
|
||||
abort(404)
|
||||
else:
|
||||
page = int(raw)
|
||||
if page == 1:
|
||||
return redirect(url_for(endpoint), code=301)
|
||||
total = count_notes(published_only=True, likes=likes)
|
||||
total_pages = max(1, -(-total // per_page))
|
||||
if page > total_pages:
|
||||
abort(404)
|
||||
notes = list_notes(published_only=True, limit=per_page,
|
||||
offset=(page - 1) * per_page, likes=likes)
|
||||
return notes, page, total_pages
|
||||
|
||||
|
||||
def _prepare_for_listing(notes) -> None:
|
||||
"""Media, tags, contexts and response counts for listed notes"""
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
# Get recent published notes (limit 20); likes have their own page (v2.5.0)
|
||||
notes = list_notes(published_only=True, limit=20, likes=False)
|
||||
|
||||
# Attach media and tags to each note for display
|
||||
for note in notes:
|
||||
media = get_note_media(note.id)
|
||||
# Use object.__setattr__ since Note is frozen dataclass
|
||||
object.__setattr__(note, 'media', media)
|
||||
|
||||
# Attach tags (v1.3.0 Phase 3)
|
||||
tags = get_note_tags(note.id)
|
||||
object.__setattr__(note, '_cached_tags', tags)
|
||||
|
||||
object.__setattr__(note, 'media', get_note_media(note.id))
|
||||
object.__setattr__(note, '_cached_tags', get_note_tags(note.id))
|
||||
attach_contexts(notes)
|
||||
attach_response_counts(notes)
|
||||
return render_template("index.html", notes=notes)
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def index():
|
||||
"""
|
||||
Homepage: published notes, newest first, 20 per page (paged in v2.21.0)
|
||||
|
||||
Likes have their own page (v2.5.0). Older notes are at /?page=2 and on.
|
||||
|
||||
Template: templates/index.html
|
||||
Microformats: h-feed containing h-entry items, with rel=prev/next paging
|
||||
"""
|
||||
result = _paged_notes("public.index", HOME_PER_PAGE, likes=False)
|
||||
if not isinstance(result, tuple): # redirect for ?page=1
|
||||
return result
|
||||
notes, page, total_pages = result
|
||||
_prepare_for_listing(notes)
|
||||
return render_template("index.html", notes=notes, page=page, total_pages=total_pages)
|
||||
|
||||
|
||||
def attach_response_counts(notes) -> None:
|
||||
@@ -341,17 +370,13 @@ def attach_contexts(notes) -> None:
|
||||
|
||||
@bp.route("/likes")
|
||||
def likes():
|
||||
"""Likes, kept off the home page and feeds (v2.5.0)"""
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
notes = list_notes(published_only=True, limit=50, likes=True)
|
||||
for note in notes:
|
||||
object.__setattr__(note, 'media', get_note_media(note.id))
|
||||
object.__setattr__(note, '_cached_tags', get_note_tags(note.id))
|
||||
attach_contexts(notes)
|
||||
attach_response_counts(notes)
|
||||
return render_template("likes.html", notes=notes)
|
||||
"""Likes, kept off the home page and feeds (v2.5.0); paged in v2.21.0"""
|
||||
result = _paged_notes("public.likes", LIKES_PER_PAGE, likes=True)
|
||||
if not isinstance(result, tuple): # redirect for ?page=1
|
||||
return result
|
||||
notes, page, total_pages = result
|
||||
_prepare_for_listing(notes)
|
||||
return render_template("likes.html", notes=notes, page=page, total_pages=total_pages)
|
||||
|
||||
|
||||
def _note_was_deleted(slug: str) -> bool:
|
||||
|
||||
Reference in new issue
Block a user