forked from mirror/MrDoc
新增用户Token API获取个人文档列表接口
This commit is contained in:
parent
721298c46d
commit
ea75fc87ca
@ -10,6 +10,7 @@ urlpatterns = [
|
||||
path('manage_token/',views.manage_token,name='manage_token'),# 用户Token管理
|
||||
path('get_projects/',views.get_projects,name="api_get_projects"), # 获取文集列表
|
||||
path('get_docs/',views.get_docs,name="api_get_docs"), # 获取文集的文档列表
|
||||
path('get_self_docs/',views.get_self_docs,name="get_self_docs"), # 获取自己的文档列表
|
||||
path('get_doc/',views.get_doc,name="api_get_doc"), # 获取单篇文档
|
||||
path('get_doc_previous_next/', views.get_doc_previous_next, name="api_get_doc_previous_next"), # 获取文档上下篇文档
|
||||
path('create_project/',views.create_project,name="api_create_project"), # 新建文集
|
||||
|
@ -1,4 +1,6 @@
|
||||
from app_doc.models import Project,ProjectCollaborator
|
||||
from django.utils.html import strip_tags
|
||||
import markdown
|
||||
|
||||
# 用户有浏览和、新增权限的文集列表
|
||||
def read_add_projects(user):
|
||||
@ -29,4 +31,18 @@ def read_add_edit_projects(user):
|
||||
set(self_list)
|
||||
.union(set(colla_list))
|
||||
)
|
||||
return view_list
|
||||
return view_list
|
||||
|
||||
# 摘取文档部分正文
|
||||
def remove_doc_tag(doc):
|
||||
try:
|
||||
if doc.editor_mode == 3: # 富文本文档
|
||||
result = strip_tags(doc.content)[:100]
|
||||
elif doc.editor_mode == 4:
|
||||
result = "此为表格文档,进入文档查看详细内容"
|
||||
else: # 其他文档
|
||||
result = strip_tags(markdown.markdown(doc.pre_content))[:100]
|
||||
except Exception as e:
|
||||
result = doc.pre_content[:100]
|
||||
result = result.replace(" ",'')
|
||||
return result
|
@ -7,13 +7,14 @@ from django.core.exceptions import PermissionDenied,ObjectDoesNotExist
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import authenticate,login,logout # 认证相关方法
|
||||
from django.contrib.auth.models import User # Django默认用户模型
|
||||
from django.core.paginator import Paginator,PageNotAnInteger,EmptyPage,InvalidPage # 后端分页
|
||||
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 app_api.utils import read_add_projects,remove_doc_tag
|
||||
from loguru import logger
|
||||
import time,hashlib
|
||||
import traceback,json
|
||||
@ -178,6 +179,64 @@ def get_docs(request):
|
||||
return JsonResponse({'status': False, 'data': _('系统异常')})
|
||||
|
||||
|
||||
# 获取个人所有文档列表
|
||||
def get_self_docs(request):
|
||||
token = request.GET.get('token', '')
|
||||
sort = request.GET.get('sort',0)
|
||||
kw = request.GET.get('kw','')
|
||||
if sort == '1':
|
||||
sort = '-'
|
||||
else:
|
||||
sort = ''
|
||||
try:
|
||||
token = UserToken.objects.get(token=token)
|
||||
# 按文档修改时间进行排序
|
||||
if kw == '':
|
||||
docs = Doc.objects.filter(create_user=token.user,status=1).order_by('{}modify_time'.format(sort))
|
||||
else:
|
||||
# kw_list = jieba.cut(kw, cut_all=True)
|
||||
# reduce(operator.or_,(Q(name__icontains=x) for x in kw_list))
|
||||
docs = Doc.objects.filter(create_user=token.user,status=1,name__icontains=kw).order_by('{}modify_time'.format(sort))
|
||||
|
||||
# 分页处理
|
||||
paginator = Paginator(docs, 10)
|
||||
page = request.GET.get('page', 1)
|
||||
try:
|
||||
docs_page = paginator.page(page)
|
||||
except PageNotAnInteger:
|
||||
docs_page = paginator.page(1)
|
||||
except EmptyPage:
|
||||
# docs_page = paginator.page(paginator.num_pages)
|
||||
return JsonResponse({'status': True, 'data': []})
|
||||
|
||||
doc_list = []
|
||||
for doc in docs_page:
|
||||
project = Project.objects.get(id=doc.top_doc)
|
||||
item = {
|
||||
'id': doc.id, # 文档ID
|
||||
'name': doc.name, # 文档名称
|
||||
'summary': remove_doc_tag(doc),
|
||||
'parent_doc':doc.parent_doc, # 上级文档
|
||||
'top_doc':doc.top_doc, # 所属文集
|
||||
'project_name':project.name,
|
||||
'project_role':project.role,
|
||||
'project_icon':project.icon,
|
||||
'editor_mode':doc.editor_mode,
|
||||
'status':doc.status, # 文档状态
|
||||
'create_time': doc.create_time, # 文档创建时间
|
||||
'modify_time': doc.modify_time, # 文档的修改时间
|
||||
'create_user': doc.create_user.username # 文档的创建者
|
||||
}
|
||||
doc_list.append(item)
|
||||
return JsonResponse({'status': True, 'data': doc_list})
|
||||
except ObjectDoesNotExist:
|
||||
return JsonResponse({'status': False, 'data': _('token无效')})
|
||||
except:
|
||||
logger.exception("token获取文档列表异常")
|
||||
return JsonResponse({'status': False, 'data': _('系统异常')})
|
||||
|
||||
|
||||
|
||||
# 获取单篇文档
|
||||
def get_doc(request):
|
||||
token = request.GET.get('token', '')
|
||||
|
Loading…
Reference in New Issue
Block a user