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

219 lines
5.7 KiB
Python
Raw Normal View History

2024-05-27 05:47:42 +00:00
from pydantic import BaseModel, ConfigDict
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
2024-05-27 05:47:42 +00:00
from apps.webui.internal.db import DB, JSONField
2024-05-26 08:15:48 +00:00
from apps.webui.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 = TextField()
2024-04-27 23:38:51 +00:00
last_active_at = BigIntegerField()
updated_at = BigIntegerField()
created_at = BigIntegerField()
2024-04-02 16:23:55 +00:00
api_key = CharField(null=True, unique=True)
2024-05-27 05:47:42 +00:00
settings = JSONField(null=True)
2023-12-26 05:44:28 +00:00
class Meta:
database = DB
2024-05-27 05:47:42 +00:00
class UserSettings(BaseModel):
ui: Optional[dict] = {}
model_config = ConfigDict(extra="allow")
pass
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
2024-04-27 23:38:51 +00:00
last_active_at: int # timestamp in epoch
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
2024-04-02 16:23:55 +00:00
api_key: Optional[str] = None
2024-05-27 05:47:42 +00:00
settings: Optional[UserSettings] = None
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(
2024-04-04 08:10:51 +00:00
self,
id: str,
name: str,
email: str,
2024-04-07 06:16:29 +00:00
profile_image_url: str = "/user.png",
2024-04-04 08:10:51 +00:00
role: str = "pending",
2024-01-06 04:59:56 +00:00
) -> Optional[UserModel]:
2023-11-19 00:47:12 +00:00
user = UserModel(
**{
"id": id,
"name": name,
"email": email,
"role": role,
"profile_image_url": profile_image_url,
2024-04-27 23:38:51 +00:00
"last_active_at": int(time.time()),
"created_at": int(time.time()),
"updated_at": 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
2024-04-02 16:23:55 +00:00
def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
try:
user = User.get(User.api_key == api_key)
return UserModel(**model_to_dict(user))
except:
return None
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
def get_first_user(self) -> UserModel:
try:
user = User.select().order_by(User.created_at).first()
return UserModel(**model_to_dict(user))
except:
return None
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-04-27 23:38:51 +00:00
def update_user_last_active_by_id(self, id: str) -> Optional[UserModel]:
try:
query = User.update(last_active_at=int(time.time())).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
2024-04-02 16:27:35 +00:00
def update_user_api_key_by_id(self, id: str, api_key: str) -> str:
try:
query = User.update(api_key=api_key).where(User.id == id)
result = query.execute()
return True if result == 1 else False
except:
return False
def get_user_api_key_by_id(self, id: str) -> Optional[str]:
try:
user = User.get(User.id == id)
return user.api_key
except:
return None
2023-11-19 00:47:12 +00:00
Users = UsersTable(DB)