1
0

notes.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import json
  2. import logging
  3. from typing import Optional
  4. from fastapi import APIRouter, Depends, HTTPException, Request, status, BackgroundTasks
  5. from pydantic import BaseModel
  6. from open_webui.models.users import Users, UserNameResponse
  7. from open_webui.models.notes import Notes, NoteModel, NoteForm, NoteUserResponse
  8. from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
  9. from open_webui.constants import ERROR_MESSAGES
  10. from open_webui.env import SRC_LOG_LEVELS
  11. from open_webui.utils.auth import get_admin_user, get_verified_user
  12. from open_webui.utils.access_control import has_access
  13. log = logging.getLogger(__name__)
  14. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  15. router = APIRouter()
  16. ############################
  17. # GetNotes
  18. ############################
  19. @router.get("/", response_model=list[NoteUserResponse])
  20. async def get_notes(user=Depends(get_verified_user)):
  21. notes = [
  22. NoteUserResponse(
  23. **{
  24. **note.model_dump(),
  25. "user": UserNameResponse(
  26. **Users.get_user_by_id(note.user_id).model_dump()
  27. ),
  28. }
  29. )
  30. for note in Notes.get_notes_by_user_id(user.id, "write")
  31. ]
  32. return notes
  33. @router.get("/list", response_model=list[NoteUserResponse])
  34. async def get_note_list(user=Depends(get_verified_user)):
  35. notes = [
  36. NoteUserResponse(
  37. **{
  38. **note.model_dump(),
  39. "user": UserNameResponse(
  40. **Users.get_user_by_id(note.user_id).model_dump()
  41. ),
  42. }
  43. )
  44. for note in Notes.get_notes_by_user_id(user.id, "read")
  45. ]
  46. return notes
  47. ############################
  48. # CreateNewNote
  49. ############################
  50. @router.post("/create", response_model=Optional[NoteModel])
  51. async def create_new_note(form_data: NoteForm, user=Depends(get_admin_user)):
  52. try:
  53. note = Notes.insert_new_note(form_data, user.id)
  54. return note
  55. except Exception as e:
  56. log.exception(e)
  57. raise HTTPException(
  58. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  59. )
  60. ############################
  61. # GetNoteById
  62. ############################
  63. @router.get("/{id}", response_model=Optional[NoteModel])
  64. async def get_note_by_id(id: str, user=Depends(get_verified_user)):
  65. note = Notes.get_note_by_id(id)
  66. if not note:
  67. raise HTTPException(
  68. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  69. )
  70. if user.role != "admin" and not has_access(
  71. user.id, type="read", access_control=note.access_control
  72. ):
  73. raise HTTPException(
  74. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  75. )
  76. return note
  77. ############################
  78. # UpdateNoteById
  79. ############################
  80. @router.post("/{id}/update", response_model=Optional[NoteModel])
  81. async def update_note_by_id(
  82. id: str, form_data: NoteForm, user=Depends(get_verified_user)
  83. ):
  84. note = Notes.get_note_by_id(id)
  85. if not note:
  86. raise HTTPException(
  87. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  88. )
  89. if user.role != "admin" and not has_access(
  90. user.id, type="write", access_control=note.access_control
  91. ):
  92. raise HTTPException(
  93. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  94. )
  95. try:
  96. note = Notes.update_note_by_id(id, form_data)
  97. return note
  98. except Exception as e:
  99. log.exception(e)
  100. raise HTTPException(
  101. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  102. )
  103. ############################
  104. # DeleteNoteById
  105. ############################
  106. @router.delete("/{id}/delete", response_model=bool)
  107. async def delete_note_by_id(id: str, user=Depends(get_verified_user)):
  108. note = Notes.get_note_by_id(id)
  109. if not note:
  110. raise HTTPException(
  111. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  112. )
  113. if user.role != "admin" and not has_access(
  114. user.id, type="write", access_control=note.access_control
  115. ):
  116. raise HTTPException(
  117. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  118. )
  119. try:
  120. note = Notes.delete_note_by_id(id)
  121. return True
  122. except Exception as e:
  123. log.exception(e)
  124. raise HTTPException(
  125. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  126. )