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

View File

@@ -285,3 +285,131 @@ def execute_query():
except Exception as e:
return {"error": f"Query execution failed: {str(e)}"}, 500
@settings.route("/import", methods=["POST"])
@login_required
def import_data():
"""Import user data from JSON file"""
try:
# Check if file was uploaded
if 'import_file' not in request.files:
return {"error": "No file uploaded"}, 400
file = request.files['import_file']
if file.filename == '':
return {"error": "No file selected"}, 400
# Validate file type
if not file.filename.endswith('.json'):
return {"error": "File must be a JSON file"}, 400
# Read and parse JSON
try:
file_content = file.read()
# Check file size (max 10MB)
if len(file_content) > 10 * 1024 * 1024:
return {"error": "File too large (max 10MB)"}, 400
import_data = json.loads(file_content)
except json.JSONDecodeError as e:
return {"error": f"Invalid JSON format: {str(e)}"}, 400
# Validate structure
if not isinstance(import_data, dict):
return {"error": "Invalid data format: expected JSON object"}, 400
user_id = current_user.id
results = {
"http_functions": {"success": [], "skipped": [], "failed": []},
"timer_functions": {"success": [], "skipped": [], "failed": []},
"shared_environments": {"success": [], "skipped": [], "failed": []}
}
# Import HTTP Functions
http_functions = import_data.get('http_functions', [])
for func in http_functions:
# Map old export column names to new import method requirements
func_data = {
'name': func.get('name'),
'code': func.get('script_content'), # Export uses 'script_content'
'environment': func.get('environment_info'), # Export uses 'environment_info'
'runtime': func.get('runtime', 'python')
}
success, message, func_id = db.import_http_function(user_id, func_data)
if success:
results['http_functions']['success'].append(message)
elif 'already exists' in message:
results['http_functions']['skipped'].append(message)
else:
results['http_functions']['failed'].append(message)
# Import Timer Functions
timer_functions = import_data.get('timer_functions', [])
for func in timer_functions:
func_data = {
'name': func.get('name'),
'code': func.get('code'),
'environment': func.get('environment'),
'runtime': func.get('runtime', 'python'),
'trigger_type': func.get('trigger_type'),
'frequency_minutes': func.get('frequency_minutes'),
'run_date': func.get('run_date'),
'cron_expression': func.get('cron_expression'),
'enabled': func.get('enabled', True)
}
success, message, func_id = db.import_timer_function(user_id, func_data)
if success:
results['timer_functions']['success'].append(message)
elif 'already exists' in message:
results['timer_functions']['skipped'].append(message)
else:
results['timer_functions']['failed'].append(message)
# Import Shared Environments
shared_envs = import_data.get('shared_environments', [])
for env in shared_envs:
env_data = {
'name': env.get('name'),
'environment': env.get('environment')
}
success, message, env_id = db.import_shared_environment(user_id, env_data)
if success:
results['shared_environments']['success'].append(message)
elif 'already exists' in message:
results['shared_environments']['skipped'].append(message)
else:
results['shared_environments']['failed'].append(message)
# Calculate totals
total_success = (len(results['http_functions']['success']) +
len(results['timer_functions']['success']) +
len(results['shared_environments']['success']))
total_skipped = (len(results['http_functions']['skipped']) +
len(results['timer_functions']['skipped']) +
len(results['shared_environments']['skipped']))
total_failed = (len(results['http_functions']['failed']) +
len(results['timer_functions']['failed']) +
len(results['shared_environments']['failed']))
return {
"success": True,
"results": results,
"summary": {
"total_success": total_success,
"total_skipped": total_skipped,
"total_failed": total_failed
}
}
except Exception as e:
return {"error": f"Import failed: {str(e)}"}, 500