feat: allow editing article slugs
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 31s
All checks were successful
Deploy Dzen assistant / deploy (push) Successful in 31s
This commit is contained in:
parent
dc2184d90b
commit
df82280121
2 changed files with 46 additions and 0 deletions
|
|
@ -1269,6 +1269,7 @@ export default function App() {
|
||||||
const [chapterPlan, setChapterPlan] = useState<{ video: Video; chapters: VideoChapter[]; recreateDrafts: boolean } | null>(null)
|
const [chapterPlan, setChapterPlan] = useState<{ video: Video; chapters: VideoChapter[]; recreateDrafts: boolean } | null>(null)
|
||||||
const [selectedChapterIds, setSelectedChapterIds] = useState<Set<string>>(new Set())
|
const [selectedChapterIds, setSelectedChapterIds] = useState<Set<string>>(new Set())
|
||||||
const [editorTitle, setEditorTitle] = useState("")
|
const [editorTitle, setEditorTitle] = useState("")
|
||||||
|
const [editorSlug, setEditorSlug] = useState("")
|
||||||
const [publishAt, setPublishAt] = useState("")
|
const [publishAt, setPublishAt] = useState("")
|
||||||
const [editorDirty, setEditorDirty] = useState(false)
|
const [editorDirty, setEditorDirty] = useState(false)
|
||||||
const [editorBusy, setEditorBusy] = useState(false)
|
const [editorBusy, setEditorBusy] = useState(false)
|
||||||
|
|
@ -1692,6 +1693,7 @@ export default function App() {
|
||||||
async function initializeArticleEditor(result: ArticleDetail) {
|
async function initializeArticleEditor(result: ArticleDetail) {
|
||||||
setSelectedArticle(result)
|
setSelectedArticle(result)
|
||||||
setEditorTitle(result.title)
|
setEditorTitle(result.title)
|
||||||
|
setEditorSlug(result.slug)
|
||||||
setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue())
|
setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue())
|
||||||
setEditorDirty(false)
|
setEditorDirty(false)
|
||||||
setArticleCloseConfirmOpen(false)
|
setArticleCloseConfirmOpen(false)
|
||||||
|
|
@ -2194,12 +2196,14 @@ export default function App() {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
title: editorTitle,
|
title: editorTitle,
|
||||||
|
slug: editorSlug,
|
||||||
featureImageUrl: selectedArticle.featureImageUrl || "",
|
featureImageUrl: selectedArticle.featureImageUrl || "",
|
||||||
editableHtml: editorDataToHtml(editorData ?? htmlToEditorData("", editorTitle), editorTitle),
|
editableHtml: editorDataToHtml(editorData ?? htmlToEditorData("", editorTitle), editorTitle),
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
setSelectedArticle(result)
|
setSelectedArticle(result)
|
||||||
setEditorTitle(result.title)
|
setEditorTitle(result.title)
|
||||||
|
setEditorSlug(result.slug)
|
||||||
setEditorDirty(false)
|
setEditorDirty(false)
|
||||||
await refresh()
|
await refresh()
|
||||||
if (showMessage) {
|
if (showMessage) {
|
||||||
|
|
@ -2875,6 +2879,25 @@ export default function App() {
|
||||||
<h2 className="min-w-0 flex-1 truncate text-lg font-semibold tracking-normal">Редактирование статьи</h2>
|
<h2 className="min-w-0 flex-1 truncate text-lg font-semibold tracking-normal">Редактирование статьи</h2>
|
||||||
<Badge variant="secondary" className="article-save-state">{editorDirty ? "Есть правки" : "Сохранено"}</Badge>
|
<Badge variant="secondary" className="article-save-state">{editorDirty ? "Есть правки" : "Сохранено"}</Badge>
|
||||||
</div>
|
</div>
|
||||||
|
<label className="mt-3 block max-w-3xl">
|
||||||
|
<span className="mb-1 block text-xs font-medium text-muted-foreground">Slug публикации</span>
|
||||||
|
<span className="flex min-w-0 items-center rounded-md border bg-background px-3 shadow-sm focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-1">
|
||||||
|
<span className="shrink-0 text-sm text-muted-foreground">/posts/</span>
|
||||||
|
<input
|
||||||
|
className="min-w-0 flex-1 bg-transparent px-1 py-2 text-sm text-foreground outline-none"
|
||||||
|
value={editorSlug}
|
||||||
|
maxLength={240}
|
||||||
|
spellCheck={false}
|
||||||
|
autoCapitalize="none"
|
||||||
|
autoCorrect="off"
|
||||||
|
onChange={(event) => {
|
||||||
|
setEditorSlug(event.target.value)
|
||||||
|
setEditorDirty(true)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</span>
|
||||||
|
<span className="mt-1 block text-xs text-muted-foreground">Строчные латинские буквы, цифры и дефисы.</span>
|
||||||
|
</label>
|
||||||
<p className="mt-1 text-sm text-muted-foreground">Редактируйте блоки как в Medium. У изображения доступны случайный кадр и перегенерация по смыслу текста.</p>
|
<p className="mt-1 text-sm text-muted-foreground">Редактируйте блоки как в Medium. У изображения доступны случайный кадр и перегенерация по смыслу текста.</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="article-modal-actions hidden shrink-0 flex-wrap justify-end gap-2 sm:flex">
|
<div className="article-modal-actions hidden shrink-0 flex-wrap justify-end gap-2 sm:flex">
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,22 @@ ALLOWED_VIDEO_EXTENSIONS = {".mp4", ".webm", ".mov", ".mkv"}
|
||||||
MAX_VIDEO_UPLOAD_BYTES = 10 * 1024 * 1024 * 1024
|
MAX_VIDEO_UPLOAD_BYTES = 10 * 1024 * 1024 * 1024
|
||||||
VIDEO_UPLOAD_CHUNK_BYTES = 1024 * 1024
|
VIDEO_UPLOAD_CHUNK_BYTES = 1024 * 1024
|
||||||
VIDEO_UPLOAD_LOG_INTERVAL_BYTES = 5 * 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):
|
class SourceIn(BaseModel):
|
||||||
|
|
@ -103,6 +119,7 @@ class ArticleUpdateIn(BaseModel):
|
||||||
title: str
|
title: str
|
||||||
editableHtml: str
|
editableHtml: str
|
||||||
featureImageUrl: str | None = None
|
featureImageUrl: str | None = None
|
||||||
|
slug: str | None = None
|
||||||
|
|
||||||
|
|
||||||
class ArticlePublishIn(BaseModel):
|
class ArticlePublishIn(BaseModel):
|
||||||
|
|
@ -1236,6 +1253,11 @@ def update_article(article_id: int, payload: ArticleUpdateIn, session: Session =
|
||||||
if not article:
|
if not article:
|
||||||
raise HTTPException(status_code=404, detail="Article not found")
|
raise HTTPException(status_code=404, detail="Article not found")
|
||||||
title = payload.title.strip()
|
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 = sanitize_article_editor_html(payload.editableHtml)
|
||||||
body = strip_leading_duplicate_title(body, title)
|
body = strip_leading_duplicate_title(body, title)
|
||||||
if not 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)
|
body = "\n".join(part for part in [body, audience_question_html] if part)
|
||||||
lead, body_html = split_lead_and_body(body)
|
lead, body_html = split_lead_and_body(body)
|
||||||
article.title = title
|
article.title = title
|
||||||
|
article.slug = next_slug
|
||||||
article.lead = lead
|
article.lead = lead
|
||||||
article.body_html = body_html
|
article.body_html = body_html
|
||||||
article.cta_html = signature_html.strip()
|
article.cta_html = signature_html.strip()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue