users.py 3.24 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
from fastapi import Response
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
############################
# UpdateUserRole
############################


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

Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
    if user.id != form_data.id:
        return Users.update_user_role_by_id(form_data.id, form_data.role)
39
40
41
42
43

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


Timothy J. Baek's avatar
Timothy J. Baek committed
46
############################
Timothy J. Baek's avatar
Timothy J. Baek committed
47
# UpdateUserById
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
51
52
@router.post("/{user_id}/update", response_model=Optional[UserModel])
async def update_user_by_id(
53
    user_id: str, form_data: UserUpdateForm, session_user=Depends(get_admin_user)
Timothy J. Baek's avatar
Timothy J. Baek committed
54
55
56
57
):
    user = Users.get_user_by_id(user_id)

    if user:
Timothy J. Baek's avatar
Timothy J. Baek committed
58
59
        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
60
61
62
63
64
65
66
67
68
69
70
            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
71
        Auths.update_email_by_id(user_id, form_data.email.lower())
Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
74
75
        updated_user = Users.update_user_by_id(
            user_id,
            {
                "name": form_data.name,
Timothy J. Baek's avatar
Timothy J. Baek committed
76
                "email": form_data.email.lower(),
Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
80
81
82
83
                "profile_image_url": form_data.profile_image_url,
            },
        )

        if updated_user:
            return updated_user

Timothy J. Baek's avatar
Timothy J. Baek committed
84
        raise HTTPException(
Timothy J. Baek's avatar
Timothy J. Baek committed
85
            status_code=status.HTTP_400_BAD_REQUEST,
86
            detail=ERROR_MESSAGES.DEFAULT(),
Timothy J. Baek's avatar
Timothy J. Baek committed
87
        )
88

89
90
91
92
93
    raise HTTPException(
        status_code=status.HTTP_400_BAD_REQUEST,
        detail=ERROR_MESSAGES.USER_NOT_FOUND,
    )

94
95

############################
Timothy J. Baek's avatar
Timothy J. Baek committed
96
# DeleteUserById
97
98
99
100
############################


@router.delete("/{user_id}", response_model=bool)
101
102
103
104
105
106
107
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

108
        raise HTTPException(
109
110
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=ERROR_MESSAGES.DELETE_USER_ERROR,
111
        )
112
113
114
115
116

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