users.py 2.48 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
9
10
11
12
13
14

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


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

    class Meta:
        database = DB


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


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


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


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

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
80
81
    def get_user_by_email(self, email: str) -> Optional[UserModel]:
        try:
            user = User.get(User.email == email)
            return UserModel(**model_to_dict(user))
        except:
82
83
            return None

84
    def get_users(self, skip: int = 0, limit: int = 50) -> List[UserModel]:
85
        return [
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
            UserModel(**model_to_dict(user))
            for user in User.select().limit(limit).offset(skip)
88
89
        ]

90
    def get_num_users(self) -> Optional[int]:
Timothy J. Baek's avatar
Timothy J. Baek committed
91
        return User.select().count()
Timothy J. Baek's avatar
Timothy J. Baek committed
92
93

    def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
94
95
96
97
98
99
100
101
        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
102

103
104

Users = UsersTable(DB)