Add functionality in settings to import data

This commit is contained in:
Peter Stockings
2025-12-02 15:19:20 +11:00
parent 290b141d32
commit ab7079f87e
4 changed files with 406 additions and 128 deletions

122
db.py
View File

@@ -427,4 +427,124 @@ ORDER BY invocation_time DESC""", [http_function_id])
link['created_at'] = link['created_at'].isoformat()
export_data['timer_function_shared_env_links'] = timer_shared_env_links or []
return export_data
return export_data
def import_http_function(self, user_id, func_data):
"""Import a single HTTP function, returns (success, message, function_id)"""
try:
# Check if function with same name exists
existing = self.execute(
"SELECT id FROM http_functions WHERE user_id = %s AND name = %s",
(user_id, func_data['name']),
one=True
)
if existing:
return (False, f"Function '{func_data['name']}' already exists", None)
# Insert the function
result = self.execute(
"""INSERT INTO http_functions
(name, code, environment, version_number, user_id, runtime)
VALUES (%s, %s, %s, %s, %s, %s)
RETURNING id""",
(
func_data['name'],
func_data['code'],
json.dumps(func_data.get('environment', {})),
1, # Start at version 1
user_id,
func_data.get('runtime', 'python')
),
one=True,
commit=True
)
return (True, f"Imported function '{func_data['name']}'", result['id'])
except Exception as e:
return (False, f"Error importing '{func_data.get('name', 'unknown')}': {str(e)}", None)
def import_timer_function(self,user_id, func_data):
"""Import a single timer function, returns (success, message, function_id)"""
try:
# Check if function with same name exists
existing = self.execute(
"SELECT id FROM timer_functions WHERE user_id = %s AND name = %s",
(user_id, func_data['name']),
one=True
)
if existing:
return (False, f"Timer function '{func_data['name']}' already exists", None)
# Calculate next_run based on trigger type
from routes.timer import calculate_next_run
next_run = calculate_next_run(
func_data['trigger_type'],
func_data.get('frequency_minutes'),
func_data.get('run_date'),
func_data.get('cron_expression')
)
# Insert the function
result = self.execute(
"""INSERT INTO timer_functions
(name, code, environment, version_number, user_id, runtime,
trigger_type, frequency_minutes, run_date, cron_expression,
next_run, enabled)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING id""",
(
func_data['name'],
func_data['code'],
json.dumps(func_data.get('environment', {})),
1, # Start at version 1
user_id,
func_data.get('runtime', 'python'),
func_data['trigger_type'],
func_data.get('frequency_minutes'),
func_data.get('run_date'),
func_data.get('cron_expression'),
next_run,
func_data.get('enabled', True)
),
one=True,
commit=True
)
return (True, f"Imported timer function '{func_data['name']}'", result['id'])
except Exception as e:
return (False, f"Error importing timer '{func_data.get('name', 'unknown')}': {str(e)}", None)
def import_shared_environment(self, user_id, env_data):
"""Import a single shared environment, returns (success, message, env_id)"""
try:
# Check if environment with same name exists
existing = self.execute(
"SELECT id FROM shared_environments WHERE user_id = %s AND name = %s",
(user_id, env_data['name']),
one=True
)
if existing:
return (False, f"Shared environment '{env_data['name']}' already exists", None)
# Insert the environment
result = self.execute(
"""INSERT INTO shared_environments
(name, environment, user_id, version_number)
VALUES (%s, %s, %s, %s)
RETURNING id""",
(
env_data['name'],
json.dumps(env_data.get('environment', {})),
user_id,
1 # Start at version 1
),
one=True,
commit=True
)
return (True, f"Imported shared environment '{env_data['name']}'", result['id'])
except Exception as e:
return (False, f"Error importing environment '{env_data.get('name', 'unknown')}': {str(e)}", None)