groups.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import os
  2. from pathlib import Path
  3. from typing import Optional
  4. from open_webui.apps.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.utils 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_admin_user)):
  20. return Groups.get_groups()
  21. ############################
  22. # CreateNewGroup
  23. ############################
  24. @router.post("/create", response_model=Optional[GroupResponse])
  25. async def create_new_function(form_data: GroupForm, user=Depends(get_admin_user)):
  26. try:
  27. group = Groups.insert_new_group(user.id, form_data)
  28. if group:
  29. return group
  30. else:
  31. raise HTTPException(
  32. status_code=status.HTTP_400_BAD_REQUEST,
  33. detail=ERROR_MESSAGES.DEFAULT("Error creating group"),
  34. )
  35. except Exception as e:
  36. print(e)
  37. raise HTTPException(
  38. status_code=status.HTTP_400_BAD_REQUEST,
  39. detail=ERROR_MESSAGES.DEFAULT(e),
  40. )
  41. ############################
  42. # GetGroupById
  43. ############################
  44. @router.get("/id/{id}", response_model=Optional[GroupResponse])
  45. async def get_group_by_id(id: str, user=Depends(get_admin_user)):
  46. group = Groups.get_group_by_id(id)
  47. if group:
  48. return group
  49. else:
  50. raise HTTPException(
  51. status_code=status.HTTP_401_UNAUTHORIZED,
  52. detail=ERROR_MESSAGES.NOT_FOUND,
  53. )
  54. ############################
  55. # UpdateGroupById
  56. ############################
  57. @router.post("/id/{id}/update", response_model=Optional[GroupResponse])
  58. async def update_group_by_id(
  59. id: str, form_data: GroupUpdateForm, user=Depends(get_admin_user)
  60. ):
  61. try:
  62. group = Groups.update_group_by_id(id, form_data)
  63. if group:
  64. return group
  65. else:
  66. raise HTTPException(
  67. status_code=status.HTTP_400_BAD_REQUEST,
  68. detail=ERROR_MESSAGES.DEFAULT("Error updating group"),
  69. )
  70. except Exception as e:
  71. print(e)
  72. raise HTTPException(
  73. status_code=status.HTTP_400_BAD_REQUEST,
  74. detail=ERROR_MESSAGES.DEFAULT(e),
  75. )
  76. ############################
  77. # DeleteGroupById
  78. ############################
  79. @router.delete("/id/{id}/delete", response_model=bool)
  80. async def delete_group_by_id(id: str, user=Depends(get_admin_user)):
  81. try:
  82. result = Groups.delete_group_by_id(id)
  83. if result:
  84. return result
  85. else:
  86. raise HTTPException(
  87. status_code=status.HTTP_400_BAD_REQUEST,
  88. detail=ERROR_MESSAGES.DEFAULT("Error deleting group"),
  89. )
  90. except Exception as e:
  91. print(e)
  92. raise HTTPException(
  93. status_code=status.HTTP_400_BAD_REQUEST,
  94. detail=ERROR_MESSAGES.DEFAULT(e),
  95. )