groups.py 3.2 KB

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