35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from typing import List
|
|
|
|
from ninja import Router
|
|
from ninja.pagination import paginate, PageNumberPagination
|
|
|
|
from article.models import Article
|
|
from article.schema import ArticleSchema, ArticleCreateSchema
|
|
|
|
# Create your views here.
|
|
|
|
# Create your views here.
|
|
router = Router(tags=['content'])
|
|
|
|
|
|
@router.get("/article", response=List[ArticleSchema], description="获取文章列表")
|
|
@paginate(PageNumberPagination, page_size=50)
|
|
def list_articles(request, ):
|
|
# 安全校验 per_page(防止过大
|
|
queryset = Article.objects.prefetch_related('attachments').select_related('author', 'section')
|
|
return queryset
|
|
|
|
|
|
@router.post("/article", response=ArticleSchema)
|
|
def create_article(request, payload: ArticleCreateSchema):
|
|
# 注意:文件上传需额外处理(此处简化)
|
|
article = Article.objects.create(
|
|
title=payload.title,
|
|
section_id=payload.section_id,
|
|
author=request.auth, # 假设认证用户是作者
|
|
content=payload.content,
|
|
is_contribution=payload.is_contribution,
|
|
is_published=payload.is_published,
|
|
)
|
|
return article
|