documents.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from fastapi import Depends, FastAPI, HTTPException, status
  2. from datetime import datetime, timedelta
  3. from typing import List, Union, Optional
  4. from fastapi import APIRouter
  5. from pydantic import BaseModel
  6. import json
  7. from apps.web.models.documents import (
  8. Documents,
  9. DocumentForm,
  10. DocumentUpdateForm,
  11. DocumentModel,
  12. DocumentResponse,
  13. )
  14. from utils.utils import get_current_user, get_admin_user
  15. from constants import ERROR_MESSAGES
  16. router = APIRouter()
  17. ############################
  18. # GetDocuments
  19. ############################
  20. @router.get("/", response_model=List[DocumentResponse])
  21. async def get_documents(user=Depends(get_current_user)):
  22. docs = [
  23. DocumentResponse(
  24. **{
  25. **doc.model_dump(),
  26. "content": json.loads(doc.content if doc.content else "{}"),
  27. }
  28. )
  29. for doc in Documents.get_docs()
  30. ]
  31. return docs
  32. ############################
  33. # CreateNewDoc
  34. ############################
  35. @router.post("/create", response_model=Optional[DocumentResponse])
  36. async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
  37. doc = Documents.get_doc_by_name(form_data.name)
  38. if doc == None:
  39. doc = Documents.insert_new_doc(user.id, form_data)
  40. if doc:
  41. return DocumentResponse(
  42. **{
  43. **doc.model_dump(),
  44. "content": json.loads(doc.content if doc.content else "{}"),
  45. }
  46. )
  47. else:
  48. raise HTTPException(
  49. status_code=status.HTTP_400_BAD_REQUEST,
  50. detail=ERROR_MESSAGES.FILE_EXISTS,
  51. )
  52. else:
  53. raise HTTPException(
  54. status_code=status.HTTP_400_BAD_REQUEST,
  55. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  56. )
  57. ############################
  58. # GetDocByName
  59. ############################
  60. @router.get("/name/{name}", response_model=Optional[DocumentResponse])
  61. async def get_doc_by_name(name: str, user=Depends(get_current_user)):
  62. doc = Documents.get_doc_by_name(name)
  63. if doc:
  64. return DocumentResponse(
  65. **{
  66. **doc.model_dump(),
  67. "content": json.loads(doc.content if doc.content else "{}"),
  68. }
  69. )
  70. else:
  71. raise HTTPException(
  72. status_code=status.HTTP_401_UNAUTHORIZED,
  73. detail=ERROR_MESSAGES.NOT_FOUND,
  74. )
  75. ############################
  76. # TagDocByName
  77. ############################
  78. class TagDocumentForm(BaseModel):
  79. name: str
  80. tags: List[dict]
  81. @router.post("/name/{name}/tags", response_model=Optional[DocumentResponse])
  82. async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_current_user)):
  83. doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
  84. if doc:
  85. return DocumentResponse(
  86. **{
  87. **doc.model_dump(),
  88. "content": json.loads(doc.content if doc.content else "{}"),
  89. }
  90. )
  91. else:
  92. raise HTTPException(
  93. status_code=status.HTTP_401_UNAUTHORIZED,
  94. detail=ERROR_MESSAGES.NOT_FOUND,
  95. )
  96. ############################
  97. # UpdateDocByName
  98. ############################
  99. @router.post("/name/{name}/update", response_model=Optional[DocumentResponse])
  100. async def update_doc_by_name(
  101. name: str, form_data: DocumentUpdateForm, user=Depends(get_admin_user)
  102. ):
  103. doc = Documents.update_doc_by_name(name, form_data)
  104. if doc:
  105. return DocumentResponse(
  106. **{
  107. **doc.model_dump(),
  108. "content": json.loads(doc.content if doc.content else "{}"),
  109. }
  110. )
  111. else:
  112. raise HTTPException(
  113. status_code=status.HTTP_400_BAD_REQUEST,
  114. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  115. )
  116. ############################
  117. # DeleteDocByName
  118. ############################
  119. @router.delete("/name/{name}/delete", response_model=bool)
  120. async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
  121. result = Documents.delete_doc_by_name(name)
  122. return result