From df82280121877afa157408355ad88e7acdfd3bbd Mon Sep 17 00:00:00 2001 From: nstarikov Date: Mon, 24 Aug 2026 11:59:38 +0300 Subject: [PATCH] feat: allow editing article slugs --- admin/src/App.tsx | 23 +++++++++++++++++++++++ app/routes.py | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/admin/src/App.tsx b/admin/src/App.tsx index b6e4ee8..579a196 100644 --- a/admin/src/App.tsx +++ b/admin/src/App.tsx @@ -1269,6 +1269,7 @@ export default function App() { const [chapterPlan, setChapterPlan] = useState<{ video: Video; chapters: VideoChapter[]; recreateDrafts: boolean } | null>(null) const [selectedChapterIds, setSelectedChapterIds] = useState>(new Set()) const [editorTitle, setEditorTitle] = useState("") + const [editorSlug, setEditorSlug] = useState("") const [publishAt, setPublishAt] = useState("") const [editorDirty, setEditorDirty] = useState(false) const [editorBusy, setEditorBusy] = useState(false) @@ -1692,6 +1693,7 @@ export default function App() { async function initializeArticleEditor(result: ArticleDetail) { setSelectedArticle(result) setEditorTitle(result.title) + setEditorSlug(result.slug) setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue()) setEditorDirty(false) setArticleCloseConfirmOpen(false) @@ -2194,12 +2196,14 @@ export default function App() { method: "PATCH", body: JSON.stringify({ title: editorTitle, + slug: editorSlug, featureImageUrl: selectedArticle.featureImageUrl || "", editableHtml: editorDataToHtml(editorData ?? htmlToEditorData("", editorTitle), editorTitle), }), }) setSelectedArticle(result) setEditorTitle(result.title) + setEditorSlug(result.slug) setEditorDirty(false) await refresh() if (showMessage) { @@ -2875,6 +2879,25 @@ export default function App() {

Редактирование статьи

{editorDirty ? "Есть правки" : "Сохранено"} +

Редактируйте блоки как в Medium. У изображения доступны случайный кадр и перегенерация по смыслу текста.

diff --git a/app/routes.py b/app/routes.py index 3471562..5cb9dc3 100644 --- a/app/routes.py +++ b/app/routes.py @@ -56,6 +56,22 @@ ALLOWED_VIDEO_EXTENSIONS = {".mp4", ".webm", ".mov", ".mkv"} MAX_VIDEO_UPLOAD_BYTES = 10 * 1024 * 1024 * 1024 VIDEO_UPLOAD_CHUNK_BYTES = 1024 * 1024 VIDEO_UPLOAD_LOG_INTERVAL_BYTES = 5 * 1024 * 1024 +ARTICLE_SLUG_PATTERN = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$") +ARTICLE_SLUG_MAX_LENGTH = 240 + + +def normalize_article_slug(value: str) -> str: + slug = value.strip().lower() + if not slug: + raise HTTPException(status_code=422, detail="Slug не может быть пустым.") + if len(slug) > ARTICLE_SLUG_MAX_LENGTH: + raise HTTPException(status_code=422, detail=f"Slug не может быть длиннее {ARTICLE_SLUG_MAX_LENGTH} символов.") + if not ARTICLE_SLUG_PATTERN.fullmatch(slug): + raise HTTPException( + status_code=422, + detail="Slug должен содержать только строчные латинские буквы, цифры и дефисы.", + ) + return slug class SourceIn(BaseModel): @@ -103,6 +119,7 @@ class ArticleUpdateIn(BaseModel): title: str editableHtml: str featureImageUrl: str | None = None + slug: str | None = None class ArticlePublishIn(BaseModel): @@ -1236,6 +1253,11 @@ def update_article(article_id: int, payload: ArticleUpdateIn, session: Session = if not article: raise HTTPException(status_code=404, detail="Article not found") title = payload.title.strip() + next_slug = normalize_article_slug(payload.slug) if payload.slug is not None else article.slug + if next_slug != article.slug: + conflict = session.scalar(select(Article).where(Article.slug == next_slug, Article.id != article.id)) + if conflict: + raise HTTPException(status_code=409, detail="Этот slug уже используется другой статьёй.") body = sanitize_article_editor_html(payload.editableHtml) body = strip_leading_duplicate_title(body, title) if not title: @@ -1259,6 +1281,7 @@ def update_article(article_id: int, payload: ArticleUpdateIn, session: Session = body = "\n".join(part for part in [body, audience_question_html] if part) lead, body_html = split_lead_and_body(body) article.title = title + article.slug = next_slug article.lead = lead article.body_html = body_html article.cta_html = signature_html.strip()