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.
105 lines
3.3 KiB
105 lines
3.3 KiB
3 weeks ago
|
# 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 Classification, Thing, Tag
|
||
|
from myapp.permission.permission import isDemoAdminUser
|
||
|
from myapp.serializers import ThingSerializer, UpdateThingSerializer
|
||
|
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def list_api(request):
|
||
|
if request.method == 'GET':
|
||
|
keyword = request.GET.get("keyword", None)
|
||
|
c = request.GET.get("c", None)
|
||
|
tag = request.GET.get("tag", None)
|
||
|
if keyword:
|
||
|
things = Thing.objects.filter(title__contains=keyword).order_by('create_time')
|
||
|
elif c:
|
||
|
classification = Classification.objects.get(pk=c)
|
||
|
things = classification.classification_thing.all()
|
||
|
elif tag:
|
||
|
tag = Tag.objects.get(id=tag)
|
||
|
print(tag)
|
||
|
things = tag.thing_set.all()
|
||
|
else:
|
||
|
things = Thing.objects.all().order_by('create_time')
|
||
|
|
||
|
serializer = ThingSerializer(things, many=True)
|
||
|
return APIResponse(code=0, msg='查询成功', data=serializer.data)
|
||
|
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def detail(request):
|
||
|
|
||
|
try:
|
||
|
pk = request.GET.get('id', -1)
|
||
|
thing = Thing.objects.get(pk=pk)
|
||
|
except Thing.DoesNotExist:
|
||
|
utils.log_error(request, '对象不存在')
|
||
|
return APIResponse(code=1, msg='对象不存在')
|
||
|
|
||
|
if request.method == 'GET':
|
||
|
serializer = ThingSerializer(thing)
|
||
|
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='演示帐号无法操作')
|
||
|
|
||
|
serializer = ThingSerializer(data=request.data)
|
||
|
if serializer.is_valid():
|
||
|
serializer.save()
|
||
|
return APIResponse(code=0, msg='创建成功', data=serializer.data)
|
||
|
else:
|
||
|
print(serializer.errors)
|
||
|
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)
|
||
|
thing = Thing.objects.get(pk=pk)
|
||
|
except Thing.DoesNotExist:
|
||
|
return APIResponse(code=1, msg='对象不存在')
|
||
|
|
||
|
serializer = UpdateThingSerializer(thing, data=request.data)
|
||
|
if serializer.is_valid():
|
||
|
serializer.save()
|
||
|
return APIResponse(code=0, msg='查询成功', data=serializer.data)
|
||
|
else:
|
||
|
print(serializer.errors)
|
||
|
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(',')
|
||
|
Thing.objects.filter(id__in=ids_arr).delete()
|
||
|
except Thing.DoesNotExist:
|
||
|
return APIResponse(code=1, msg='对象不存在')
|
||
|
return APIResponse(code=0, msg='删除成功')
|