"test/srt/test_openai_function_calling.py" did not exist on "f265d15b9681ad3fc6c0983e0bb06eefcb7b1274"
users.py 3.71 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
from fastapi import Response, Request
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
4
5
6
7
8
9
10
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
from typing import List, Union, Optional

from fastapi import APIRouter
from pydantic import BaseModel
import time
import uuid

Timothy J. Baek's avatar
Timothy J. Baek committed
11
from apps.web.models.users import UserModel, UserUpdateForm, UserRoleUpdateForm, Users
Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
from apps.web.models.auths import Auths

14
from utils.utils import get_current_user, get_password_hash, get_admin_user
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17
18
19
20
21
22
23
24
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetUsers
############################


@router.get("/", response_model=List[UserModel])
25
async def get_users(skip: int = 0, limit: int = 50, user=Depends(get_admin_user)):
26
    return Users.get_users(skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
27
28


Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
############################
# User Permissions
############################


@router.get("/permissions/user")
async def get_user_permissions(request: Request, user=Depends(get_admin_user)):
    return request.app.state.USER_PERMISSIONS


@router.post("/permissions/user")
async def update_user_permissions(
    request: Request, form_data: dict, user=Depends(get_admin_user)
):
    request.app.state.USER_PERMISSIONS = form_data
    return request.app.state.USER_PERMISSIONS


Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
52
############################
# UpdateUserRole
############################


@router.post("/update/role", response_model=Optional[UserModel])
Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
async def update_user_role(form_data: UserRoleUpdateForm, user=Depends(get_admin_user)):

Timothy J. Baek's avatar
Timothy J. Baek committed
55
56
    if user.id != form_data.id:
        return Users.update_user_role_by_id(form_data.id, form_data.role)
57
58
59
60
61

    raise HTTPException(
        status_code=status.HTTP_403_FORBIDDEN,
        detail=ERROR_MESSAGES.ACTION_PROHIBITED,
    )
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63


Timothy J. Baek's avatar
Timothy J. Baek committed
64
############################
Timothy J. Baek's avatar
Timothy J. Baek committed
65
# UpdateUserById
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
69
70
@router.post("/{user_id}/update", response_model=Optional[UserModel])
async def update_user_by_id(
71
    user_id: str, form_data: UserUpdateForm, session_user=Depends(get_admin_user)
Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
74
75
):
    user = Users.get_user_by_id(user_id)

    if user:
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
        if form_data.email.lower() != user.email:
            email_user = Users.get_user_by_email(form_data.email.lower())
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
80
81
82
83
84
85
86
87
88
            if email_user:
                raise HTTPException(
                    status_code=status.HTTP_400_BAD_REQUEST,
                    detail=ERROR_MESSAGES.EMAIL_TAKEN,
                )

        if form_data.password:
            hashed = get_password_hash(form_data.password)
            print(hashed)
            Auths.update_user_password_by_id(user_id, hashed)

Timothy J. Baek's avatar
Timothy J. Baek committed
89
        Auths.update_email_by_id(user_id, form_data.email.lower())
Timothy J. Baek's avatar
Timothy J. Baek committed
90
91
92
93
        updated_user = Users.update_user_by_id(
            user_id,
            {
                "name": form_data.name,
Timothy J. Baek's avatar
Timothy J. Baek committed
94
                "email": form_data.email.lower(),
Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
97
98
99
100
101
                "profile_image_url": form_data.profile_image_url,
            },
        )

        if updated_user:
            return updated_user

Timothy J. Baek's avatar
Timothy J. Baek committed
102
        raise HTTPException(
Timothy J. Baek's avatar
Timothy J. Baek committed
103
            status_code=status.HTTP_400_BAD_REQUEST,
104
            detail=ERROR_MESSAGES.DEFAULT(),
Timothy J. Baek's avatar
Timothy J. Baek committed
105
        )
106

107
108
109
110
111
    raise HTTPException(
        status_code=status.HTTP_400_BAD_REQUEST,
        detail=ERROR_MESSAGES.USER_NOT_FOUND,
    )

112
113

############################
Timothy J. Baek's avatar
Timothy J. Baek committed
114
# DeleteUserById
115
116
117
118
############################


@router.delete("/{user_id}", response_model=bool)
119
120
121
122
123
124
125
async def delete_user_by_id(user_id: str, user=Depends(get_admin_user)):
    if user.id != user_id:
        result = Auths.delete_auth_by_id(user_id)

        if result:
            return True

126
        raise HTTPException(
127
128
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=ERROR_MESSAGES.DELETE_USER_ERROR,
129
        )
130
131
132
133
134

    raise HTTPException(
        status_code=status.HTTP_403_FORBIDDEN,
        detail=ERROR_MESSAGES.ACTION_PROHIBITED,
    )