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

154 lines
3.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.misc import get_gravatar_url
2023-12-26 05:44:28 +00:00
from apps.web.internal.db import DB
2023-12-29 07:24:51 +00:00
from apps.web.models.chats import Chats
2023-12-29 07:02:49 +00:00
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
2024-01-06 04:59:56 +00:00
class UserUpdateForm(BaseModel):
name: str
email: str
profile_image_url: str
password: Optional[str] = None
2024-01-06 04:59:56 +00:00
class UsersTable:
2023-11-19 00:47:12 +00:00
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
2024-01-06 04:59:56 +00:00
def insert_new_user(
self, id: str, name: str, email: str, role: str = "pending"
) -> Optional[UserModel]:
2023-11-19 00:47:12 +00:00
user = UserModel(
**{
"id": id,
"name": name,
"email": email,
"role": role,
"profile_image_url": "/user.png",
2023-12-26 05:44:28 +00:00
"timestamp": int(time.time()),
2024-01-06 04:59:56 +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
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))
2024-02-14 21:24:40 +00:00
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
2024-01-06 04:59:56 +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
2024-01-27 04:27:45 +00:00
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
2024-01-06 04:59:56 +00: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 07:02:49 +00:00
def delete_user_by_id(self, id: str) -> bool:
try:
# Delete User Chats
2023-12-29 07:24:51 +00:00
result = Chats.delete_chats_by_user_id(id)
2023-12-29 07:02:49 +00:00
2023-12-29 07:17:58 +00:00
if result:
# Delete User
query = User.delete().where(User.id == id)
2024-01-06 04:59:56 +00:00
query.execute() # Remove the rows, return number of rows removed.
2023-12-29 07:02:49 +00:00
2023-12-29 07:17:58 +00:00
return True
else:
return False
2023-12-29 07:02:49 +00:00
except:
return False
2023-11-19 00:47:12 +00:00
Users = UsersTable(DB)