You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

# Create your views here.
from rest_framework.decorators import api_view, authentication_classes
from myapp import utils
from myapp.auth.authentication import AdminTokenAuthtication
from myapp.handler import APIResponse
from myapp.models import Tag
from myapp.permission.permission import isDemoAdminUser
from myapp.serializers import TagSerializer
@api_view(['GET'])
def list_api(request):
if request.method == 'GET':
tags = Tag.objects.all().order_by('-create_time')
serializer = TagSerializer(tags, many=True)
return APIResponse(code=0, msg='查询成功', data=serializer.data)
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def create(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
tags = Tag.objects.filter(title=request.data['title'])
if len(tags) > 0:
return APIResponse(code=1, msg='该名称已存在')
serializer = TagSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='创建成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='创建失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def update(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
pk = request.GET.get('id', -1)
tags = Tag.objects.get(pk=pk)
except Tag.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
serializer = TagSerializer(tags, data=request.data)
if serializer.is_valid():
serializer.save()
return APIResponse(code=0, msg='更新成功', data=serializer.data)
else:
utils.log_error(request, '参数错误')
return APIResponse(code=1, msg='更新失败')
@api_view(['POST'])
@authentication_classes([AdminTokenAuthtication])
def delete(request):
if isDemoAdminUser(request):
return APIResponse(code=1, msg='演示帐号无法操作')
try:
ids = request.GET.get('ids')
ids_arr = ids.split(',')
Tag.objects.filter(id__in=ids_arr).delete()
except Tag.DoesNotExist:
return APIResponse(code=1, msg='对象不存在')
return APIResponse(code=0, msg='删除成功')