diff --git a/backend/wiki/wiki_workspace/document/export/router.py b/backend/wiki/wiki_workspace/document/export/router.py index 965bcdd..82225d5 100644 --- a/backend/wiki/wiki_workspace/document/export/router.py +++ b/backend/wiki/wiki_workspace/document/export/router.py @@ -61,8 +61,8 @@ async def export_document( ya_disk) if export_type.DOCX: res = converters.docx.convert(html) - headers = {"Content-Disposition": f'attachment; filename="{title}".docx'} - return Response(res, headers=headers, media_type="application/pdf") + headers = {"Content-Disposition": f'attachment; filename*=UTF-8\'\'"{title}"'} + return Response(res, headers=headers, media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document") raise Exception diff --git a/backend/wiki/wiki_workspace/document/repository.py b/backend/wiki/wiki_workspace/document/repository.py index e4c326f..d925a23 100644 --- a/backend/wiki/wiki_workspace/document/repository.py +++ b/backend/wiki/wiki_workspace/document/repository.py @@ -1,7 +1,7 @@ from typing import Optional from uuid import UUID -from sqlalchemy import select, and_ +from sqlalchemy import select, and_, update from starlette import status from wiki.common.exceptions import WikiException, WikiErrorCode @@ -41,13 +41,22 @@ async def _get_result_document_with_permission(self, user_id: UUID, *whereclause *whereclause ) - async def get_documents_with_permission(self, user_id: UUID) -> list: - res = await self._get_result_document_with_permission(user_id) + async def get_documents_with_permission(self, user_id: UUID, is_only_existing: bool = True) -> list: + whereclause = [] + if is_only_existing: + whereclause.append(Document.is_deleted == False) + res = await self._get_result_document_with_permission(user_id, *whereclause) return res.all() @menage_db_not_found_result_method(NotFoundResultMode.EXCEPTION, ex=document_not_found_exception) - async def get_document_with_permission_by_id(self, user_id: UUID, document_id: UUID): - res = await self._get_result_document_with_permission(user_id, Document.id == document_id) + async def get_document_with_permission_by_id(self, + user_id: UUID, + document_id: UUID, + is_only_existing: bool = True): + whereclause = [Document.id == document_id] + if is_only_existing: + whereclause.append(Document.is_deleted == False) + res = await self._get_result_document_with_permission(user_id, *whereclause) return res.first() @menage_db_not_found_result_method(NotFoundResultMode.EXCEPTION, ex=document_not_found_exception) @@ -82,6 +91,11 @@ async def mark_document_delete(self, document_id: UUID): document = await self.get_document_by_id(document_id) document.is_deleted = True + query = (update(Document).where(Document.parent_document_id == document_id) + .values(parent_document_id=document.parent_document_id)) + + await self.session.execute(query) + self.session.add(document) @menage_db_commit_method(CommitMode.FLUSH) diff --git a/backend/wiki/wiki_workspace/document/router.py b/backend/wiki/wiki_workspace/document/router.py index 41ff885..97b6c82 100644 --- a/backend/wiki/wiki_workspace/document/router.py +++ b/backend/wiki/wiki_workspace/document/router.py @@ -174,6 +174,14 @@ async def delete_document( session: AsyncSession = Depends(get_db), user: WikiUserHandlerData = Depends(BasePermission(responsibility=ResponsibilityType.VIEWER)) ): + """ + ## Deleted document by id + **You have to have a deletion permission or be an administrator.** + + When a document is deleted, the hierarchy changes: + all child documents become child documents of the parent. + """ + document_repository: DocumentRepository = DocumentRepository(session) if user.wiki_api_client.responsibility != ResponsibilityType.ADMIN: document = await document_repository.get_document_with_permission_by_id(user.id, document_id) @@ -186,7 +194,9 @@ async def delete_document( else: document = await document_repository.get_document_by_id(document_id) - await document_repository + await document_repository.mark_document_delete(document.id) + + return BaseResponse(msg="Document deleted") @document_router.post( diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 87df71f..3a88536 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -2541,6 +2541,47 @@ paths: summary: Get tree documents tags: - WorkspaceDocument + /api/v1/document/{document_id}: + delete: + description: '## Deleted document by id + + **You have to have a deletion permission or be an administrator.** + + + When a document is deleted, the hierarchy changes: + + all child documents become child documents of the parent.' + operationId: delete_document_api_v1_document__document_id__delete + parameters: + - in: path + name: document_id + required: true + schema: + format: uuid + title: Document Id + type: string + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/BaseResponse' + description: Successful Response + '422': + content: + application/json: + schema: + $ref: '#/components/schemas/HTTPValidationError' + description: Validation Error + security: + - WikiRootApiKey: [] + - WikiApiKey: [] + - WikiApiKey: [] + - WikiAccessToken: [] + - WikiBearer: [] + summary: Delete document + tags: + - WorkspaceDocument /api/v1/document/{document_id}/publish: post: description: '## Publish document