Update configuration and enhance newsletter generation features

- Change WIKI_API_URL in .env.example and config to point to the new API endpoint.
- Set COOKIE_SECURE to false for local development in .env.example and config.
- Improve the newsletter generation logic to handle new and edited articles separately.
- Add display options for showing date, user, and category in the newsletter output.
- Enhance error handling for Wiki API requests and update templates for better user feedback.
- Update CSS for new UI elements and improve overall layout in dashboard and login pages.
This commit is contained in:
smueller
2026-07-03 12:47:35 +02:00
parent 0d7729287f
commit e5e4d812c7
13 changed files with 629 additions and 91 deletions

View File

@@ -6,6 +6,14 @@ import httpx
from app.core.config import settings
USER_AGENT = "TK-Newsletter-Admin/1.0 (internal; contact@thomas-krenn.com)"
class WikiFetchError(Exception):
def __init__(self, message: str) -> None:
self.message = message
super().__init__(message)
class WikiService:
def __init__(self) -> None:
@@ -17,7 +25,7 @@ class WikiService:
rcstart = now.strftime("%Y-%m-%dT%H:%M:%SZ")
rcend = start.strftime("%Y-%m-%dT%H:%M:%SZ")
params = {
params: dict[str, str] = {
"action": "query",
"format": "json",
"list": "recentchanges",
@@ -34,18 +42,31 @@ class WikiService:
rows: list[dict[str, Any]] = []
cont: dict[str, Any] = {}
async with httpx.AsyncClient(timeout=20.0) as client:
while True:
response = await client.get(self.api_url, params={**params, **cont})
response.raise_for_status()
payload = response.json()
rows.extend(payload.get("query", {}).get("recentchanges", []))
if "continue" not in payload:
break
cont = payload["continue"]
headers = {"User-Agent": USER_AGENT}
try:
async with httpx.AsyncClient(timeout=30.0, headers=headers, follow_redirects=True) as client:
while True:
response = await client.get(self.api_url, params={**params, **cont})
if response.status_code >= 400:
raise WikiFetchError(
f"Wiki-API nicht erreichbar (HTTP {response.status_code}). "
f"Prüfe WIKI_API_URL in der Konfiguration."
)
payload = response.json()
if "error" in payload:
code = payload["error"].get("code", "unknown")
info = payload["error"].get("info", "Unbekannter API-Fehler")
raise WikiFetchError(f"Wiki-API-Fehler ({code}): {info}")
titles = sorted({item["title"] for item in rows if item.get("title")})
categories = await self._get_categories_for_titles(client, titles)
rows.extend(payload.get("query", {}).get("recentchanges", []))
if "continue" not in payload:
break
cont = payload["continue"]
titles = sorted({item["title"] for item in rows if item.get("title")})
categories = await self._get_categories_for_titles(client, titles)
except httpx.RequestError as exc:
raise WikiFetchError(f"Wiki-API-Anfrage fehlgeschlagen: {exc}") from exc
for row in rows:
row["categories"] = categories.get(row.get("title", ""), [])
@@ -67,12 +88,15 @@ class WikiService:
"titles": "|".join(chunk),
}
response = await client.get(self.api_url, params=params)
response.raise_for_status()
if response.status_code >= 400:
continue
pages = response.json().get("query", {}).get("pages", {})
for page in pages.values():
title = page.get("title")
if not title:
continue
raw_categories = page.get("categories", [])
result[title] = [c.get("title", "").replace("Kategorie:", "") for c in raw_categories if c.get("title")]
result[title] = [
c.get("title", "").replace("Kategorie:", "") for c in raw_categories if c.get("title")
]
return result