117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
from django.shortcuts import render
|
|
from loguru import logger
|
|
|
|
# Create your views here.
|
|
from ninja import Router
|
|
from django.shortcuts import get_object_or_404
|
|
from typing import List
|
|
|
|
from ninja.errors import HttpError
|
|
from ninja.pagination import paginate, PageNumberPagination
|
|
|
|
from users.permission import has_permission, require_permissions
|
|
from utils.dept_utils import get_dept_children_ids
|
|
from .models import NewsSection, News
|
|
from .schema import (
|
|
NewsSectionSchema, NewsSectionCreateSchema, NewsSectionUpdateSchema,
|
|
NewsSchema, NewsCreateSchema, NewsUpdateSchema
|
|
)
|
|
|
|
router = Router(tags=['news'])
|
|
|
|
|
|
# ================= 新闻模块接口 =================
|
|
|
|
@router.get("/news_section/", response=List[NewsSectionSchema])
|
|
@paginate(PageNumberPagination, page_size=20)
|
|
def list_sections(request):
|
|
return NewsSection.objects.all()
|
|
|
|
|
|
@router.get("/news_section/{section_id}", response=NewsSectionSchema)
|
|
def retrieve_section(request, section_id: int):
|
|
return get_object_or_404(NewsSection, id=section_id)
|
|
|
|
|
|
@router.post("/news_section/", response=NewsSectionSchema)
|
|
def create_section(request, data: NewsSectionCreateSchema):
|
|
section = NewsSection.objects.create(**data.dict())
|
|
return section
|
|
|
|
|
|
@router.put("/news_section/{section_id}", response=NewsSectionSchema)
|
|
def update_section(request, section_id: int, data: NewsSectionUpdateSchema):
|
|
section = get_object_or_404(NewsSection, id=section_id)
|
|
for attr, value in data.dict(exclude_unset=True).items():
|
|
setattr(section, attr, value)
|
|
section.save()
|
|
return section
|
|
|
|
|
|
@router.delete("/news_section/{section_id}", response={"success": bool})
|
|
def delete_section(request, section_id: int):
|
|
section = get_object_or_404(NewsSection, id=section_id)
|
|
section.delete()
|
|
return {"success": True}
|
|
|
|
|
|
# ================= 新闻接口 =================
|
|
|
|
@router.get("/news/", response=List[NewsSchema])
|
|
@paginate(PageNumberPagination, page_size=20)
|
|
@require_permissions("news:list")
|
|
def list_news(request, section_id: int = 0, title: str = ''):
|
|
user = request.auth
|
|
if not has_permission(user.id, "news:list"):
|
|
raise
|
|
news = News.objects.all()
|
|
|
|
# 板块筛选
|
|
if section_id > 0:
|
|
return news.filter(section_id=section_id).all()
|
|
|
|
# 根据用户权限筛选
|
|
if has_permission(user.id, 'news:manage'):
|
|
dept_ids = get_dept_children_ids(user.dept_id)
|
|
# 筛选:作者属于这些部门中的任意一个
|
|
news = news.filter(author__dept_id__in=dept_ids)
|
|
else:
|
|
logger.info(user)
|
|
news = news.filter(author=user)
|
|
|
|
# 根据新闻标题筛选
|
|
if title:
|
|
news = news.filter(title__contains=title)
|
|
return news
|
|
|
|
|
|
@router.get("/news/{news_id}", response=NewsSchema)
|
|
@require_permissions("news:view")
|
|
def retrieve_news(request, news_id: int):
|
|
return get_object_or_404(News, id=news_id)
|
|
|
|
|
|
@router.post("/news/", response=NewsSchema)
|
|
@require_permissions("news:create")
|
|
def create_news(request, data: NewsCreateSchema):
|
|
news = News.objects.create(**data.dict())
|
|
return news
|
|
|
|
|
|
@router.put("/news/{news_id}", response=NewsSchema)
|
|
@require_permissions("news:update")
|
|
def update_news(request, news_id: int, data: NewsUpdateSchema):
|
|
news = get_object_or_404(News, id=news_id)
|
|
for attr, value in data.dict(exclude_unset=True).items():
|
|
setattr(news, attr, value)
|
|
news.save()
|
|
return news
|
|
|
|
|
|
@router.delete("/news/{news_id}", response={"success": bool})
|
|
@require_permissions("news:delete")
|
|
def delete_news(request, news_id: int):
|
|
news = get_object_or_404(News, id=news_id)
|
|
news.delete()
|
|
return {"success": True}
|