function create_sample_data
Flask API endpoint that creates sample data for testing purposes by invoking the LegacySystemMigrator's create_sample_data method and returns the generated data as JSON.
/tf/active/vicechatdev/vice_ai/new_app.py
1812 - 1822
simple
Purpose
This endpoint is designed to populate a database with sample/test data during development or testing phases. It's protected by authentication and provides a convenient way to initialize the system with mock data without manual data entry. The endpoint is typically used during setup or when resetting a test environment.
Source Code
def create_sample_data():
"""Create sample data for testing"""
try:
migrator = LegacySystemMigrator('.', DATABASE_PATH)
sample_data = migrator.create_sample_data()
return jsonify(sample_data)
except Exception as e:
logger.error(f"Sample data creation error: {e}")
return jsonify({'error': str(e)}), 500
Return Value
Returns a Flask JSON response. On success, returns the sample data dictionary created by the migrator (status code 200). On failure, returns a JSON object with an 'error' key containing the error message string and HTTP status code 500.
Dependencies
flaskloggingmigration
Required Imports
from flask import jsonify
import logging
from migration import LegacySystemMigrator
Usage Example
# This is a Flask route handler, typically called via HTTP request
# Example HTTP request:
# POST /api/setup/sample-data
# Headers: Authorization: Bearer <token>
# Example programmatic usage (within Flask app context):
from flask import Flask
from migration import LegacySystemMigrator
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
DATABASE_PATH = './test_database.db'
@app.route('/api/setup/sample-data', methods=['POST'])
def create_sample_data():
try:
migrator = LegacySystemMigrator('.', DATABASE_PATH)
sample_data = migrator.create_sample_data()
return jsonify(sample_data)
except Exception as e:
logger.error(f"Sample data creation error: {e}")
return jsonify({'error': str(e)}), 500
# Client-side usage:
# import requests
# response = requests.post('http://localhost:5000/api/setup/sample-data',
# headers={'Authorization': 'Bearer <token>'})
# data = response.json()
Best Practices
- This endpoint should only be used in development/testing environments, never in production
- Ensure proper authentication is in place via the require_auth decorator to prevent unauthorized data creation
- The DATABASE_PATH should point to a test database, not production data
- Consider adding additional validation to prevent accidental execution in production environments
- The endpoint returns all sample data in the response, which could be large - consider pagination or limiting response size
- Error messages are logged and returned to the client - ensure sensitive information is not exposed in error messages
- Consider adding idempotency checks to prevent duplicate sample data creation
- The function catches all exceptions broadly - consider more specific exception handling for better error diagnosis
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function migrate_from_legacy 70.9% similar
-
function create_data_section 64.5% similar
-
function upload_data 56.9% similar
-
function create_data_section_analysis_session 54.6% similar
-
function load_sql_data 54.6% similar