81 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Python
		
	
	
	
| import json
 | |
| import random
 | |
| import warnings
 | |
| from calendar import timegm
 | |
| from datetime import datetime
 | |
| 
 | |
| import requests
 | |
| from django.db.models import Q
 | |
| from django_filters.rest_framework import DjangoFilterBackend
 | |
| from openpyxl import Workbook, load_workbook
 | |
| from rest_framework import status
 | |
| from rest_framework.decorators import action
 | |
| from rest_framework.filters import OrderingFilter, SearchFilter
 | |
| from rest_framework.permissions import IsAuthenticated
 | |
| from rest_framework.response import Response
 | |
| from rest_framework.views import APIView
 | |
| from rest_framework.viewsets import ModelViewSet
 | |
| from rest_framework_jwt.authentication import JSONWebTokenAuthentication
 | |
| from rest_framework_jwt.serializers import (jwt_encode_handler,
 | |
|                                             jwt_payload_handler)
 | |
| from rest_framework_jwt.settings import api_settings
 | |
| # Create your views here.
 | |
| 
 | |
| from .models import Article, Material
 | |
| from .serializers import ArticelSerializer, ArticelListSerializer, MaterialSerializer
 | |
| from utils.custom import CommonPagination
 | |
| class ArticleViewSet(ModelViewSet):
 | |
|     """
 | |
|     文章:增删改查
 | |
|     """
 | |
|     perms_map = [
 | |
|         {'get': '*'}, {'post': 'article_create'},
 | |
|         {'put': 'article_update'}, {'delete': 'article_delete'}]
 | |
|     queryset = Article.objects.filter(is_delete=0).all()
 | |
|     serializer_class = ArticelSerializer
 | |
|     pagination_class = CommonPagination
 | |
|     filter_backends = [DjangoFilterBackend,SearchFilter, OrderingFilter]
 | |
|     search_fields = ['title','content']
 | |
|     ordering_fields = ['title','update_time']
 | |
|     ordering = ['-is_top', '-update_time']
 | |
| 
 | |
|     def get_serializer_class(self):
 | |
|         if self.action=='list':
 | |
|             return ArticelListSerializer
 | |
|         else:
 | |
|             return ArticelSerializer
 | |
|     
 | |
|     @action(methods=['put'], detail=True, url_name='top_article', perms_map=[{'*':'top_article'}])
 | |
|     def top(self, request, *args, **kwargs):
 | |
|         '''
 | |
|         置顶文章
 | |
|         '''
 | |
|         instance = self.get_object()
 | |
|         instance.is_top = False if instance.is_top else True
 | |
|         instance.save()
 | |
|         return Response(status=status.HTTP_200_OK)
 | |
| 
 | |
| class MaterialViewSet(ModelViewSet):
 | |
|     """
 | |
|     资料:增删改查
 | |
|     """
 | |
|     perms_map = [
 | |
|         {'get': '*'}, {'post': 'material_create'},
 | |
|         {'put': 'material_update'}, {'delete': 'material_delete'}]
 | |
|     queryset = Material.objects.filter(is_delete=0)
 | |
|     serializer_class = MaterialSerializer
 | |
|     pagination_class = CommonPagination
 | |
|     filter_backends = [DjangoFilterBackend,SearchFilter, OrderingFilter]
 | |
|     search_fields = ['name','description']
 | |
|     ordering_fields = ['update_time', 'down_count']
 | |
|     ordering = ['-update_time']
 | |
| 
 | |
|     @action(methods=['get'], detail=True, url_name='down_material', perms_map=[{'*':'down_material'}])
 | |
|     def down(self, request, *args, **kwargs):
 | |
|         '''
 | |
|         下载资料
 | |
|         '''
 | |
|         instance = self.get_object()
 | |
|         instance.down_count = instance.down_count + 1
 | |
|         instance.save()
 | |
|         return Response({'path':instance.path, 'down_count':instance.down_count}) |