open-webui/backend/apps/web/models/users.py

115 lines
2.8 KiB
Python
Raw Normal View History

2023-11-19 00:47:12 +00:00
from pydantic import BaseModel
2023-12-26 05:44:28 +00:00
from peewee import *
from playhouse.shortcuts import model_to_dict
2023-11-19 00:47:12 +00:00
from typing import List, Union, Optional
import time
2023-11-19 05:41:43 +00:00
from utils.utils import decode_token
from utils.misc import get_gravatar_url
2023-12-26 05:44:28 +00:00
from apps.web.internal.db import DB
2023-11-19 00:47:12 +00:00
####################
# User DB Schema
####################
2023-12-26 05:44:28 +00:00
class User(Model):
id = CharField(unique=True)
name = CharField()
email = CharField()
role = CharField()
profile_image_url = CharField()
timestamp = DateField()
class Meta:
database = DB
2023-11-19 00:47:12 +00:00
class UserModel(BaseModel):
id: str
name: str
email: str
2023-11-19 05:41:43 +00:00
role: str = "pending"
profile_image_url: str = "/user.png"
2023-12-26 05:44:28 +00:00
timestamp: int # timestamp in epoch
2023-11-19 00:47:12 +00:00
####################
# Forms
####################
2023-11-19 08:13:59 +00:00
class UserRoleUpdateForm(BaseModel):
id: str
role: str
2023-11-19 00:47:12 +00:00
class UsersTable:
def __init__(self, db):
self.db = db
2023-12-26 05:44:28 +00:00
self.db.create_tables([User])
2023-11-19 00:47:12 +00:00
def insert_new_user(
2023-11-19 05:41:43 +00:00
self, id: str, name: str, email: str, role: str = "pending"
2023-11-19 00:47:12 +00:00
) -> Optional[UserModel]:
user = UserModel(
**{
"id": id,
"name": name,
"email": email,
"role": role,
2023-11-19 05:41:43 +00:00
"profile_image_url": get_gravatar_url(email),
2023-12-26 05:44:28 +00:00
"timestamp": int(time.time()),
2023-11-19 00:47:12 +00:00
}
)
2023-12-26 05:44:28 +00:00
result = User.create(**user.model_dump())
2023-11-19 00:47:12 +00:00
if result:
return user
else:
return None
2023-12-26 05:44:28 +00:00
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
2023-11-19 00:47:12 +00:00
2023-12-26 05:44:28 +00:00
def get_user_by_email(self, email: str) -> Optional[UserModel]:
try:
user = User.get(User.email == email)
return UserModel(**model_to_dict(user))
except:
2023-11-19 00:47:12 +00:00
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
2023-11-21 17:39:53 +00:00
def get_users(self, skip: int = 0, limit: int = 50) -> List[UserModel]:
2023-11-19 00:47:12 +00:00
return [
2023-12-26 05:44:28 +00:00
UserModel(**model_to_dict(user))
for user in User.select().limit(limit).offset(skip)
2023-11-19 00:47:12 +00:00
]
def get_num_users(self) -> Optional[int]:
2023-12-26 05:44:28 +00:00
return User.select().count()
2023-11-19 08:13:59 +00:00
def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
2023-12-26 05:44:28 +00:00
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
2023-11-19 08:13:59 +00:00
2023-11-19 00:47:12 +00:00
Users = UsersTable(DB)