From ea75fc87ca61a874c7fe7f1341d2ca128f282084 Mon Sep 17 00:00:00 2001 From: yangjian Date: Mon, 6 Mar 2023 07:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7Token=20API?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=AA=E4=BA=BA=E6=96=87=E6=A1=A3=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app_api/urls.py | 1 + app_api/utils.py | 18 +++++++++++++- app_api/views.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/app_api/urls.py b/app_api/urls.py index 9e0b8ca..7f404c9 100644 --- a/app_api/urls.py +++ b/app_api/urls.py @@ -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"), # 新建文集 diff --git a/app_api/utils.py b/app_api/utils.py index 6304546..a49ca85 100644 --- a/app_api/utils.py +++ b/app_api/utils.py @@ -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 \ No newline at end of file + 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 \ No newline at end of file diff --git a/app_api/views.py b/app_api/views.py index 6ddcaaa..f7584aa 100644 --- a/app_api/views.py +++ b/app_api/views.py @@ -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', '')