feat: add admin area with item CRUD, Lexical editor and access control (M6)
Backend: - Item model (title, url, points, username, content, created_at) - is_admin flag on User + startup migration for pre-existing DBs - get_current_admin dependency (401 without/invalid token, 403 for non-admins); server-side check on every admin endpoint - /api/v1/admin/items CRUD: pagination, case-insensitive title/url search, sort by title/created_at/points, in-memory rate limiting (1000 req/60s per admin -> 429) - seed_admin.py (ADMIN_USERNAME/ADMIN_PASSWORD), documented in backend/.env.example Frontend: - Auth context exposes is_admin; RequireAdmin route guard (redirects signed-out users, renders 403 page for non-admins) - AdminLayout with responsive dark sidebar; Admin link in header for admins only - AdminDashboard: paginated table (page size 10/20/50), 300ms debounced search, sortable title/points/created_at columns, edit/delete row actions - EditItemPage: create + edit with client-side validation and a Lexical (0.25) WYSIWYG editor (bold/italic/underline, H1-H6, lists, code blocks, links, undo/redo), optional debounced auto-save - DeleteConfirmation modal with warning icon and explicit "Yes, delete" confirmation - Unit tests (34) and mocked E2E workflow tests (10) All acceptance criteria for milestones/m6-setup-admin-area-prompt.md verified: 108 backend, 87 frontend unit and 29 E2E tests pass.
This commit is contained in:
1 parent
b186d0f797
commit
b440809548
38 files changed
+5415
-50
No files matched your search
@@ -4,6 +4,7 @@ from models.article import Article
|
||||
from models.author import Author
|
||||
from models.base import Base
|
||||
from models.category import Category, slugify
|
||||
from models.item import Item
|
||||
from models.user import User
|
||||
|
||||
__all__ = ["Article", "Author", "Base", "Category", "User", "slugify"]
|
||||
__all__ = ["Article", "Author", "Base", "Category", "Item", "User", "slugify"]
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Item model: a news story managed through the admin area.
|
||||
|
||||
Unlike ``Article`` (long-form pieces filed under an author and category),
|
||||
an ``Item`` is a lightweight news entry — headline, optional external URL,
|
||||
community points and an optional HTML body — created and maintained by
|
||||
administrators through the admin API (see ``api.v1.admin``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import DateTime, Integer, String, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from models.base import Base
|
||||
|
||||
|
||||
def _utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
class Item(Base):
|
||||
"""A news item (story) listed on the front page and managed by admins.
|
||||
|
||||
``username`` stores the (denormalized) name of the user the item
|
||||
belongs to, defaulting to the account that created it.
|
||||
"""
|
||||
|
||||
__tablename__ = "items"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(300), nullable=False, index=True)
|
||||
url: Mapped[str] = mapped_column(String(500), default="", nullable=False)
|
||||
points: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
username: Mapped[str] = mapped_column(String(60), default="anonymous", nullable=False)
|
||||
content: Mapped[str] = mapped_column(Text, default="", nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, nullable=False
|
||||
)
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover
|
||||
return f"<Item id={self.id} title={self.title!r}>"
|
||||
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import DateTime, String
|
||||
from sqlalchemy import Boolean, DateTime, String
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from models.base import Base
|
||||
@@ -26,6 +26,9 @@ class User(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
username: Mapped[str] = mapped_column(String(60), unique=True, index=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(128))
|
||||
#: Grants access to the admin area (``/api/v1/admin/*``). Never set by
|
||||
#: the registration endpoint; promote users via ``seed_admin.py``.
|
||||
is_admin: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), default=_utcnow, nullable=False
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user