documents.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_verified_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_verified_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("/doc", response_model=Optional[DocumentResponse])
  61. async def get_doc_by_name(name: str, user=Depends(get_verified_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 TagItem(BaseModel):
  79. name: str
  80. class TagDocumentForm(BaseModel):
  81. name: str
  82. tags: List[dict]
  83. @router.post("/doc/tags", response_model=Optional[DocumentResponse])
  84. async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_verified_user)):
  85. doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
  86. if doc:
  87. return DocumentResponse(
  88. **{
  89. **doc.model_dump(),
  90. "content": json.loads(doc.content if doc.content else "{}"),
  91. }
  92. )
  93. else:
  94. raise HTTPException(
  95. status_code=status.HTTP_401_UNAUTHORIZED,
  96. detail=ERROR_MESSAGES.NOT_FOUND,
  97. )
  98. ############################
  99. # UpdateDocByName
  100. ############################
  101. @router.post("/doc/update", response_model=Optional[DocumentResponse])
  102. async def update_doc_by_name(
  103. name: str,
  104. form_data: DocumentUpdateForm,
  105. user=Depends(get_admin_user),
  106. ):
  107. doc = Documents.update_doc_by_name(name, form_data)
  108. if doc:
  109. return DocumentResponse(
  110. **{
  111. **doc.model_dump(),
  112. "content": json.loads(doc.content if doc.content else "{}"),
  113. }
  114. )
  115. else:
  116. raise HTTPException(
  117. status_code=status.HTTP_400_BAD_REQUEST,
  118. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  119. )
  120. ############################
  121. # DeleteDocByName
  122. ############################
  123. @router.delete("/doc/delete", response_model=bool)
  124. async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
  125. result = Documents.delete_doc_by_name(name)
  126. return result