feat: user valves endpoints

This commit is contained in:
Timothy J. Baek
2024-06-22 11:26:33 -07:00
parent 8345bb55d4
commit 15fc23df87
6 changed files with 304 additions and 25 deletions

View File

@@ -5,6 +5,7 @@ from typing import List, Union, Optional
import time
import logging
from apps.webui.internal.db import DB, JSONField
from apps.webui.models.users import Users
import json
@@ -115,6 +116,46 @@ class FunctionsTable:
for function in Function.select().where(Function.type == type)
]
def get_user_valves_by_id_and_user_id(
self, id: str, user_id: str
) -> Optional[dict]:
try:
user = Users.get_user_by_id(user_id)
# Check if user has "functions" and "valves" settings
if "functions" not in user.settings:
user.settings["functions"] = {}
if "valves" not in user.settings["functions"]:
user.settings["functions"]["valves"] = {}
return user.settings["functions"]["valves"].get(id, {})
except Exception as e:
print(f"An error occurred: {e}")
return None
def update_user_valves_by_id_and_user_id(
self, id: str, user_id: str, valves: dict
) -> Optional[dict]:
try:
user = Users.get_user_by_id(user_id)
# Check if user has "functions" and "valves" settings
if "functions" not in user.settings:
user.settings["functions"] = {}
if "valves" not in user.settings["functions"]:
user.settings["functions"]["valves"] = {}
user.settings["functions"]["valves"][id] = valves
# Update the user settings in the database
query = Users.update_user_by_id(user_id, {"settings": user.settings})
query.execute()
return user.settings["functions"]["valves"][id]
except Exception as e:
print(f"An error occurred: {e}")
return None
def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]:
try:
query = Function.update(