groups.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import os
  2. from pathlib import Path
  3. from typing import Optional
  4. import logging
  5. from open_webui.models.users import Users
  6. from open_webui.models.groups import (
  7. Groups,
  8. GroupForm,
  9. GroupUpdateForm,
  10. GroupResponse,
  11. )
  12. from open_webui.config import CACHE_DIR
  13. from open_webui.constants import ERROR_MESSAGES
  14. from fastapi import APIRouter, Depends, HTTPException, Request, status
  15. from open_webui.utils.auth import get_admin_user, get_verified_user
  16. from open_webui.env import SRC_LOG_LEVELS
  17. log = logging.getLogger(__name__)
  18. log.setLevel(SRC_LOG_LEVELS["MAIN"])
  19. router = APIRouter()
  20. ############################
  21. # GetFunctions
  22. ############################
  23. @router.get("/", response_model=list[GroupResponse])
  24. async def get_groups(user=Depends(get_verified_user)):
  25. if user.role == "admin":
  26. return Groups.get_groups()
  27. else:
  28. return Groups.get_groups_by_member_id(user.id)
  29. ############################
  30. # CreateNewGroup
  31. ############################
  32. @router.post("/create", response_model=Optional[GroupResponse])
  33. async def create_new_group(form_data: GroupForm, user=Depends(get_admin_user)):
  34. try:
  35. group = Groups.insert_new_group(user.id, form_data)
  36. if group:
  37. return group
  38. else:
  39. raise HTTPException(
  40. status_code=status.HTTP_400_BAD_REQUEST,
  41. detail=ERROR_MESSAGES.DEFAULT("Error creating group"),
  42. )
  43. except Exception as e:
  44. log.exception(f"Error creating a new group: {e}")
  45. raise HTTPException(
  46. status_code=status.HTTP_400_BAD_REQUEST,
  47. detail=ERROR_MESSAGES.DEFAULT(e),
  48. )
  49. ############################
  50. # GetGroupById
  51. ############################
  52. @router.get("/id/{id}", response_model=Optional[GroupResponse])
  53. async def get_group_by_id(id: str, user=Depends(get_admin_user)):
  54. group = Groups.get_group_by_id(id)
  55. if group:
  56. return group
  57. else:
  58. raise HTTPException(
  59. status_code=status.HTTP_401_UNAUTHORIZED,
  60. detail=ERROR_MESSAGES.NOT_FOUND,
  61. )
  62. ############################
  63. # UpdateGroupById
  64. ############################
  65. @router.post("/id/{id}/update", response_model=Optional[GroupResponse])
  66. async def update_group_by_id(
  67. id: str, form_data: GroupUpdateForm, user=Depends(get_admin_user)
  68. ):
  69. try:
  70. if form_data.user_ids:
  71. form_data.user_ids = Users.get_valid_user_ids(form_data.user_ids)
  72. group = Groups.update_group_by_id(id, form_data)
  73. if group:
  74. return group
  75. else:
  76. raise HTTPException(
  77. status_code=status.HTTP_400_BAD_REQUEST,
  78. detail=ERROR_MESSAGES.DEFAULT("Error updating group"),
  79. )
  80. except Exception as e:
  81. log.exception(f"Error updating group {id}: {e}")
  82. raise HTTPException(
  83. status_code=status.HTTP_400_BAD_REQUEST,
  84. detail=ERROR_MESSAGES.DEFAULT(e),
  85. )
  86. ############################
  87. # DeleteGroupById
  88. ############################
  89. @router.delete("/id/{id}/delete", response_model=bool)
  90. async def delete_group_by_id(id: str, user=Depends(get_admin_user)):
  91. try:
  92. result = Groups.delete_group_by_id(id)
  93. if result:
  94. return result
  95. else:
  96. raise HTTPException(
  97. status_code=status.HTTP_400_BAD_REQUEST,
  98. detail=ERROR_MESSAGES.DEFAULT("Error deleting group"),
  99. )
  100. except Exception as e:
  101. log.exception(f"Error deleting group {id}: {e}")
  102. raise HTTPException(
  103. status_code=status.HTTP_400_BAD_REQUEST,
  104. detail=ERROR_MESSAGES.DEFAULT(e),
  105. )