documents.py 4.1 KB

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