feat. 添加文章列表分页接口
This commit is contained in:
+25
-11
@@ -1,10 +1,10 @@
|
||||
from django.http import HttpResponseForbidden
|
||||
from django.shortcuts import render
|
||||
|
||||
from article.models import Article
|
||||
from utils.permission import user_has_permission
|
||||
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.
|
||||
|
||||
@@ -12,9 +12,23 @@ from ninja import Router
|
||||
router = Router(tags=['content'])
|
||||
|
||||
|
||||
@router.get('/article', description="获取文章列表")
|
||||
def article_details(request):
|
||||
if not user_has_permission(request.user, 'article', 'view'):
|
||||
return HttpResponseForbidden("无权访问")
|
||||
articles = Article.objects.all()
|
||||
return render(request, 'articles/list.html', {'articles': articles})
|
||||
@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
|
||||
|
||||
Reference in New Issue
Block a user