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

211 lines
5.5 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 = TextField()
2024-04-28 01:38:51 +02:00
last_active_at = BigIntegerField()
updated_at = BigIntegerField()
created_at = BigIntegerField()
2024-04-02 18:23:55 +02:00
api_key = CharField(null=True, unique=True)
2023-12-26 06:44:28 +01:00
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
2024-04-28 01:38:51 +02:00
last_active_at: int # timestamp in epoch
updated_at: int # timestamp in epoch
created_at: int # timestamp in epoch
2024-04-02 18:23:55 +02:00
api_key: Optional[str] = None
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(
2024-04-04 10:10:51 +02:00
self,
id: str,
name: str,
email: str,
2024-04-07 08:16:29 +02:00
profile_image_url: str = "/user.png",
2024-04-04 10:10:51 +02:00
role: str = "pending",
2024-01-06 05:59:56 +01:00
) -> Optional[UserModel]:
2023-11-19 01:47:12 +01:00
user = UserModel(
**{
"id": id,
"name": name,
"email": email,
"role": role,
"profile_image_url": profile_image_url,
2024-04-28 01:38:51 +02:00
"last_active_at": int(time.time()),
"created_at": int(time.time()),
"updated_at": 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
2024-04-02 18:23:55 +02: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 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))
2024-02-14 22:24:40 +01:00
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
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 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-27 05:27:45 +01: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-28 01:38:51 +02: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 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
2024-04-02 18:27:35 +02: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 01:47:12 +01:00
Users = UsersTable(DB)