documents.py 4.3 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.webui.internal.db import get_db
  8. from apps.webui.models.documents import (
  9. Documents,
  10. DocumentForm,
  11. DocumentUpdateForm,
  12. DocumentModel,
  13. DocumentResponse,
  14. )
  15. from utils.utils import get_current_user, get_admin_user
  16. from constants import ERROR_MESSAGES
  17. router = APIRouter()
  18. ############################
  19. # GetDocuments
  20. ############################
  21. @router.get("/", response_model=List[DocumentResponse])
  22. async def get_documents(user=Depends(get_current_user), db=Depends(get_db)):
  23. docs = [
  24. DocumentResponse(
  25. **{
  26. **doc.model_dump(),
  27. "content": json.loads(doc.content if doc.content else "{}"),
  28. }
  29. )
  30. for doc in Documents.get_docs(db)
  31. ]
  32. return docs
  33. ############################
  34. # CreateNewDoc
  35. ############################
  36. @router.post("/create", response_model=Optional[DocumentResponse])
  37. async def create_new_doc(
  38. form_data: DocumentForm, user=Depends(get_admin_user), db=Depends(get_db)
  39. ):
  40. doc = Documents.get_doc_by_name(db, form_data.name)
  41. if doc == None:
  42. doc = Documents.insert_new_doc(db, user.id, form_data)
  43. if doc:
  44. return DocumentResponse(
  45. **{
  46. **doc.model_dump(),
  47. "content": json.loads(doc.content if doc.content else "{}"),
  48. }
  49. )
  50. else:
  51. raise HTTPException(
  52. status_code=status.HTTP_400_BAD_REQUEST,
  53. detail=ERROR_MESSAGES.FILE_EXISTS,
  54. )
  55. else:
  56. raise HTTPException(
  57. status_code=status.HTTP_400_BAD_REQUEST,
  58. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  59. )
  60. ############################
  61. # GetDocByName
  62. ############################
  63. @router.get("/doc", response_model=Optional[DocumentResponse])
  64. async def get_doc_by_name(
  65. name: str, user=Depends(get_current_user), db=Depends(get_db)
  66. ):
  67. doc = Documents.get_doc_by_name(db, 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 TagItem(BaseModel):
  84. name: str
  85. class TagDocumentForm(BaseModel):
  86. name: str
  87. tags: List[dict]
  88. @router.post("/doc/tags", response_model=Optional[DocumentResponse])
  89. async def tag_doc_by_name(
  90. form_data: TagDocumentForm, user=Depends(get_current_user), db=Depends(get_db)
  91. ):
  92. doc = Documents.update_doc_content_by_name(
  93. db, form_data.name, {"tags": form_data.tags}
  94. )
  95. if doc:
  96. return DocumentResponse(
  97. **{
  98. **doc.model_dump(),
  99. "content": json.loads(doc.content if doc.content else "{}"),
  100. }
  101. )
  102. else:
  103. raise HTTPException(
  104. status_code=status.HTTP_401_UNAUTHORIZED,
  105. detail=ERROR_MESSAGES.NOT_FOUND,
  106. )
  107. ############################
  108. # UpdateDocByName
  109. ############################
  110. @router.post("/doc/update", response_model=Optional[DocumentResponse])
  111. async def update_doc_by_name(
  112. name: str,
  113. form_data: DocumentUpdateForm,
  114. user=Depends(get_admin_user),
  115. db=Depends(get_db),
  116. ):
  117. doc = Documents.update_doc_by_name(db, name, form_data)
  118. if doc:
  119. return DocumentResponse(
  120. **{
  121. **doc.model_dump(),
  122. "content": json.loads(doc.content if doc.content else "{}"),
  123. }
  124. )
  125. else:
  126. raise HTTPException(
  127. status_code=status.HTTP_400_BAD_REQUEST,
  128. detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
  129. )
  130. ############################
  131. # DeleteDocByName
  132. ############################
  133. @router.delete("/doc/delete", response_model=bool)
  134. async def delete_doc_by_name(
  135. name: str, user=Depends(get_admin_user), db=Depends(get_db)
  136. ):
  137. result = Documents.delete_doc_by_name(db, name)
  138. return result