feat: add manual article drafts
Some checks failed
Deploy Dzen assistant / deploy (push) Failing after 2s
Some checks failed
Deploy Dzen assistant / deploy (push) Failing after 2s
This commit is contained in:
parent
f3a557cf17
commit
6aac7d9e28
3 changed files with 105 additions and 8 deletions
|
|
@ -1202,8 +1202,10 @@ export default function App() {
|
||||||
})
|
})
|
||||||
const [videos, setVideos] = useState<Video[]>([])
|
const [videos, setVideos] = useState<Video[]>([])
|
||||||
const [articles, setArticles] = useState<Article[]>([])
|
const [articles, setArticles] = useState<Article[]>([])
|
||||||
|
const [manualDrafts, setManualDrafts] = useState<Article[]>([])
|
||||||
const [articlesHasMore, setArticlesHasMore] = useState(false)
|
const [articlesHasMore, setArticlesHasMore] = useState(false)
|
||||||
const [articlesLoading, setArticlesLoading] = useState(false)
|
const [articlesLoading, setArticlesLoading] = useState(false)
|
||||||
|
const [manualArticleCreating, setManualArticleCreating] = useState(false)
|
||||||
const [selectedLog, setSelectedLog] = useState<VideoLog | null>(null)
|
const [selectedLog, setSelectedLog] = useState<VideoLog | null>(null)
|
||||||
const [selectedArticle, setSelectedArticle] = useState<ArticleDetail | null>(null)
|
const [selectedArticle, setSelectedArticle] = useState<ArticleDetail | null>(null)
|
||||||
const [chapterPlan, setChapterPlan] = useState<{ video: Video; chapters: VideoChapter[]; recreateDrafts: boolean } | null>(null)
|
const [chapterPlan, setChapterPlan] = useState<{ video: Video; chapters: VideoChapter[]; recreateDrafts: boolean } | null>(null)
|
||||||
|
|
@ -1309,12 +1311,14 @@ export default function App() {
|
||||||
setArticlesLoading(true)
|
setArticlesLoading(true)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const [nextVideos, nextArticles] = await Promise.all([
|
const [nextVideos, nextArticles, nextManualDrafts] = await Promise.all([
|
||||||
api<Video[]>("/api/videos"),
|
api<Video[]>("/api/videos"),
|
||||||
api<ArticlesPage>(`/api/articles?status=published&limit=${ARTICLES_PAGE_SIZE}&offset=0`),
|
api<ArticlesPage>(`/api/articles?status=published&limit=${ARTICLES_PAGE_SIZE}&offset=0`),
|
||||||
|
api<ArticlesPage>("/api/articles?status=draft&manual=true&limit=50"),
|
||||||
])
|
])
|
||||||
setVideos(nextVideos)
|
setVideos(nextVideos)
|
||||||
setArticles(nextArticles.items)
|
setArticles(nextArticles.items)
|
||||||
|
setManualDrafts(nextManualDrafts.items)
|
||||||
articlesOffsetRef.current = nextArticles.items.length
|
articlesOffsetRef.current = nextArticles.items.length
|
||||||
articlesHasMoreRef.current = nextArticles.hasMore
|
articlesHasMoreRef.current = nextArticles.hasMore
|
||||||
setArticlesHasMore(nextArticles.hasMore)
|
setArticlesHasMore(nextArticles.hasMore)
|
||||||
|
|
@ -1627,8 +1631,7 @@ export default function App() {
|
||||||
setSelectedLog(null)
|
setSelectedLog(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function openArticlePreview(id: number) {
|
async function initializeArticleEditor(result: ArticleDetail) {
|
||||||
const result = await api<ArticleDetail>(`/api/articles/${id}`)
|
|
||||||
setSelectedArticle(result)
|
setSelectedArticle(result)
|
||||||
setEditorTitle(result.title)
|
setEditorTitle(result.title)
|
||||||
setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue())
|
setPublishAt(toDateTimeLocalValue(result.publishedAt) || currentDateTimeLocalValue())
|
||||||
|
|
@ -1637,7 +1640,7 @@ export default function App() {
|
||||||
setArticleMoreOpen(false)
|
setArticleMoreOpen(false)
|
||||||
appliedImageGenerationJobsRef.current = new Set()
|
appliedImageGenerationJobsRef.current = new Set()
|
||||||
try {
|
try {
|
||||||
const active = await fetch(`/api/articles/${id}/image-generations/active`)
|
const active = await fetch(`/api/articles/${result.id}/image-generations/active`)
|
||||||
if (active.status === 204) {
|
if (active.status === 204) {
|
||||||
setImageGenerationBatch(null)
|
setImageGenerationBatch(null)
|
||||||
} else if (active.ok) {
|
} else if (active.ok) {
|
||||||
|
|
@ -1648,6 +1651,21 @@ export default function App() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function openArticlePreview(id: number) {
|
||||||
|
await initializeArticleEditor(await api<ArticleDetail>(`/api/articles/${id}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createManualArticle() {
|
||||||
|
setManualArticleCreating(true)
|
||||||
|
try {
|
||||||
|
const article = await api<ArticleDetail>("/api/articles", { method: "POST" })
|
||||||
|
setManualDrafts((current) => [article, ...current.filter((currentArticle) => currentArticle.id !== article.id)])
|
||||||
|
await initializeArticleEditor(article)
|
||||||
|
} finally {
|
||||||
|
setManualArticleCreating(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function publishArticle(id: number, scheduledAt?: string) {
|
async function publishArticle(id: number, scheduledAt?: string) {
|
||||||
const result = await api<{ url: string; publishedAt?: string }>(`/api/articles/${id}/publish`, {
|
const result = await api<{ url: string; publishedAt?: string }>(`/api/articles/${id}/publish`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
|
@ -2542,8 +2560,16 @@ export default function App() {
|
||||||
|
|
||||||
<Card className="work-column min-w-0">
|
<Card className="work-column min-w-0">
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
|
<div className="flex items-start justify-between gap-4">
|
||||||
|
<div>
|
||||||
<CardTitle>Статьи</CardTitle>
|
<CardTitle>Статьи</CardTitle>
|
||||||
<CardDescription>Принятые материалы: уже вышедшие в RSS и отложенные по времени.</CardDescription>
|
<CardDescription>Черновики, принятые материалы и отложенные публикации.</CardDescription>
|
||||||
|
</div>
|
||||||
|
<Button size="sm" disabled={manualArticleCreating} onClick={() => void createManualArticle()}>
|
||||||
|
{manualArticleCreating ? <Loader2 className="animate-spin" data-icon="inline-start" /> : <FilePenLine data-icon="inline-start" />}
|
||||||
|
Создать материал
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent
|
<CardContent
|
||||||
className="work-list flex max-h-[72vh] flex-col gap-2 overflow-y-auto"
|
className="work-list flex max-h-[72vh] flex-col gap-2 overflow-y-auto"
|
||||||
|
|
@ -2555,6 +2581,32 @@ export default function App() {
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
{manualDrafts.length > 0 ? (
|
||||||
|
<section className="space-y-2 border-b pb-4">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Ручные черновики</p>
|
||||||
|
{manualDrafts.map((article) => (
|
||||||
|
<div key={article.id} className="article-item rounded-md border bg-background p-3">
|
||||||
|
<div className="flex items-start justify-between gap-3">
|
||||||
|
<button className="min-w-0 flex-1 text-left" onClick={() => void openArticlePreview(article.id)}>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<StatusBadge status="draft" />
|
||||||
|
<h3 className="min-w-0 flex-1 truncate text-sm font-semibold">{article.title}</h3>
|
||||||
|
</div>
|
||||||
|
<p className="mt-2 text-xs text-muted-foreground">Создано: {formatDateTime(article.createdAt)}</p>
|
||||||
|
</button>
|
||||||
|
<div className="flex shrink-0 gap-2">
|
||||||
|
<Button size="icon" variant="outline" title="Открыть редактор" aria-label="Открыть редактор" onClick={() => void openArticlePreview(article.id)}>
|
||||||
|
<FileText className="size-4" />
|
||||||
|
</Button>
|
||||||
|
<Button size="icon" variant="outline" title="Удалить" aria-label="Удалить" onClick={() => deleteArticle(article.id)}>
|
||||||
|
<Trash2 className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</section>
|
||||||
|
) : null}
|
||||||
{publishedArticles.length > 0 ? (
|
{publishedArticles.length > 0 ? (
|
||||||
publishedArticles.map((article) => (
|
publishedArticles.map((article) => (
|
||||||
<div key={article.id} className="article-item rounded-md border bg-background p-3">
|
<div key={article.id} className="article-item rounded-md border bg-background p-3">
|
||||||
|
|
|
||||||
|
|
@ -951,6 +951,7 @@ async def video_logs_ws(websocket: WebSocket, video_id: int) -> None:
|
||||||
@router.get("/api/articles")
|
@router.get("/api/articles")
|
||||||
def list_articles(
|
def list_articles(
|
||||||
status: str | None = Query(default=None),
|
status: str | None = Query(default=None),
|
||||||
|
manual: bool = Query(default=False),
|
||||||
limit: int = Query(default=10, ge=1, le=50),
|
limit: int = Query(default=10, ge=1, le=50),
|
||||||
offset: int = Query(default=0, ge=0),
|
offset: int = Query(default=0, ge=0),
|
||||||
session: Session = Depends(get_session),
|
session: Session = Depends(get_session),
|
||||||
|
|
@ -959,6 +960,8 @@ def list_articles(
|
||||||
filters = [Article.status != "rejected"]
|
filters = [Article.status != "rejected"]
|
||||||
if status:
|
if status:
|
||||||
filters.append(Article.status == status)
|
filters.append(Article.status == status)
|
||||||
|
if manual:
|
||||||
|
filters.append(Article.video_id.is_(None))
|
||||||
total = session.scalar(select(func.count(Article.id)).where(*filters)) or 0
|
total = session.scalar(select(func.count(Article.id)).where(*filters)) or 0
|
||||||
articles = session.scalars(
|
articles = session.scalars(
|
||||||
select(Article)
|
select(Article)
|
||||||
|
|
@ -983,6 +986,34 @@ def list_articles(
|
||||||
return {"items": items, "total": total, "limit": limit, "offset": offset, "hasMore": offset + len(items) < total}
|
return {"items": items, "total": total, "limit": limit, "offset": offset, "hasMore": offset + len(items) < total}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/api/articles", status_code=201)
|
||||||
|
def create_manual_article(session: Session = Depends(get_session), settings: Settings = Depends(get_settings)) -> dict:
|
||||||
|
signature_html = session.scalar(select(AppSetting.value).where(AppSetting.key == SETTING_SIGNATURE)) or ""
|
||||||
|
article = Article(
|
||||||
|
slug=f"manual-{uuid4().hex}",
|
||||||
|
title="Новый материал",
|
||||||
|
cta_html=sanitize_article_editor_html(signature_html),
|
||||||
|
status="draft",
|
||||||
|
)
|
||||||
|
session.add(article)
|
||||||
|
session.commit()
|
||||||
|
session.refresh(article)
|
||||||
|
return {
|
||||||
|
"id": article.id,
|
||||||
|
"title": article.title,
|
||||||
|
"slug": article.slug,
|
||||||
|
"status": article.status,
|
||||||
|
"videoId": None,
|
||||||
|
"videoSourceUrl": "",
|
||||||
|
"url": article_url(settings, article),
|
||||||
|
"featureImageUrl": article_cover_url(session, article),
|
||||||
|
"createdAt": article.created_at,
|
||||||
|
"publishedAt": article.published_at,
|
||||||
|
"contentHtml": render_article_html(article),
|
||||||
|
"editableHtml": editable_article_html(article),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/articles/{article_id}")
|
@router.get("/api/articles/{article_id}")
|
||||||
def get_article(article_id: int, session: Session = Depends(get_session), settings: Settings = Depends(get_settings)) -> dict:
|
def get_article(article_id: int, session: Session = Depends(get_session), settings: Settings = Depends(get_settings)) -> dict:
|
||||||
article = session.get(Article, article_id)
|
article = session.get(Article, article_id)
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@ from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app.config import Settings
|
from app.config import Settings
|
||||||
from app.database import Base
|
from app.database import Base
|
||||||
from app.models import Article, Video
|
from app.models import AppSetting, Article, Video
|
||||||
from app.routes import ArticleUpdateIn, editable_article_html, update_article
|
from app.routes import ArticleUpdateIn, create_manual_article, editable_article_html, update_article
|
||||||
from app.rss import VIDEO_PLACEHOLDER, render_article_html
|
from app.rss import VIDEO_PLACEHOLDER, render_article_html
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -61,6 +61,20 @@ class ArticleEditorContractTest(unittest.TestCase):
|
||||||
self.assertLess(html.index("После видео."), html.index("Ваше мнение:"))
|
self.assertLess(html.index("После видео."), html.index("Ваше мнение:"))
|
||||||
self.assertLess(html.index("Ваше мнение:"), html.index("Подпись."))
|
self.assertLess(html.index("Ваше мнение:"), html.index("Подпись."))
|
||||||
|
|
||||||
|
def test_manual_article_is_a_draft_without_video_and_snapshots_signature(self):
|
||||||
|
with Session(self.engine) as session:
|
||||||
|
session.add(AppSetting(key="article_signature_html", value="<p>Подпись редакции.</p>"))
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
result = create_manual_article(session, Settings(public_base_url="https://example.test"))
|
||||||
|
article = session.get(Article, result["id"])
|
||||||
|
|
||||||
|
self.assertEqual(article.status, "draft")
|
||||||
|
self.assertIsNone(article.video_id)
|
||||||
|
self.assertEqual(article.title, "Новый материал")
|
||||||
|
self.assertEqual(article.cta_html, "<p>Подпись редакции.</p>")
|
||||||
|
self.assertIn("Подпись редакции.", result["editableHtml"])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue