"vscode:/vscode.git/clone" did not exist on "2c9f9c4832058e95efa601862b753088603af5a2"
users.py 4.6 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
class User(Model):
    id = CharField(unique=True)
    name = CharField()
    email = CharField()
    role = CharField()
    profile_image_url = CharField()
    timestamp = DateField()
23
    api_key = CharField(null=True, unique=True)
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28

    class Meta:
        database = DB


29
30
31
32
class UserModel(BaseModel):
    id: str
    name: str
    email: str
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
    role: str = "pending"
    profile_image_url: str = "/user.png"
Timothy J. Baek's avatar
Timothy J. Baek committed
35
    timestamp: int  # timestamp in epoch
36
    api_key: Optional[str] = None
37
38
39
40
41
42
43


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


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


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

55

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
83
84
85
    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
86

87
88
89
90
91
92
93
    def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
        try:
            user = User.get(User.api_key == api_key)
            return UserModel(**model_to_dict(user))
        except:
            return None

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

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
111
    def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
112
113
114
115
116
117
118
119
        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
120

121
122
123
124
125
126
127
128
129
130
131
132
133
134
    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
135
136
137
138
139
140
141
142
143
144
    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

145
146
147
    def delete_user_by_id(self, id: str) -> bool:
        try:
            # Delete User Chats
Timothy J. Baek's avatar
Timothy J. Baek committed
148
            result = Chats.delete_chats_by_user_id(id)
149

150
151
152
            if result:
                # Delete User
                query = User.delete().where(User.id == id)
Timothy J. Baek's avatar
Timothy J. Baek committed
153
                query.execute()  # Remove the rows, return number of rows removed.
154

155
156
157
                return True
            else:
                return False
158
159
160
        except:
            return False

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    def update_user_api_key_by_id(self, id: str, api_key: str) -> str:
        try:
            query = User.update(api_key=api_key).where(User.id == id)
            result = query.execute()

            return True if result == 1 else False
        except:
            return False

    def get_user_api_key_by_id(self, id: str) -> Optional[str]:
        try:
            user = User.get(User.id == id)
            return user.api_key
        except:
            return None

177
178

Users = UsersTable(DB)