users.py 2.76 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
6
from typing import List, Union, Optional
import time

Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9
from utils.utils import decode_token
from utils.misc import get_gravatar_url

Timothy J. Baek's avatar
Timothy J. Baek committed
10
from apps.web.internal.db import DB
11
12
13
14
15
16

####################
# User DB Schema
####################


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

    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
37
38
39
40
41
42


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


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


48
49
50
class UsersTable:
    def __init__(self, db):
        self.db = db
Timothy J. Baek's avatar
Timothy J. Baek committed
51
        self.db.create_tables([User])
52
53

    def insert_new_user(
Timothy J. Baek's avatar
Timothy J. Baek committed
54
        self, id: str, name: str, email: str, role: str = "pending"
55
56
57
58
59
60
61
    ) -> Optional[UserModel]:
        user = UserModel(
            **{
                "id": id,
                "name": name,
                "email": email,
                "role": role,
Timothy J. Baek's avatar
Timothy J. Baek committed
62
                "profile_image_url": get_gravatar_url(email),
Timothy J. Baek's avatar
Timothy J. Baek committed
63
                "timestamp": int(time.time()),
64
65
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
66
        result = User.create(**user.model_dump())
67
68
69
70
71
        if result:
            return user
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
72
73
74
75
76
77
    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
78

Timothy J. Baek's avatar
Timothy J. Baek committed
79
80
81
82
83
    def get_user_by_email(self, email: str) -> Optional[UserModel]:
        try:
            user = User.get(User.email == email)
            return UserModel(**model_to_dict(user))
        except:
84
85
86
87
88
89
90
91
92
93
            return None

    def get_user_by_token(self, token: str) -> Optional[UserModel]:
        data = decode_token(token)

        if data != None and "email" in data:
            return self.get_user_by_email(data["email"])
        else:
            return None

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

100
    def get_num_users(self) -> Optional[int]:
Timothy J. Baek's avatar
Timothy J. Baek committed
101
        return User.select().count()
Timothy J. Baek's avatar
Timothy J. Baek committed
102
103

    def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
106
107
108
109
110
111
        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
112

113
114

Users = UsersTable(DB)