|
|
@@ -1,7 +1,7 @@
|
|
|
import asyncio
|
|
|
from datetime import datetime
|
|
|
import re
|
|
|
-from typing import Optional
|
|
|
+from typing import Optional, Tuple
|
|
|
from enum import Enum
|
|
|
from typing import List, Any
|
|
|
import os
|
|
|
@@ -108,7 +108,7 @@ class DocumentsRepository(DocumentBase):
|
|
|
def __init__(self, engine=engine):
|
|
|
super().__init__(Documents, engine)
|
|
|
|
|
|
- def add_document_with_categories(self, open_id, file_path, category_name="default"):
|
|
|
+ def add_document_with_categories(self, open_id, file_path, category_name="default") -> DocumentCategories:
|
|
|
with Session(bind=self.engine) as session:
|
|
|
doc_model:Documents = self.exec_add_or_update_file(open_id, file_path, session)
|
|
|
cr = CategoriesRepository()
|
|
|
@@ -116,11 +116,15 @@ class DocumentsRepository(DocumentBase):
|
|
|
dcr = DocumentCategoriesRepository()
|
|
|
doc_categ_model = dcr.add_or_update(DocumentCategories(id=doc_model.id, category_id=category_model.id), session)
|
|
|
session.commit()
|
|
|
- return True
|
|
|
+ # 强制刷新,让 model 从数据库总获取最新状态
|
|
|
+ session.refresh(doc_model)
|
|
|
+ session.refresh(category_model)
|
|
|
+ session.refresh(doc_categ_model)
|
|
|
+ return (doc_model, category_model, doc_categ_model)
|
|
|
|
|
|
def exec_add_or_update_file(self, open_id, file_path, session):
|
|
|
- # file_path = {DATA_DIR}/{open_id}/docs/xxx/example_file.pdf
|
|
|
- relative_path = DocumentsRepository.get_relative_path(file_path)
|
|
|
+ # file_path = {MNT_DOUYIN_DATA}/{open_id}/docs/xxx/example_file.pdf
|
|
|
+ relative_path = DocumentsRepository.get_doc_path_from_full_path(file_path)
|
|
|
if relative_path == None:
|
|
|
return
|
|
|
self.instance_model = Documents(
|
|
|
@@ -131,44 +135,65 @@ class DocumentsRepository(DocumentBase):
|
|
|
res = self.add_or_update(self.instance_model, session)
|
|
|
return res
|
|
|
|
|
|
- def get_relative_path(full_path):
|
|
|
- pattern = r'docs(/.*?)$'
|
|
|
+ '''
|
|
|
+ 从绝对路径中提取相对路径
|
|
|
+ input: {MNT_DOUYIN_DATA}/{open_id}/docs/xxx/example_file.pdf
|
|
|
+ output: xxx/example_file.pdf
|
|
|
+ '''
|
|
|
+ def get_doc_path_from_full_path(full_path):
|
|
|
+ pattern = r'docs/(.*?)$'
|
|
|
match = re.search(pattern, full_path)
|
|
|
if match:
|
|
|
return match.group(1)
|
|
|
else:
|
|
|
logger.error(f"Can not get rel path:{full_path}")
|
|
|
|
|
|
-
|
|
|
- def get_user_files_path(self, open_id: str, category_id: Optional[UUID4] = None, category_name: Optional[str] = None) -> List[str]:
|
|
|
- with Session(self.engine) as session:
|
|
|
- # 基础查询,从 Documents 表中选择 path
|
|
|
- base_statement = select(Documents.path).where(Documents.open_id == open_id)
|
|
|
-
|
|
|
- # 根据 category_id 或 category_name 进行过滤
|
|
|
- if category_id:
|
|
|
- base_statement = base_statement.where(Documents.id.in_(
|
|
|
- select(DocumentCategories.id).where(DocumentCategories.category_id == category_id)
|
|
|
- ))
|
|
|
- elif category_name:
|
|
|
- category_subquery = select(Categories.id).where(Categories.name == category_name)
|
|
|
- doc_category_subquery = select(DocumentCategories.id).where(DocumentCategories.category_id.in_(category_subquery))
|
|
|
- base_statement = base_statement.where(Documents.id.in_(doc_category_subquery))
|
|
|
-
|
|
|
- # 执行查询并返回结果
|
|
|
+ '''
|
|
|
+ 从 doc model 中提取文件相对路径
|
|
|
+ input: Documents(path=example_file.pdf)
|
|
|
+ output: {open_id}/docs/example_file.pdf
|
|
|
+ '''
|
|
|
+ def get_user_file_relpath_from_docmodel(doc_model:Documents):
|
|
|
+ return os.path.join(str(doc_model.open_id), "docs", doc_model.path)
|
|
|
+ '''
|
|
|
+ return: List[Tuple[file_path, category_id]]
|
|
|
+ '''
|
|
|
+ def get_user_files_path(self, open_id: str, category_id: Optional[UUID4] = None, category_name: Optional[str] = None) -> List[Tuple[str, UUID4]]:
|
|
|
+ with Session(self.engine) as session:
|
|
|
+ # 基础查询,从 Documents 表中选择 path 和 id
|
|
|
+ base_statement = select(Documents.path, Documents.id).where(Documents.open_id == open_id)
|
|
|
+
|
|
|
+ # 如果提供了 category_id,则通过 DocumentCategories 进行关联查询
|
|
|
+ if category_id:
|
|
|
+ base_statement = base_statement.join(DocumentCategories, Documents.id == DocumentCategories.id).where(DocumentCategories.category_id == category_id)
|
|
|
+ # 如果提供了 category_name,则先找到对应的 category_id 再进行关联查询
|
|
|
+ elif category_name:
|
|
|
+ category_subquery = select(Categories.id).where(Categories.name == category_name)
|
|
|
+ doc_category_subquery = select(DocumentCategories.id).where(DocumentCategories.category_id.in_(category_subquery))
|
|
|
+ base_statement = (
|
|
|
+ base_statement.join(DocumentCategories, Documents.id == DocumentCategories.id)
|
|
|
+ .where(DocumentCategories.id.in_(doc_category_subquery))
|
|
|
+ )
|
|
|
+
|
|
|
+ # 执行查询并返回结果(每个结果为一个元组:(文档路径, 分类ID))
|
|
|
results = session.exec(base_statement)
|
|
|
- return results.all()
|
|
|
-
|
|
|
+ return [(result.path, result.id) for result in results]
|
|
|
+
|
|
|
# 示例使用
|
|
|
def main():
|
|
|
from db.user import test_add
|
|
|
open_id = test_add()
|
|
|
# 创建实例
|
|
|
documents_repo = DocumentsRepository()
|
|
|
- documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/readme.md")
|
|
|
- documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/99readme3.md")
|
|
|
- documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/readme5.md")
|
|
|
- logger.info(documents_repo.get_user_files_path(open_id))
|
|
|
+ # model = Documents(id=uuid.UUID("f7069528-ccb9-11ee-933a-00155db00104"), status=5)
|
|
|
+ # model = documents_repo.update(model)
|
|
|
+ # res = documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/readme.md")
|
|
|
+ # documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/99readme3.md")
|
|
|
+ doc_model,_,_ = documents_repo.add_document_with_categories(open_id,"/home/user/code/open-douyin/open_id/docs/readme5.md")
|
|
|
+ rel_path = DocumentsRepository.get_user_file_relpath_from_docmodel(doc_model)
|
|
|
+ logger.info(rel_path)
|
|
|
+ # res = documents_repo.get_user_files_path(open_id)
|
|
|
+ # 假设调用服务端的代码。注意这里只是假设示例,实际上要自己编写调用的代码逻辑
|
|
|
# 添加分类
|
|
|
# doc1 = Documents(open_id=open_id, document_name="docs_fn", status="ready", file_path="/path")
|
|
|
# doc2 = Documents(open_id=open_id, document_name="docs_jj", status="ready", file_path="/path")
|