groups.py 3.4 KB

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