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.
103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
"""Schemas for the Item model (admin area, see ``api.v1.admin``)."""
|
|
|
|
from datetime import datetime
|
|
from urllib.parse import urlsplit
|
|
|
|
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
|
|
|
|
|
def _clean_title(value: str) -> str:
|
|
stripped = value.strip()
|
|
if not stripped:
|
|
raise ValueError("title must not be empty")
|
|
return stripped
|
|
|
|
|
|
def _clean_url(value: str | None) -> str:
|
|
"""Normalize an optional URL: empty/blank -> ``""``, otherwise it must
|
|
be an absolute ``http://`` or ``https://`` URL."""
|
|
if value is None:
|
|
return ""
|
|
stripped = value.strip()
|
|
if not stripped:
|
|
return ""
|
|
parsed = urlsplit(stripped)
|
|
if parsed.scheme not in ("http", "https") or not parsed.netloc:
|
|
raise ValueError("must be an absolute http:// or https:// URL")
|
|
return stripped
|
|
|
|
|
|
class ItemBase(BaseModel):
|
|
"""Fields shared by the item create schema and the read model."""
|
|
|
|
title: str = Field(..., min_length=1, max_length=300)
|
|
url: str = Field(default="", max_length=500)
|
|
points: int = Field(default=0, ge=0)
|
|
content: str = Field(default="", description="HTML body from the Lexical editor.")
|
|
|
|
@field_validator("title")
|
|
@classmethod
|
|
def _strip_title(cls, value: str) -> str:
|
|
return _clean_title(value)
|
|
|
|
@field_validator("url")
|
|
@classmethod
|
|
def _validate_url(cls, value: str) -> str:
|
|
return _clean_url(value)
|
|
|
|
|
|
class ItemCreate(ItemBase):
|
|
"""Payload of ``POST /api/v1/admin/items``.
|
|
|
|
``username`` is optional: when omitted the item belongs to the
|
|
authenticated admin.
|
|
"""
|
|
|
|
username: str | None = Field(default=None, min_length=1, max_length=60)
|
|
|
|
|
|
class ItemUpdate(BaseModel):
|
|
"""Payload of ``PUT /api/v1/admin/items/{id}`` (all fields optional)."""
|
|
|
|
title: str | None = Field(default=None, max_length=300)
|
|
url: str | None = Field(default=None, max_length=500)
|
|
points: int | None = Field(default=None, ge=0)
|
|
username: str | None = Field(default=None, min_length=1, max_length=60)
|
|
content: str | None = None
|
|
|
|
@field_validator("title")
|
|
@classmethod
|
|
def _strip_title(cls, value: str | None) -> str | None:
|
|
if value is None:
|
|
return None
|
|
return _clean_title(value)
|
|
|
|
@field_validator("url")
|
|
@classmethod
|
|
def _validate_url(cls, value: str | None) -> str | None:
|
|
return None if value is None else _clean_url(value)
|
|
|
|
|
|
class ItemRead(BaseModel):
|
|
"""Item as returned by the admin API."""
|
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
id: int
|
|
title: str
|
|
url: str
|
|
points: int
|
|
username: str
|
|
content: str
|
|
created_at: datetime
|
|
|
|
|
|
class ItemAdminList(BaseModel):
|
|
"""Paginated item list of ``GET /api/v1/admin/items``."""
|
|
|
|
items: list[ItemRead]
|
|
total: int
|
|
page: int
|
|
page_size: int
|
|
total_pages: int
|