From 6e0de01cb16d9d4d010275d08d799c02e68eef2b Mon Sep 17 00:00:00 2001 From: yangjian Date: Fri, 16 Apr 2021 20:55:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96restful=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MrDoc/urls.py | 4 +-- app_api/views_app.py | 84 +++++++++++++++++++++----------------------- app_doc/views.py | 16 ++++++--- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/MrDoc/urls.py b/MrDoc/urls.py index ecd84fa..a25af3c 100644 --- a/MrDoc/urls.py +++ b/MrDoc/urls.py @@ -25,8 +25,8 @@ sitemaps = SitemapAll() urlpatterns = [ path('',include('app_doc.urls')), # doc应用 path('user/',include('app_admin.urls'),), # admin应用 - path('api/',include('app_api.urls')), # API应用 - path('api_app/',include('app_api.urls_app')), # App的API接口 + path('api/',include('app_api.urls')), # 用户 Token API 接口 + path('api_app/',include('app_api.urls_app')), # RESTFUL API 接口 re_path('^static/(?P.*)$',serve,{'document_root':settings.STATIC_ROOT}),# 静态文件 re_path('^media/(?P.*)$',serve,{'document_root':settings.MEDIA_ROOT}),# 媒体文件 path('sitemap.xml', views.index, {'sitemaps': sitemaps,'template_name':'sitemap/sitemap-index.xml'},name='sitemap',), # 站点地图索引 diff --git a/app_api/views_app.py b/app_api/views_app.py index 8dbabcc..fd667a8 100644 --- a/app_api/views_app.py +++ b/app_api/views_app.py @@ -27,20 +27,21 @@ import os ''' 响应: - code:状态码 - data:数据 - + code:状态码 + data:数据 + 状态码: - 0:成功 - 1:资源未找到 - 2:无权访问 - 3:需要访问码 - 4:系统异常 - 5:参数不正确 - 6:需要登录 + 0:成功 + 1:资源未找到 + 2:无权访问 + 3:需要访问码 + 4:系统异常 + 5:参数不正确 + 6:需要登录 ''' + # 生成Token的函数 def get_token_code(username): """ @@ -625,28 +626,25 @@ class DocView(APIView): # 文档模板视图 class DocTempView(APIView): - authentication_classes = (AppAuth,SessionAuthentication) + authentication_classes = (AppMustAuth,SessionAuthentication) # 获取文档模板 def get(self, request): - if request.auth: - temp_id = request.query_params.get('id','') - if temp_id != '': - doctemp = DocTemp.objects.get(id=int(temp_id)) - if request.user == doctemp.create_user: - serializer = DocTempSerializer(doctemp) - resp = {'code': 0, 'data': serializer.data} - else: - resp = {'code':2,'data':_('无权操作')} + temp_id = request.query_params.get('id','') + if temp_id != '': + doctemp = DocTemp.objects.get(id=int(temp_id)) + if request.user == doctemp.create_user: + serializer = DocTempSerializer(doctemp) + resp = {'code': 0, 'data': serializer.data} else: - doctemps = DocTemp.objects.filter(create_user=request.user) - page = PageNumberPagination() - page_doctemps = page.paginate_queryset(doctemps,request,view=self) - serializer = DocTempSerializer(page_doctemps,many=True) - resp = {'code':0,'data':serializer.data,'count':doctemps.count()} - return Response(resp) + resp = {'code':2,'data':_('无权操作')} else: - return Response({'code': 6, 'data': _('请登录')}) + doctemps = DocTemp.objects.filter(create_user=request.user) + page = PageNumberPagination() + page_doctemps = page.paginate_queryset(doctemps,request,view=self) + serializer = DocTempSerializer(page_doctemps,many=True) + resp = {'code':0,'data':serializer.data,'count':doctemps.count()} + return Response(resp) def post(self, request): try: @@ -709,24 +707,22 @@ class DocTempView(APIView): # 图片视图 class ImageView(APIView): - authentication_classes = (AppAuth,SessionAuthentication) + authentication_classes = (AppMustAuth,SessionAuthentication) + # 获取 def get(self, request): - if request.auth: - g_id = int(request.query_params.get('group', 0)) # 图片分组id - if int(g_id) == 0: - image_list = Image.objects.filter(user=request.user) # 查询所有图片 - elif int(g_id) == -1: - image_list = Image.objects.filter(user=request.user, group_id=None) # 查询指定分组的图片 - else: - image_list = Image.objects.filter(user=request.user, group_id=g_id) # 查询指定分组的图片 - page = PageNumberPagination() - page_images = page.paginate_queryset(image_list,request,view=self) - serializer = ImageSerializer(page_images,many=True) - resp = {'code':0,'data':serializer.data,'count':image_list.count()} - return Response(resp) + g_id = int(request.query_params.get('group', 0)) # 图片分组id + if int(g_id) == 0: + image_list = Image.objects.filter(user=request.user) # 查询所有图片 + elif int(g_id) == -1: + image_list = Image.objects.filter(user=request.user, group_id=None) # 查询指定分组的图片 else: - return Response({'code': 6, 'data': '请登录'}) + image_list = Image.objects.filter(user=request.user, group_id=g_id) # 查询指定分组的图片 + page = PageNumberPagination() + page_images = page.paginate_queryset(image_list,request,view=self) + serializer = ImageSerializer(page_images,many=True) + resp = {'code':0,'data':serializer.data,'count':image_list.count()} + return Response(resp) # 上传 def post(self, request): @@ -763,7 +759,7 @@ class ImageView(APIView): # 图片分组视图 class ImageGroupView(APIView): - authentication_classes = (AppAuth,SessionAuthentication) + authentication_classes = (AppMustAuth,SessionAuthentication) def get(self, request): try: @@ -818,7 +814,7 @@ class ImageGroupView(APIView): # 附件视图 class AttachmentView(APIView): - authentication_classes = (AppAuth,SessionAuthentication) + authentication_classes = (AppMustAuth,SessionAuthentication) # 文件大小 字节转换 def sizeFormat(size, is_disk=False, precision=2): diff --git a/app_doc/views.py b/app_doc/views.py index b7173cd..49b63e3 100644 --- a/app_doc/views.py +++ b/app_doc/views.py @@ -54,8 +54,10 @@ def get_pro_toc(pro_id): # except: # print("重新生成") # 查询存在上级文档的文档 - parent_id_list = Doc.objects.filter(top_doc=pro_id, status=1).exclude(parent_doc=0).values_list('parent_doc', - flat=True) + parent_id_list = Doc.objects.filter( + top_doc=pro_id, + status=1 + ).exclude(parent_doc=0).values_list('parent_doc',flat=True) # 获取存在上级文档的上级文档ID # print(parent_id_list) doc_list = [] @@ -75,7 +77,10 @@ def get_pro_toc(pro_id): if doc['id'] in parent_id_list: # 获取二级文档 sec_docs = Doc.objects.filter( - top_doc=pro_id, parent_doc=doc['id'], status=1).values('id', 'name','open_children').order_by('sort') + top_doc=pro_id, + parent_doc=doc['id'], + status=1 + ).values('id', 'name','open_children').order_by('sort') top_item['children'] = [] for doc in sec_docs: sec_item = { @@ -88,7 +93,10 @@ def get_pro_toc(pro_id): if doc['id'] in parent_id_list: # 获取三级文档 thr_docs = Doc.objects.filter( - top_doc=pro_id, parent_doc=doc['id'], status=1).values('id','name').order_by('sort') + top_doc=pro_id, + parent_doc=doc['id'], + status=1 + ).values('id','name').order_by('sort') sec_item['children'] = [] for doc in thr_docs: item = {