forked from mirror/MrDoc
新增用户Token获取文档上下篇文档接口
This commit is contained in:
parent
b58b4bf95b
commit
3670088eb3
32
app_api/utils.py
Normal file
32
app_api/utils.py
Normal file
@ -0,0 +1,32 @@
|
||||
from app_doc.models import Project,ProjectCollaborator
|
||||
|
||||
# 用户有浏览和、新增权限的文集列表
|
||||
def read_add_projects(user):
|
||||
# 用户的协作文集ID列表
|
||||
colla_list = [i.project.id for i in ProjectCollaborator.objects.filter(user=user)]
|
||||
|
||||
# 用户自己的文集ID列表
|
||||
self_list = [i.id for i in Project.objects.filter(create_user=user)]
|
||||
|
||||
# 合并上述文集ID列表
|
||||
view_list = list(
|
||||
set(self_list)
|
||||
.union(set(colla_list))
|
||||
)
|
||||
return view_list
|
||||
|
||||
|
||||
# 用户有浏览、新增、和修改所有文档权限的文集列表
|
||||
def read_add_edit_projects(user):
|
||||
# 用户的协作文集ID列表
|
||||
colla_list = [i.project.id for i in ProjectCollaborator.objects.filter(user=user,role=1)]
|
||||
|
||||
# 用户自己的文集ID列表
|
||||
self_list = [i.id for i in Project.objects.filter(create_user=user)]
|
||||
|
||||
# 合并上述文集ID列表
|
||||
view_list = list(
|
||||
set(self_list)
|
||||
.union(set(colla_list))
|
||||
)
|
||||
return view_list
|
@ -10,8 +10,10 @@ from django.contrib.auth.models import User # Django默认用户模型
|
||||
from django.shortcuts import render,redirect
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from app_doc.util_upload_img import upload_generation_dir,base_img_upload,url_img_upload
|
||||
from app_doc.utils import find_doc_next,find_doc_previous
|
||||
from app_api.models import UserToken
|
||||
from app_doc.models import Project,Doc,DocHistory,Image
|
||||
from app_api.utils import read_add_projects
|
||||
from loguru import logger
|
||||
import time,hashlib
|
||||
import traceback,json
|
||||
@ -205,6 +207,37 @@ def get_doc(request):
|
||||
return JsonResponse({'status': False, 'data': _('系统异常')})
|
||||
|
||||
|
||||
# 获取文档上下篇文档
|
||||
def get_doc_previous_next(request):
|
||||
token = request.GET.get('token', '')
|
||||
try:
|
||||
token = UserToken.objects.get(token=token)
|
||||
did = request.GET.get('did', '')
|
||||
doc = Doc.objects.get(id=did) # 查询文档
|
||||
project = Project.objects.get(id=doc.top_doc) # 查询文档所属的文集
|
||||
# 用户有浏览和新增权限的文集列表
|
||||
view_list = read_add_projects(token.user)
|
||||
|
||||
if project.id not in view_list:
|
||||
return JsonResponse({'status': False, 'data': _('无权限')})
|
||||
|
||||
try:
|
||||
previous_doc = find_doc_previous(did)
|
||||
previous_doc_id = previous_doc.id
|
||||
except Exception as e:
|
||||
previous_doc_id = None
|
||||
try:
|
||||
next_doc = find_doc_next(did)
|
||||
next_doc_id = next_doc.id
|
||||
except Exception as e:
|
||||
next_doc_id = None
|
||||
return JsonResponse({'status': True, 'data': {'next':next_doc_id,'previous':previous_doc_id}})
|
||||
except Exception as e:
|
||||
logger.exception("获取文档上下篇文档异常")
|
||||
return JsonResponse({'status':False,'data':'系统异常'})
|
||||
|
||||
|
||||
|
||||
# 新建文集
|
||||
@require_http_methods(['GET','POST'])
|
||||
@csrf_exempt
|
||||
|
116
app_doc/utils.py
Normal file
116
app_doc/utils.py
Normal file
@ -0,0 +1,116 @@
|
||||
from app_doc.models import Doc,Project
|
||||
|
||||
# 查找文档的下级文档
|
||||
def find_doc_next(doc_id):
|
||||
doc = Doc.objects.get(id=int(doc_id)) # 当前文档
|
||||
# 获取文集的文档默认排序方式
|
||||
sort = Project.objects.get(id=doc.top_doc).sort_value
|
||||
|
||||
# 获取文档的下级文档
|
||||
subdoc = Doc.objects.filter(parent_doc=doc.id,top_doc=doc.top_doc, status=1)
|
||||
|
||||
# 如果存在子级文档,那么下一篇文档为第一篇子级文档
|
||||
if subdoc.count() != 0:
|
||||
if sort == 1:
|
||||
next_doc = subdoc.order_by('-create_time')[0]
|
||||
else:
|
||||
next_doc = subdoc.order_by('sort')[0]
|
||||
|
||||
# 如果不存在子级文档
|
||||
else:
|
||||
# 获取兄弟文档
|
||||
if sort == 1:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc,top_doc=doc.top_doc, status=1).order_by('-create_time')
|
||||
else:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc,top_doc=doc.top_doc, status=1).order_by('sort','create_time')
|
||||
sibling_list = [d.id for d in sibling_docs]
|
||||
# 如果当前文档不是兄弟文档中的最后一个,那么下一篇文档是当前文档的下一个兄弟文档
|
||||
if sibling_list.index(doc.id) != len(sibling_list) - 1:
|
||||
next_id = sibling_list[sibling_list.index(doc.id) + 1]
|
||||
next_doc = Doc.objects.get(id=next_id)
|
||||
# 如果当前文档是兄弟文档中的最后一个,那么从上级文档中查找
|
||||
else:
|
||||
# 如果文档的上级文档为0,说明文档没有上级文档
|
||||
if doc.parent_doc == 0:
|
||||
next_doc = None
|
||||
else:
|
||||
next_doc = find_doc_parent_sibling(doc.parent_doc,sort=sort)
|
||||
|
||||
return next_doc
|
||||
|
||||
|
||||
# 查找文档的上级文档的同级文档(用于遍历获取文档的下一篇文档)
|
||||
def find_doc_parent_sibling(doc_id,sort):
|
||||
doc = Doc.objects.get(id=int(doc_id)) # 当前文档
|
||||
|
||||
# 获取兄弟文档
|
||||
if sort == 1:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc, top_doc=doc.top_doc, status=1).order_by(
|
||||
'-create_time')
|
||||
else:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc, top_doc=doc.top_doc, status=1).order_by('sort',
|
||||
'create_time')
|
||||
sibling_list = [d.id for d in sibling_docs]
|
||||
# 如果当前文档不是兄弟文档中的最后一个,那么下一篇文档是当前文档的下一个兄弟文档
|
||||
if sibling_list.index(doc.id) != len(sibling_list) - 1:
|
||||
next_id = sibling_list[sibling_list.index(doc.id) + 1]
|
||||
next_doc = Doc.objects.get(id=next_id)
|
||||
# 如果当前文档是兄弟文档中的最后一个,那么从上级文档中查找
|
||||
else:
|
||||
# 如果文档的上级文档为0,说明文档没有上级文档
|
||||
if doc.parent_doc == 0:
|
||||
next_doc = None
|
||||
else:
|
||||
next_doc = find_doc_parent_sibling(doc.parent_doc,sort)
|
||||
return next_doc
|
||||
|
||||
|
||||
# 查找文档的上一篇文档
|
||||
def find_doc_previous(doc_id):
|
||||
doc = Doc.objects.get(id=int(doc_id)) # 当前文档
|
||||
# 获取文集的文档默认排序方式
|
||||
sort = Project.objects.get(id=doc.top_doc).sort_value
|
||||
# 获取文档的兄弟文档
|
||||
# 获取兄弟文档
|
||||
if sort == 1:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc, top_doc=doc.top_doc, status=1).order_by(
|
||||
'-create_time')
|
||||
else:
|
||||
sibling_docs = Doc.objects.filter(parent_doc=doc.parent_doc, top_doc=doc.top_doc, status=1).order_by('sort',
|
||||
'create_time')
|
||||
sibling_list = [d.id for d in sibling_docs]
|
||||
|
||||
# 如果文档为兄弟文档的第一个,那么其上级文档即为上一篇文档
|
||||
if sibling_list.index(doc.id) == 0:
|
||||
# 如果其为顶级文档,那么没有上一篇文档
|
||||
if doc.parent_doc == 0:
|
||||
previous_doc = None
|
||||
# 如果其为次级文档,那么其上一篇文档为上级文档
|
||||
else:
|
||||
previous_doc = Doc.objects.get(id=doc.parent_doc)
|
||||
# 如果文档不为兄弟文档的第一个,从兄弟文档中查找
|
||||
else:
|
||||
previous_id = sibling_list[sibling_list.index(doc.id) - 1]
|
||||
previous_doc = find_doc_sibling_sub(previous_id,sort)
|
||||
|
||||
return previous_doc
|
||||
|
||||
|
||||
# 查找文档的最下级文档(用于遍历获取文档的上一篇文档)
|
||||
def find_doc_sibling_sub(doc_id,sort):
|
||||
doc = Doc.objects.get(id=int(doc_id)) # 当前文档
|
||||
# 查询文档的下级文档
|
||||
if sort == 1:
|
||||
subdoc = Doc.objects.filter(parent_doc=doc.id, top_doc=doc.top_doc, status=1).order_by(
|
||||
'-create_time')
|
||||
else:
|
||||
subdoc = Doc.objects.filter(parent_doc=doc.id, top_doc=doc.top_doc, status=1).order_by('sort','create_time')
|
||||
subdoc_list = [d.id for d in subdoc]
|
||||
# 如果文档没有下级文档,那么返回自己
|
||||
if subdoc.count() == 0:
|
||||
previous_doc = doc
|
||||
# 如果文档存在下级文档,查找最靠后的下级文档
|
||||
else:
|
||||
previous_doc = find_doc_sibling_sub(subdoc_list[len(subdoc) - 1],sort)
|
||||
|
||||
return previous_doc
|
Loading…
x
Reference in New Issue
Block a user