users.py 3.42 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

Timothy J. Baek's avatar
Timothy J. Baek committed
14
from utils.utils import get_current_user, get_password_hash
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])
Timothy J. Baek's avatar
Timothy J. Baek committed
25
async def get_users(skip: int = 0, limit: int = 50, user=Depends(get_current_user)):
26
    if user.role != "admin":
Timothy J. Baek's avatar
Timothy J. Baek committed
27
        raise HTTPException(
28
29
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
30
        )
31
    return Users.get_users(skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34


############################
Timothy J. Baek's avatar
Timothy J. Baek committed
35
# UpdateUserById
Timothy J. Baek's avatar
Timothy J. Baek committed
36
37
38
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
41
42
43
@router.post("/{user_id}/update", response_model=Optional[UserModel])
async def update_user_by_id(
    user_id: str, form_data: UserUpdateForm, session_user=Depends(get_current_user)
):
    if session_user.role != "admin":
44
45
46
47
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
48

Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    user = Users.get_user_by_id(user_id)

    if user:
        if form_data.email != user.email:
            email_user = Users.get_user_by_email(form_data.email)
            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)

        Auths.update_email_by_id(user_id, form_data.email)
        updated_user = Users.update_user_by_id(
            user_id,
            {
                "name": form_data.name,
                "email": form_data.email,
                "profile_image_url": form_data.profile_image_url,
            },
        )

        if updated_user:
            return updated_user
        else:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ERROR_MESSAGES.DEFAULT(),
            )

Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
    else:
        raise HTTPException(
Timothy J. Baek's avatar
Timothy J. Baek committed
85
86
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.USER_NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
87
        )
88
89
90


############################
Timothy J. Baek's avatar
Timothy J. Baek committed
91
# DeleteUserById
92
93
94
95
############################


@router.delete("/{user_id}", response_model=bool)
Anuraag Jain's avatar
Anuraag Jain committed
96
97
98
99
100
101
102
async def delete_user_by_id(user_id: str, user=Depends(get_current_user)):
    if user.role == "admin":
        if user.id != user_id:
            result = Auths.delete_auth_by_id(user_id)

            if result:
                return True
103
104
            else:
                raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
105
106
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    detail=ERROR_MESSAGES.DELETE_USER_ERROR,
107
108
109
110
                )
        else:
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
Anuraag Jain's avatar
Anuraag Jain committed
111
                detail=ERROR_MESSAGES.ACTION_PROHIBITED,
112
113
114
            )
    else:
        raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
115
116
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
117
        )