notes.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, UserResponse
  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": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()),
  26. }
  27. )
  28. for note in Notes.get_notes_by_user_id(user.id, "write")
  29. ]
  30. return notes
  31. @router.get("/list", response_model=list[NoteUserResponse])
  32. async def get_note_list(user=Depends(get_verified_user)):
  33. notes = [
  34. NoteUserResponse(
  35. **{
  36. **note.model_dump(),
  37. "user": UserResponse(**Users.get_user_by_id(note.user_id).model_dump()),
  38. }
  39. )
  40. for note in Notes.get_notes_by_user_id(user.id, "read")
  41. ]
  42. return notes
  43. ############################
  44. # CreateNewNote
  45. ############################
  46. @router.post("/create", response_model=Optional[NoteModel])
  47. async def create_new_note(form_data: NoteForm, user=Depends(get_admin_user)):
  48. try:
  49. note = Notes.insert_new_note(form_data, user.id)
  50. return note
  51. except Exception as e:
  52. log.exception(e)
  53. raise HTTPException(
  54. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  55. )
  56. ############################
  57. # GetNoteById
  58. ############################
  59. @router.get("/{id}", response_model=Optional[NoteModel])
  60. async def get_note_by_id(id: str, user=Depends(get_verified_user)):
  61. note = Notes.get_note_by_id(id)
  62. if not note:
  63. raise HTTPException(
  64. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  65. )
  66. if user.role != "admin" and not has_access(
  67. user.id, type="read", access_control=note.access_control
  68. ):
  69. raise HTTPException(
  70. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  71. )
  72. return note
  73. ############################
  74. # UpdateNoteById
  75. ############################
  76. @router.post("/{id}/update", response_model=Optional[NoteModel])
  77. async def update_note_by_id(
  78. id: str, form_data: NoteForm, user=Depends(get_verified_user)
  79. ):
  80. note = Notes.get_note_by_id(id)
  81. if not note:
  82. raise HTTPException(
  83. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  84. )
  85. if user.role != "admin" and not has_access(
  86. user.id, type="write", access_control=note.access_control
  87. ):
  88. raise HTTPException(
  89. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  90. )
  91. try:
  92. note = Notes.update_note_by_id(id, form_data)
  93. return note
  94. except Exception as e:
  95. log.exception(e)
  96. raise HTTPException(
  97. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  98. )
  99. ############################
  100. # DeleteNoteById
  101. ############################
  102. @router.delete("/{id}/delete", response_model=bool)
  103. async def delete_note_by_id(id: str, user=Depends(get_verified_user)):
  104. note = Notes.get_note_by_id(id)
  105. if not note:
  106. raise HTTPException(
  107. status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.NOT_FOUND
  108. )
  109. if user.role != "admin" and not has_access(
  110. user.id, type="write", access_control=note.access_control
  111. ):
  112. raise HTTPException(
  113. status_code=status.HTTP_403_FORBIDDEN, detail=ERROR_MESSAGES.DEFAULT()
  114. )
  115. try:
  116. note = Notes.delete_note_by_id(id)
  117. return True
  118. except Exception as e:
  119. log.exception(e)
  120. raise HTTPException(
  121. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  122. )