users.py 3.9 KB
Newer Older
1
from pydantic import BaseModel
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
from peewee import *
from playhouse.shortcuts import model_to_dict
4
5
from typing import List, Union, Optional
import time
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7
from utils.misc import get_gravatar_url

Timothy J. Baek's avatar
Timothy J. Baek committed
8
from apps.web.internal.db import DB
Timothy J. Baek's avatar
Timothy J. Baek committed
9
from apps.web.models.chats import Chats
10

11
12
13
14
15
####################
# User DB Schema
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
16
17
18
19
20
21
22
23
24
25
26
27
class User(Model):
    id = CharField(unique=True)
    name = CharField()
    email = CharField()
    role = CharField()
    profile_image_url = CharField()
    timestamp = DateField()

    class Meta:
        database = DB


28
29
30
31
class UserModel(BaseModel):
    id: str
    name: str
    email: str
Timothy J. Baek's avatar
Timothy J. Baek committed
32
    role: str = "pending"
33
    profile_image_url: str
Timothy J. Baek's avatar
Timothy J. Baek committed
34
    timestamp: int  # timestamp in epoch
35
36
37
38
39
40
41


####################
# Forms
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
42
43
44
45
46
class UserRoleUpdateForm(BaseModel):
    id: str
    role: str


Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
52
class UserUpdateForm(BaseModel):
    name: str
    email: str
    profile_image_url: str
    password: Optional[str] = None

53

Timothy J. Baek's avatar
Timothy J. Baek committed
54
class UsersTable:
55
56
    def __init__(self, db):
        self.db = db
Timothy J. Baek's avatar
Timothy J. Baek committed
57
        self.db.create_tables([User])
58

Timothy J. Baek's avatar
Timothy J. Baek committed
59
    def insert_new_user(
Danny Liu's avatar
Danny Liu committed
60
61
62
63
        self,
        id: str,
        name: str,
        email: str,
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
64
        profile_image_url: str = "/user.png",
Danny Liu's avatar
Danny Liu committed
65
        role: str = "pending",
Timothy J. Baek's avatar
Timothy J. Baek committed
66
    ) -> Optional[UserModel]:
67
68
69
70
71
72
        user = UserModel(
            **{
                "id": id,
                "name": name,
                "email": email,
                "role": role,
73
                "profile_image_url": profile_image_url,
Timothy J. Baek's avatar
Timothy J. Baek committed
74
                "timestamp": int(time.time()),
Timothy J. Baek's avatar
Timothy J. Baek committed
75
76
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
77
        result = User.create(**user.model_dump())
78
79
80
81
82
        if result:
            return user
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
87
88
    def get_user_by_id(self, id: str) -> Optional[UserModel]:
        try:
            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None
89

Timothy J. Baek's avatar
Timothy J. Baek committed
90
91
92
93
94
    def get_user_by_email(self, email: str) -> Optional[UserModel]:
        try:
            user = User.get(User.email == email)
            return UserModel(**model_to_dict(user))
        except:
95
96
            return None

97
    def get_users(self, skip: int = 0, limit: int = 50) -> List[UserModel]:
98
        return [
Timothy J. Baek's avatar
Timothy J. Baek committed
99
            UserModel(**model_to_dict(user))
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
            for user in User.select()
            # .limit(limit).offset(skip)
102
103
        ]

104
    def get_num_users(self) -> Optional[int]:
Timothy J. Baek's avatar
Timothy J. Baek committed
105
        return User.select().count()
Timothy J. Baek's avatar
Timothy J. Baek committed
106

Timothy J. Baek's avatar
Timothy J. Baek committed
107
    def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
108
109
110
111
112
113
114
115
        try:
            query = User.update(role=role).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
116

117
118
119
120
121
122
123
124
125
126
127
128
129
130
    def update_user_profile_image_url_by_id(
        self, id: str, profile_image_url: str
    ) -> Optional[UserModel]:
        try:
            query = User.update(profile_image_url=profile_image_url).where(
                User.id == id
            )
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
131
132
133
134
135
136
137
138
139
140
    def update_user_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
        try:
            query = User.update(**updated).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

141
142
143
    def delete_user_by_id(self, id: str) -> bool:
        try:
            # Delete User Chats
Timothy J. Baek's avatar
Timothy J. Baek committed
144
            result = Chats.delete_chats_by_user_id(id)
145

146
147
148
            if result:
                # Delete User
                query = User.delete().where(User.id == id)
Timothy J. Baek's avatar
Timothy J. Baek committed
149
                query.execute()  # Remove the rows, return number of rows removed.
150

151
152
153
                return True
            else:
                return False
154
155
156
        except:
            return False

157
158

Users = UsersTable(DB)