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
35
############################
# UpdateUserRole
############################


@router.post("/update/role", response_model=Optional[UserModel])
async def update_user_role(
36
    form_data: UserRoleUpdateForm, user=Depends(get_admin_user)
Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
39
):
    if user.id != form_data.id:
        return Users.update_user_role_by_id(form_data.id, form_data.role)
40
41
42
43
44

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


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


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

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

        if updated_user:
            return updated_user

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

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

95
96

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


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

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

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