优化restful接口视图

This commit is contained in:
yangjian 2021-04-16 20:55:26 +08:00
parent 761dc3d467
commit 6e0de01cb1
3 changed files with 54 additions and 50 deletions

View File

@ -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<path>.*)$',serve,{'document_root':settings.STATIC_ROOT}),# 静态文件
re_path('^media/(?P<path>.*)$',serve,{'document_root':settings.MEDIA_ROOT}),# 媒体文件
path('sitemap.xml', views.index, {'sitemaps': sitemaps,'template_name':'sitemap/sitemap-index.xml'},name='sitemap',), # 站点地图索引

View File

@ -41,6 +41,7 @@ import os
'''
# 生成Token的函数
def get_token_code(username):
"""
@ -625,11 +626,10 @@ 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))
@ -645,8 +645,6 @@ class DocTempView(APIView):
serializer = DocTempSerializer(page_doctemps,many=True)
resp = {'code':0,'data':serializer.data,'count':doctemps.count()}
return Response(resp)
else:
return Response({'code': 6, 'data': _('请登录')})
def post(self, request):
try:
@ -709,10 +707,10 @@ 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) # 查询所有图片
@ -725,8 +723,6 @@ class ImageView(APIView):
serializer = ImageSerializer(page_images,many=True)
resp = {'code':0,'data':serializer.data,'count':image_list.count()}
return Response(resp)
else:
return Response({'code': 6, 'data': '请登录'})
# 上传
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):

View File

@ -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 = {