ADD file via upload
This commit is contained in:
parent
17f2e511a9
commit
e58a68abfd
|
@ -0,0 +1,147 @@
|
|||
import json
|
||||
|
||||
from django.http import HttpResponse, JsonResponse, Http404
|
||||
from django.shortcuts import render
|
||||
from django.views import View
|
||||
from .models import ProjectsInfo
|
||||
from . import serializers
|
||||
from rest_framework.views import APIView
|
||||
|
||||
class Projects(View):
|
||||
|
||||
def parse(self, request):
|
||||
content_type = request.headers.get("Content-Type")
|
||||
if content_type == "application/json":
|
||||
try:
|
||||
return json.loads(request.body)
|
||||
except Exception as e:
|
||||
result = {"message": f"{e}", "code": "0"}
|
||||
return JsonResponse(result, status=400, safe=False,
|
||||
json_dumps_params={"ensure_ascii": False})
|
||||
elif content_type == "application/x-www-form-urlencoded":
|
||||
return request.POST
|
||||
else:
|
||||
pass
|
||||
|
||||
def get(self, request):
|
||||
"""1.需要能获取到项目的列数数据(获取所有数据)
|
||||
url: /projects/ method:GET response data: json"""
|
||||
# 从数据库读取所有的项目数据(QuerySet对象)
|
||||
qs = ProjectsInfo.objects.all()
|
||||
|
||||
# 序列化输出
|
||||
# 注意:查询集中涉及到多条数据要用many=true,会返回一个列表
|
||||
serializer = serializers.ProjectModelSerializer(instance=qs, many=True)
|
||||
return JsonResponse(serializer.data, safe=False, status=200, json_dumps_params={"ensure_ascii": False})
|
||||
|
||||
def post(self, request):
|
||||
"""3.能够创建项目(创建一个项目)
|
||||
url: /projects/ method:POST request data: json response data: json"""
|
||||
"""
|
||||
Content-Type=application/json
|
||||
请求体参数:
|
||||
{"name": "项目名称,必填,唯一",
|
||||
"leader": "负责人,必填",
|
||||
"is_execute": "是否执行, 非必填",
|
||||
"desc": "描述,非必填"}
|
||||
"""
|
||||
|
||||
try:
|
||||
serializer = serializers.ProjectModelSerializer(data=self.parse(request))
|
||||
serializer.is_valid(raise_exception=True)
|
||||
# 这里添加token与在序列化器类种添加token一致
|
||||
# serializer.validated_data["token"] = "validate token" # 使用这个不能调用serializer.save()
|
||||
serializer.save()
|
||||
except Exception as e:
|
||||
result = {"message": f"{e}", "code": "0"}
|
||||
return JsonResponse(result, status=400, safe=False,
|
||||
json_dumps_params={"ensure_ascii": False})
|
||||
res = serializer.validated_data
|
||||
return JsonResponse(res, safe=False, status=200,
|
||||
json_dumps_params={"ensure_ascii": False})
|
||||
|
||||
|
||||
class ProjectsPK(View):
|
||||
primary_key_name = "pk"
|
||||
|
||||
def get_object(self):
|
||||
try:
|
||||
# return ProjectsInfo.objects.get(id=pk)
|
||||
# return ProjectsInfo.objects.get(**self.kwargs)
|
||||
pk = self.kwargs.get(self.primary_key_name)
|
||||
return ProjectsInfo.objects.get(pk=pk)
|
||||
except Exception:
|
||||
raise Http404
|
||||
|
||||
def get(self, request, pk):
|
||||
""" 2.需要能获取到项目的详情数据(获取前端指定某一条数据)
|
||||
url: /projects/<int:pk>/ method:GET response data: json"""
|
||||
obj = self.get_object()
|
||||
serializer = serializers.ProjectModelSerializer(instance=obj)
|
||||
return JsonResponse(serializer.data, safe=False, status=200, json_dumps_params={"ensure_ascii": False})
|
||||
|
||||
def put(self, request, pk):
|
||||
""" 4.能够更新项目(只更新某一个项目)
|
||||
url: /projects/<int:pk>/ method:PUT request data: json response data: json"""
|
||||
# 从前端获取待更新的项目数据
|
||||
try:
|
||||
python_dict = json.loads(request.body)
|
||||
obj = self.get_object()
|
||||
serializer = serializers.ProjectModelSerializer(instance=obj,
|
||||
data=python_dict)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
except Exception as e:
|
||||
result = {"message": f"{e}", "code": "0"}
|
||||
return JsonResponse(result, status=400, safe=False,
|
||||
json_dumps_params={"ensure_ascii": False})
|
||||
return JsonResponse(serializer.data, status=200, safe=False,
|
||||
json_dumps_params={"ensure_ascii": False})
|
||||
|
||||
def delete(self, request, pk):
|
||||
""" 5.能够删除项目(只删除某一个项目)
|
||||
url: /projects/<int:pk>/ method:DELETE"""
|
||||
obj = self.get_object()
|
||||
# 项目模型对象调用delete方法
|
||||
obj.delete()
|
||||
# 返回空给前端
|
||||
return JsonResponse(None, status=204, safe=False, json_dumps_params={"ensure_ascii": False})
|
||||
|
||||
|
||||
"""
|
||||
反序列化输入过程
|
||||
将前端传递的参数(json参数,www-form参数)转换为python常用数据类型(字典,列表嵌套字典)
|
||||
|
||||
数据库校验过程
|
||||
|
||||
|
||||
数据库操作过程(创建,更新操作,读取操作,删除)
|
||||
|
||||
序列化输出过程
|
||||
将python常见的数据类型或者模型类转换为前端能正常解析的json/xml字符串
|
||||
|
||||
|
||||
5个接口分别涉及的过程
|
||||
获取所有的项目数据:数据库操作过程,序列化输出过程
|
||||
创建项目:反序列化输入过程,数据校验过程,数据库操作过程,序列化输出过程
|
||||
获取单个项目数据:数据库操作过程,序列化输出过程
|
||||
更新项目:反序列化输入过程,数据校验过程,数据库操作过程,序列化输出过程
|
||||
删除项目:数据库操作过程
|
||||
|
||||
当前设计的5个接口,有哪些痛点
|
||||
代码冗余非常严重,不够简洁
|
||||
数据校验极为麻烦,可读性差,复用性更差
|
||||
代码没有统一的规范,维护性差
|
||||
序列化输出的过程没有差异性
|
||||
在获取所有项目的接口中,有很多功能缺失:
|
||||
没有提供分页功能
|
||||
没有提供过滤功能
|
||||
没有提供排序功能
|
||||
|
||||
整个项目,也有功能缺失
|
||||
没有提供认证授权功能
|
||||
没有提供限流功能(反爬虫)
|
||||
5个接口不能放在同一个类视图中定义
|
||||
传递参数的方式单一,不支持www-form表单参数
|
||||
|
||||
"""
|
Loading…
Reference in New Issue