123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import os
- from pathlib import Path
- from typing import Optional
- from open_webui.models.users import Users
- from open_webui.models.groups import (
- Groups,
- GroupForm,
- GroupUpdateForm,
- GroupResponse,
- )
- from open_webui.config import CACHE_DIR
- from open_webui.constants import ERROR_MESSAGES
- from fastapi import APIRouter, Depends, HTTPException, Request, status
- from open_webui.utils.auth import get_admin_user, get_verified_user
- router = APIRouter()
- ############################
- # GetFunctions
- ############################
- @router.get("/", response_model=list[GroupResponse])
- async def get_groups(user=Depends(get_verified_user)):
- if user.role == "admin":
- return Groups.get_groups()
- else:
- return Groups.get_groups_by_member_id(user.id)
- ############################
- # CreateNewGroup
- ############################
- @router.post("/create", response_model=Optional[GroupResponse])
- async def create_new_function(form_data: GroupForm, user=Depends(get_admin_user)):
- try:
- group = Groups.insert_new_group(user.id, form_data)
- if group:
- return group
- else:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT("Error creating group"),
- )
- except Exception as e:
- print(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT(e),
- )
- ############################
- # GetGroupById
- ############################
- @router.get("/id/{id}", response_model=Optional[GroupResponse])
- async def get_group_by_id(id: str, user=Depends(get_admin_user)):
- group = Groups.get_group_by_id(id)
- if group:
- return group
- else:
- raise HTTPException(
- status_code=status.HTTP_401_UNAUTHORIZED,
- detail=ERROR_MESSAGES.NOT_FOUND,
- )
- ############################
- # UpdateGroupById
- ############################
- @router.post("/id/{id}/update", response_model=Optional[GroupResponse])
- async def update_group_by_id(
- id: str, form_data: GroupUpdateForm, user=Depends(get_admin_user)
- ):
- try:
- if form_data.user_ids:
- form_data.user_ids = Users.get_valid_user_ids(form_data.user_ids)
- group = Groups.update_group_by_id(id, form_data)
- if group:
- return group
- else:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT("Error updating group"),
- )
- except Exception as e:
- print(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT(e),
- )
- ############################
- # DeleteGroupById
- ############################
- @router.delete("/id/{id}/delete", response_model=bool)
- async def delete_group_by_id(id: str, user=Depends(get_admin_user)):
- try:
- result = Groups.delete_group_by_id(id)
- if result:
- return result
- else:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT("Error deleting group"),
- )
- except Exception as e:
- print(e)
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail=ERROR_MESSAGES.DEFAULT(e),
- )
|