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

139 lines
3.4 KiB
Python
Raw Normal View History

2023-11-19 01:47:12 +01:00
from pydantic import BaseModel
2023-12-26 06:44:28 +01:00
from peewee import *
from playhouse.shortcuts import model_to_dict
2023-11-19 01:47:12 +01:00
from typing import List, Union, Optional
import time
2023-11-19 06:41:43 +01:00
from utils.misc import get_gravatar_url
2023-12-26 06:44:28 +01:00
from apps.web.internal.db import DB
2023-12-29 08:24:51 +01:00
from apps.web.models.chats import Chats
2023-12-29 08:02:49 +01:00
2023-11-19 01:47:12 +01:00
####################
# User DB Schema
####################
2023-12-26 06:44:28 +01: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 01:47:12 +01:00
class UserModel(BaseModel):
id: str
name: str
email: str
2023-11-19 06:41:43 +01:00
role: str = "pending"
profile_image_url: str = "/user.png"
2023-12-26 06:44:28 +01:00
timestamp: int # timestamp in epoch
2023-11-19 01:47:12 +01:00
####################
# Forms
####################
2023-11-19 09:13:59 +01:00
class UserRoleUpdateForm(BaseModel):
id: str
role: str
2024-01-06 05:59:56 +01:00
class UserUpdateForm(BaseModel):
name: str
email: str
profile_image_url: str
password: Optional[str] = None
2024-01-06 05:59:56 +01:00
class UsersTable:
2023-11-19 01:47:12 +01:00
def __init__(self, db):
self.db = db
2023-12-26 06:44:28 +01:00
self.db.create_tables([User])
2023-11-19 01:47:12 +01:00
2024-01-06 05:59:56 +01:00
def insert_new_user(
self, id: str, name: str, email: str, role: str = "pending"
) -> Optional[UserModel]:
2023-11-19 01:47:12 +01:00
user = UserModel(
**{
"id": id,
"name": name,
"email": email,
"role": role,
2023-11-19 06:41:43 +01:00
"profile_image_url": get_gravatar_url(email),
2023-12-26 06:44:28 +01:00
"timestamp": int(time.time()),
2024-01-06 05:59:56 +01:00
}
)
2023-12-26 06:44:28 +01:00
result = User.create(**user.model_dump())
2023-11-19 01:47:12 +01:00
if result:
return user
else:
return None
2023-12-26 06:44:28 +01: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 01:47:12 +01:00
2023-12-26 06:44:28 +01: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 01:47:12 +01:00
return None
2023-11-21 18:39:53 +01:00
def get_users(self, skip: int = 0, limit: int = 50) -> List[UserModel]:
2023-11-19 01:47:12 +01:00
return [
2023-12-26 06:44:28 +01:00
UserModel(**model_to_dict(user))
for user in User.select().limit(limit).offset(skip)
2023-11-19 01:47:12 +01:00
]
def get_num_users(self) -> Optional[int]:
2023-12-26 06:44:28 +01:00
return User.select().count()
2023-11-19 09:13:59 +01:00
2024-01-06 05:59:56 +01:00
def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
2023-12-26 06:44:28 +01: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 09:13:59 +01:00
2024-01-06 05:59:56 +01:00
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
2023-12-29 08:02:49 +01:00
def delete_user_by_id(self, id: str) -> bool:
try:
# Delete User Chats
2023-12-29 08:24:51 +01:00
result = Chats.delete_chats_by_user_id(id)
2023-12-29 08:02:49 +01:00
2023-12-29 08:17:58 +01:00
if result:
# Delete User
query = User.delete().where(User.id == id)
2024-01-06 05:59:56 +01:00
query.execute() # Remove the rows, return number of rows removed.
2023-12-29 08:02:49 +01:00
2023-12-29 08:17:58 +01:00
return True
else:
return False
2023-12-29 08:02:49 +01:00
except:
return False
2023-11-19 01:47:12 +01:00
Users = UsersTable(DB)