new web ract

This commit is contained in:
jze9
2026-05-15 03:31:28 +05:00
parent de78624495
commit 2335497226
58 changed files with 7397 additions and 406 deletions

View File

@@ -21,6 +21,7 @@ class ArticleIn(BaseModel):
content: str = ""
excerpt: str = ""
cover_url: Optional[str] = None
source_url: Optional[str] = None
font_family: str = "Merriweather"
category_id: Optional[str] = None
tag_names: list[str] = []
@@ -35,6 +36,7 @@ def _article_dict(a: Article) -> dict:
"content": a.content,
"excerpt": a.excerpt,
"cover_url": a.cover_url,
"source_url": a.source_url,
"font_family": a.font_family,
"status": a.status.value,
"view_count": a.view_count,
@@ -73,14 +75,19 @@ async def list_articles(
offset: int = Query(0, ge=0),
limit: int = Query(50, ge=1, le=200),
status: Optional[ArticleStatus] = Query(None),
q: Optional[str] = Query(None, max_length=200),
db: AsyncSession = Depends(get_db),
_: str = Depends(require_admin),
):
q = select(Article).options(selectinload(Article.category), selectinload(Article.tags))
from sqlalchemy import or_
stmt = select(Article).options(selectinload(Article.category), selectinload(Article.tags))
if status:
q = q.where(Article.status == status)
total = (await db.execute(select(func.count()).select_from(q.subquery()))).scalar_one()
articles = (await db.execute(q.order_by(Article.created_at.desc()).offset(offset).limit(limit))).scalars().all()
stmt = stmt.where(Article.status == status)
if q:
pattern = f"%{q}%"
stmt = stmt.where(or_(Article.title.ilike(pattern), Article.excerpt.ilike(pattern)))
total = (await db.execute(select(func.count()).select_from(stmt.subquery()))).scalar_one()
articles = (await db.execute(stmt.order_by(Article.created_at.desc()).offset(offset).limit(limit))).scalars().all()
return {
"total": total,
"offset": offset,
@@ -115,6 +122,7 @@ async def create_article(
content=data.content,
excerpt=data.excerpt,
cover_url=data.cover_url,
source_url=data.source_url,
font_family=data.font_family,
status=data.status,
category=cat,
@@ -173,7 +181,8 @@ async def update_article(
article.slug = slug
article.content = data.content
article.excerpt = data.excerpt
article.cover_url = data.cover_url
article.cover_url = data.cover_url
article.source_url = data.source_url
article.font_family = data.font_family
article.status = data.status
article.category = cat