documents.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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
  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_current_user)):
  37. if user.role != "admin":
  38. raise HTTPException(
  39. status_code=status.HTTP_401_UNAUTHORIZED,
  40. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  41. )
  42. doc = Documents.get_doc_by_name(form_data.name)
  43. if doc == None:
  44. doc = Documents.insert_new_doc(user.id, form_data)
  45. if doc:
  46. return DocumentResponse(
  47. **{
  48. **doc.model_dump(),
  49. "content": json.loads(doc.content if doc.content else "{}"),
  50. }
  51. )
  52. else:
  53. raise HTTPException(
  54. status_code=status.HTTP_400_BAD_REQUEST,
  55. detail=ERROR_MESSAGES.FILE_EXISTS,
  56. )
  57. else:
  58. raise HTTPException(
  59. status_code=status.HTTP_400_BAD_REQUEST,
  60. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  61. )
  62. ############################
  63. # GetDocByName
  64. ############################
  65. @router.get("/name/{name}", response_model=Optional[DocumentResponse])
  66. async def get_doc_by_name(name: str, user=Depends(get_current_user)):
  67. doc = Documents.get_doc_by_name(name)
  68. if doc:
  69. return DocumentResponse(
  70. **{
  71. **doc.model_dump(),
  72. "content": json.loads(doc.content if doc.content else "{}"),
  73. }
  74. )
  75. else:
  76. raise HTTPException(
  77. status_code=status.HTTP_401_UNAUTHORIZED,
  78. detail=ERROR_MESSAGES.NOT_FOUND,
  79. )
  80. ############################
  81. # TagDocByName
  82. ############################
  83. class TagDocumentForm(BaseModel):
  84. name: str
  85. tags: List[dict]
  86. @router.post("/name/{name}/tags", response_model=Optional[DocumentResponse])
  87. async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_current_user)):
  88. doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
  89. if doc:
  90. return DocumentResponse(
  91. **{
  92. **doc.model_dump(),
  93. "content": json.loads(doc.content if doc.content else "{}"),
  94. }
  95. )
  96. else:
  97. raise HTTPException(
  98. status_code=status.HTTP_401_UNAUTHORIZED,
  99. detail=ERROR_MESSAGES.NOT_FOUND,
  100. )
  101. ############################
  102. # UpdateDocByName
  103. ############################
  104. @router.post("/name/{name}/update", response_model=Optional[DocumentResponse])
  105. async def update_doc_by_name(
  106. name: str, form_data: DocumentUpdateForm, user=Depends(get_current_user)
  107. ):
  108. if user.role != "admin":
  109. raise HTTPException(
  110. status_code=status.HTTP_401_UNAUTHORIZED,
  111. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  112. )
  113. doc = Documents.update_doc_by_name(name, form_data)
  114. if doc:
  115. return DocumentResponse(
  116. **{
  117. **doc.model_dump(),
  118. "content": json.loads(doc.content if doc.content else "{}"),
  119. }
  120. )
  121. else:
  122. raise HTTPException(
  123. status_code=status.HTTP_400_BAD_REQUEST,
  124. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  125. )
  126. ############################
  127. # DeleteDocByName
  128. ############################
  129. @router.delete("/name/{name}/delete", response_model=bool)
  130. async def delete_doc_by_name(name: str, user=Depends(get_current_user)):
  131. if user.role != "admin":
  132. raise HTTPException(
  133. status_code=status.HTTP_401_UNAUTHORIZED,
  134. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  135. )
  136. result = Documents.delete_doc_by_name(name)
  137. return result