1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
from django.shortcuts import render
from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet from app01.models import Book
from rest_framework.decorators import action from rest_framework.response import Response from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import AllowAny,IsAuthenticated,IsAdminUser,IsAuthenticatedOrReadOnly
from app01.response import APIResponse from app01 import models from app01 import ser as serializers classPublishAPIView(APIView): defget(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: publish_obj = models.Publish.objects.filter(pk=pk).first() ifnot publish_obj: return APIResponse(1, 'pk error', http_status=400) publish_data = serializers.PublishModelSerializer(publish_obj).data return APIResponse(results=publish_data)
publish_query = models.Publish.objects.all() return APIResponse(0, 'ok', data=serializers.PublishModelSerializer(publish_query, many=True).data)
classBookAPIView(APIView):
defget(self, request, *args, **kwargs): pk = kwargs.get('pk') if pk: book_obj = models.Book.objects.filter(pk=pk, is_delete=False).first() ifnot book_obj: return APIResponse(1, 'pk error', http_status=400) book_data = serializers.BookModelSerializer(book_obj).data print(book_data) return APIResponse(data=book_data)
book_query = models.Book.objects.filter(is_delete=False).all()
return APIResponse(0, 'ok', data=serializers.BookModelSerializer(book_query, many=True).data)
defdelete(self, request, *args, **kwargs): """ 单删:前台数据为pk,接口为 /books/(pk)/ 群删:前台数据为pks,接口为 /books/ """ pk = kwargs.get('pk')
if pk: pks = [pk] else: pks = request.data.get('pks')
ifnot pks: return APIResponse(1, 'delete error', http_status=400)
rows = models.Book.objects.filter(is_delete=False, pk__in=pks).update(is_delete=True) if rows: return APIResponse(0, 'delete ok') return APIResponse(1, 'delete failed')
defpost(self, request, *args, **kwargs): """ 单增:前台提交字典,接口 /books/ 群增:前台提交列表套字典,接口 /books/ """ request_data = request.data if isinstance(request_data, dict): book_ser = serializers.BookModelSerializer(data=request_data) if book_ser.is_valid(): book_obj = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj).data) return APIResponse(1, msg=book_ser.errors) elif isinstance(request_data, list) and len(request_data) != 0 : book_ser = serializers.BookModelSerializer(data=request_data, many=True) book_ser.is_valid(raise_exception=True) book_obj_list = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj_list, many=True).data) else: return APIResponse(1, 'data error', http_status=400)
defput(self, request, *args, **kwargs): """ 单整体改:前台提交字典,接口 /books/(pk)/ 群整体改:前台提交列表套字典,接口 /books/,注每一个字典都可以通过pk """ pk = kwargs.get('pk') request_data = request.data if pk: try: book_obj = models.Book.objects.get(pk=pk) except: return APIResponse(1, 'pk error')
book_ser = serializers.BookModelSerializer(instance=book_obj, data=request_data) book_ser.is_valid(raise_exception=True) book_obj = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj).data) else: ifnot isinstance(request_data, list) or len(request_data) == 0: return APIResponse(1, 'data error', http_status=400)
obj_list = [] data_list = [] for dic in request_data:
try: pk = dic.pop('pk') try: obj = models.Book.objects.get(pk=pk, is_delete=False) obj_list.append(obj) data_list.append(dic) except: pass except: return APIResponse(1, 'data error', http_status=400)
book_ser = serializers.BookModelSerializer(instance=obj_list, data=data_list, many=True) book_ser.is_valid(raise_exception=True) book_obj_list = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj_list, many=True).data)
defpatch(self, request, *args, **kwargs): """ 单整体改:前台提交字典,接口 /books/(pk)/ 群整体改:前台提交列表套字典,接口 /books/,注每一个字典都可以通过pk """ pk = kwargs.get('pk') request_data = request.data if pk: try: book_obj = models.Book.objects.get(pk=pk) except: return APIResponse(1, 'pk error')
book_ser = serializers.BookModelSerializer(instance=book_obj, data=request_data, partial=True) book_ser.is_valid(raise_exception=True) book_obj = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj).data)
else: ifnot isinstance(request_data, list) or len(request_data) == 0: return APIResponse(1, 'data error', http_status=400)
obj_list = [] data_list = [] for dic in request_data:
try: pk = dic.pop('pk') try: obj = models.Book.objects.get(pk=pk, is_delete=False) obj_list.append(obj) data_list.append(dic) except: pass except: return APIResponse(1, 'data error', http_status=400)
book_ser = serializers.BookModelSerializer(instance=obj_list, data=data_list, many=True, partial=True) book_ser.is_valid(raise_exception=True) book_obj_list = book_ser.save() return APIResponse(data=serializers.BookModelSerializer(book_obj_list, many=True).data)
classAuthorAPIView(APIView): defget(self,request,*args,**kwargs): authors=models.Author.objects.all() author_ser=serializers.AuthorModelSerializer(authors,many=True) return APIResponse(data=author_ser.data) defput(self,reuqest,*args,**kwargs): pass defpost(self,request,*args,**kwargs): author_ser=serializers.AuthorModelSerializer(data=request.data) author_ser.is_valid(raise_exception=True) author_ser.save() return APIResponse() defdelete(self,request,*args,**kwargs): pass
|