| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | import time | 
					
						
							|  |  |  | from typing import Optional | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-10 16:54:13 +08:00
										 |  |  | from open_webui.internal.db import Base, JSONField, get_db | 
					
						
							|  |  |  | from open_webui.models.users import Users | 
					
						
							| 
									
										
										
										
											2024-09-04 22:54:48 +08:00
										 |  |  | from open_webui.env import SRC_LOG_LEVELS | 
					
						
							| 
									
										
										
										
											2024-08-28 06:10:27 +08:00
										 |  |  | from pydantic import BaseModel, ConfigDict | 
					
						
							|  |  |  | from sqlalchemy import BigInteger, Boolean, Column, String, Text | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | log = logging.getLogger(__name__) | 
					
						
							|  |  |  | log.setLevel(SRC_LOG_LEVELS["MODELS"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #################### | 
					
						
							|  |  |  | # Functions DB Schema | 
					
						
							|  |  |  | #################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  | class Function(Base): | 
					
						
							|  |  |  |     __tablename__ = "function" | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  |     id = Column(String, primary_key=True) | 
					
						
							|  |  |  |     user_id = Column(String) | 
					
						
							|  |  |  |     name = Column(Text) | 
					
						
							|  |  |  |     type = Column(Text) | 
					
						
							|  |  |  |     content = Column(Text) | 
					
						
							|  |  |  |     meta = Column(JSONField) | 
					
						
							|  |  |  |     valves = Column(JSONField) | 
					
						
							|  |  |  |     is_active = Column(Boolean) | 
					
						
							| 
									
										
										
										
											2024-06-28 15:19:56 +08:00
										 |  |  |     is_global = Column(Boolean) | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  |     updated_at = Column(BigInteger) | 
					
						
							|  |  |  |     created_at = Column(BigInteger) | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunctionMeta(BaseModel): | 
					
						
							|  |  |  |     description: Optional[str] = None | 
					
						
							| 
									
										
										
										
											2024-06-24 11:31:40 +08:00
										 |  |  |     manifest: Optional[dict] = {} | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunctionModel(BaseModel): | 
					
						
							|  |  |  |     id: str | 
					
						
							|  |  |  |     user_id: str | 
					
						
							|  |  |  |     name: str | 
					
						
							|  |  |  |     type: str | 
					
						
							|  |  |  |     content: str | 
					
						
							|  |  |  |     meta: FunctionMeta | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |     is_active: bool = False | 
					
						
							| 
									
										
										
										
											2024-06-28 04:04:12 +08:00
										 |  |  |     is_global: bool = False | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     updated_at: int  # timestamp in epoch | 
					
						
							|  |  |  |     created_at: int  # timestamp in epoch | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  |     model_config = ConfigDict(from_attributes=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #################### | 
					
						
							|  |  |  | # Forms | 
					
						
							|  |  |  | #################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunctionResponse(BaseModel): | 
					
						
							|  |  |  |     id: str | 
					
						
							|  |  |  |     user_id: str | 
					
						
							| 
									
										
										
										
											2024-06-20 16:16:31 +08:00
										 |  |  |     type: str | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     name: str | 
					
						
							|  |  |  |     meta: FunctionMeta | 
					
						
							| 
									
										
										
										
											2024-06-24 09:05:33 +08:00
										 |  |  |     is_active: bool | 
					
						
							| 
									
										
										
										
											2024-06-28 04:04:12 +08:00
										 |  |  |     is_global: bool | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     updated_at: int  # timestamp in epoch | 
					
						
							|  |  |  |     created_at: int  # timestamp in epoch | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FunctionForm(BaseModel): | 
					
						
							|  |  |  |     id: str | 
					
						
							|  |  |  |     name: str | 
					
						
							|  |  |  |     content: str | 
					
						
							|  |  |  |     meta: FunctionMeta | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 09:05:33 +08:00
										 |  |  | class FunctionValves(BaseModel): | 
					
						
							|  |  |  |     valves: Optional[dict] = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-20 15:37:02 +08:00
										 |  |  | class FunctionsTable: | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     def insert_new_function( | 
					
						
							| 
									
										
										
										
											2024-06-20 15:54:58 +08:00
										 |  |  |         self, user_id: str, type: str, form_data: FunctionForm | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     ) -> Optional[FunctionModel]: | 
					
						
							|  |  |  |         function = FunctionModel( | 
					
						
							|  |  |  |             **{ | 
					
						
							|  |  |  |                 **form_data.model_dump(), | 
					
						
							|  |  |  |                 "user_id": user_id, | 
					
						
							| 
									
										
										
										
											2024-06-20 15:54:58 +08:00
										 |  |  |                 "type": type, | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |                 "updated_at": int(time.time()), | 
					
						
							|  |  |  |                 "created_at": int(time.time()), | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |             with get_db() as db: | 
					
						
							|  |  |  |                 result = Function(**function.model_dump()) | 
					
						
							|  |  |  |                 db.add(result) | 
					
						
							|  |  |  |                 db.commit() | 
					
						
							|  |  |  |                 db.refresh(result) | 
					
						
							|  |  |  |                 if result: | 
					
						
							|  |  |  |                     return FunctionModel.model_validate(result) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     return None | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |             log.exception(f"Error creating a new function: {e}") | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |             return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-25 03:39:19 +08:00
										 |  |  |     def sync_functions( | 
					
						
							|  |  |  |         self, user_id: str, functions: list[FunctionModel] | 
					
						
							|  |  |  |     ) -> list[FunctionModel]: | 
					
						
							|  |  |  |         # Synchronize functions for a user by updating existing ones, inserting new ones, and removing those that are no longer present. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with get_db() as db: | 
					
						
							|  |  |  |                 # Get existing functions | 
					
						
							|  |  |  |                 existing_functions = db.query(Function).all() | 
					
						
							|  |  |  |                 existing_ids = {func.id for func in existing_functions} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # Prepare a set of new function IDs | 
					
						
							|  |  |  |                 new_function_ids = {func.id for func in functions} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # Update or insert functions | 
					
						
							|  |  |  |                 for func in functions: | 
					
						
							|  |  |  |                     if func.id in existing_ids: | 
					
						
							|  |  |  |                         db.query(Function).filter_by(id=func.id).update( | 
					
						
							|  |  |  |                             { | 
					
						
							|  |  |  |                                 **func.model_dump(), | 
					
						
							|  |  |  |                                 "user_id": user_id, | 
					
						
							|  |  |  |                                 "updated_at": int(time.time()), | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         new_func = Function( | 
					
						
							|  |  |  |                             **{ | 
					
						
							|  |  |  |                                 **func.model_dump(), | 
					
						
							|  |  |  |                                 "user_id": user_id, | 
					
						
							|  |  |  |                                 "updated_at": int(time.time()), | 
					
						
							|  |  |  |                             } | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                         db.add(new_func) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 # Remove functions that are no longer present | 
					
						
							|  |  |  |                 for func in existing_functions: | 
					
						
							|  |  |  |                     if func.id not in new_function_ids: | 
					
						
							|  |  |  |                         db.delete(func) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 db.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return [ | 
					
						
							|  |  |  |                     FunctionModel.model_validate(func) | 
					
						
							|  |  |  |                     for func in db.query(Function).all() | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             log.exception(f"Error syncing functions for user {user_id}: {e}") | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     def get_function_by_id(self, id: str) -> Optional[FunctionModel]: | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |             with get_db() as db: | 
					
						
							|  |  |  |                 function = db.get(Function, id) | 
					
						
							|  |  |  |                 return FunctionModel.model_validate(function) | 
					
						
							| 
									
										
										
										
											2024-08-14 20:38:19 +08:00
										 |  |  |         except Exception: | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |             return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  |     def get_functions(self, active_only=False) -> list[FunctionModel]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             if active_only: | 
					
						
							|  |  |  |                 return [ | 
					
						
							|  |  |  |                     FunctionModel.model_validate(function) | 
					
						
							|  |  |  |                     for function in db.query(Function).filter_by(is_active=True).all() | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return [ | 
					
						
							|  |  |  |                     FunctionModel.model_validate(function) | 
					
						
							|  |  |  |                     for function in db.query(Function).all() | 
					
						
							|  |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_functions_by_type( | 
					
						
							|  |  |  |         self, type: str, active_only=False | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  |     ) -> list[FunctionModel]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             if active_only: | 
					
						
							|  |  |  |                 return [ | 
					
						
							|  |  |  |                     FunctionModel.model_validate(function) | 
					
						
							|  |  |  |                     for function in db.query(Function) | 
					
						
							|  |  |  |                     .filter_by(type=type, is_active=True) | 
					
						
							|  |  |  |                     .all() | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 return [ | 
					
						
							|  |  |  |                     FunctionModel.model_validate(function) | 
					
						
							|  |  |  |                     for function in db.query(Function).filter_by(type=type).all() | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  |     def get_global_filter_functions(self) -> list[FunctionModel]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |             return [ | 
					
						
							| 
									
										
										
										
											2024-07-03 12:56:32 +08:00
										 |  |  |                 FunctionModel.model_validate(function) | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 for function in db.query(Function) | 
					
						
							|  |  |  |                 .filter_by(type="filter", is_active=True, is_global=True) | 
					
						
							| 
									
										
										
										
											2024-06-24 19:06:15 +08:00
										 |  |  |                 .all() | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  |             ] | 
					
						
							| 
									
										
										
										
											2024-06-28 04:04:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 20:46:31 +08:00
										 |  |  |     def get_global_action_functions(self) -> list[FunctionModel]: | 
					
						
							| 
									
										
										
										
											2024-07-12 09:41:00 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             return [ | 
					
						
							|  |  |  |                 FunctionModel.model_validate(function) | 
					
						
							|  |  |  |                 for function in db.query(Function) | 
					
						
							|  |  |  |                 .filter_by(type="action", is_active=True, is_global=True) | 
					
						
							|  |  |  |                 .all() | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 10:18:13 +08:00
										 |  |  |     def get_function_valves_by_id(self, id: str) -> Optional[dict]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 function = db.get(Function, id) | 
					
						
							|  |  |  |                 return function.valves if function.valves else {} | 
					
						
							|  |  |  |             except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-25 22:36:25 +08:00
										 |  |  |                 log.exception(f"Error getting function valves by id {id}: {e}") | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2024-06-24 09:34:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def update_function_valves_by_id( | 
					
						
							|  |  |  |         self, id: str, valves: dict | 
					
						
							|  |  |  |     ) -> Optional[FunctionValves]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 function = db.get(Function, id) | 
					
						
							|  |  |  |                 function.valves = valves | 
					
						
							|  |  |  |                 function.updated_at = int(time.time()) | 
					
						
							|  |  |  |                 db.commit() | 
					
						
							|  |  |  |                 db.refresh(function) | 
					
						
							|  |  |  |                 return self.get_function_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-08-14 20:38:19 +08:00
										 |  |  |             except Exception: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2024-07-04 15:37:05 +08:00
										 |  |  |             user_settings = user.settings.model_dump() if user.settings else {} | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Check if user has "functions" and "valves" settings | 
					
						
							| 
									
										
										
										
											2024-06-23 03:08:32 +08:00
										 |  |  |             if "functions" not in user_settings: | 
					
						
							|  |  |  |                 user_settings["functions"] = {} | 
					
						
							|  |  |  |             if "valves" not in user_settings["functions"]: | 
					
						
							|  |  |  |                 user_settings["functions"]["valves"] = {} | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 03:08:32 +08:00
										 |  |  |             return user_settings["functions"]["valves"].get(id, {}) | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-27 14:18:18 +08:00
										 |  |  |             log.exception( | 
					
						
							|  |  |  |                 f"Error getting user values by id {id} and user id {user_id}: {e}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |             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) | 
					
						
							| 
									
										
										
										
											2024-07-04 15:37:05 +08:00
										 |  |  |             user_settings = user.settings.model_dump() if user.settings else {} | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Check if user has "functions" and "valves" settings | 
					
						
							| 
									
										
										
										
											2024-06-23 03:08:32 +08:00
										 |  |  |             if "functions" not in user_settings: | 
					
						
							|  |  |  |                 user_settings["functions"] = {} | 
					
						
							|  |  |  |             if "valves" not in user_settings["functions"]: | 
					
						
							|  |  |  |                 user_settings["functions"]["valves"] = {} | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 03:08:32 +08:00
										 |  |  |             user_settings["functions"]["valves"][id] = valves | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Update the user settings in the database | 
					
						
							| 
									
										
										
										
											2024-07-02 07:11:44 +08:00
										 |  |  |             Users.update_user_by_id(user_id, {"settings": user_settings}) | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 03:08:32 +08:00
										 |  |  |             return user_settings["functions"]["valves"][id] | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2025-02-27 14:18:18 +08:00
										 |  |  |             log.exception( | 
					
						
							|  |  |  |                 f"Error updating user valves by id {id} and user_id {user_id}: {e}" | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2024-06-23 02:26:33 +08:00
										 |  |  |             return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     def update_function_by_id(self, id: str, updated: dict) -> Optional[FunctionModel]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 db.query(Function).filter_by(id=id).update( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         **updated, | 
					
						
							|  |  |  |                         "updated_at": int(time.time()), | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 db.commit() | 
					
						
							|  |  |  |                 return self.get_function_by_id(id) | 
					
						
							| 
									
										
										
										
											2024-08-14 20:38:19 +08:00
										 |  |  |             except Exception: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-24 10:28:33 +08:00
										 |  |  |     def deactivate_all_functions(self) -> Optional[bool]: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 db.query(Function).update( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         "is_active": False, | 
					
						
							|  |  |  |                         "updated_at": int(time.time()), | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |                 db.commit() | 
					
						
							|  |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2024-08-14 20:38:19 +08:00
										 |  |  |             except Exception: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return None | 
					
						
							| 
									
										
										
										
											2024-06-24 10:28:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  |     def delete_function_by_id(self, id: str) -> bool: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |         with get_db() as db: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 db.query(Function).filter_by(id=id).delete() | 
					
						
							| 
									
										
										
										
											2024-07-06 23:10:58 +08:00
										 |  |  |                 db.commit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return True | 
					
						
							| 
									
										
										
										
											2024-08-14 20:38:19 +08:00
										 |  |  |             except Exception: | 
					
						
							| 
									
										
										
										
											2024-07-04 14:32:39 +08:00
										 |  |  |                 return False | 
					
						
							| 
									
										
										
										
											2024-06-19 01:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-18 21:03:31 +08:00
										 |  |  | Functions = FunctionsTable() |