function export_analysis_content
Flask API endpoint that exports analysis session content for integration into documents, with authentication and authorization checks.
/tf/active/vicechatdev/vice_ai/new_app.py
6096 - 6116
moderate
Purpose
This endpoint retrieves and exports the content of a data analysis session for a specific user. It validates that the data analysis service is available, authenticates the user, verifies session ownership, and returns the session content in a format suitable for document integration. Used when users want to incorporate their analysis results into reports or documents.
Source Code
def export_analysis_content(session_id):
"""Export analysis session content for document integration"""
if not DATA_ANALYSIS_AVAILABLE:
return jsonify({'error': 'Data analysis service not available'}), 503
user_email = get_current_user()
session = data_analysis_service.get_analysis_session(session_id)
if not session or session.user_id != user_email:
return jsonify({'error': 'Session not found or access denied'}), 404
try:
content = data_analysis_service.get_session_content_for_export(session_id)
return jsonify({
'success': True,
'content': content
})
except Exception as e:
logger.error(f"Error exporting analysis content: {e}")
return jsonify({'error': str(e)}), 500
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
session_id |
- | - | positional_or_keyword |
Parameter Details
session_id: String identifier for the analysis session to export. This is extracted from the URL path parameter. Must correspond to an existing analysis session that belongs to the authenticated user.
Return Value
Returns a Flask JSON response. On success (200): {'success': True, 'content': <session_content>} where content is the formatted analysis session data. On service unavailable (503): {'error': 'Data analysis service not available'}. On not found/unauthorized (404): {'error': 'Session not found or access denied'}. On error (500): {'error': <error_message>}.
Dependencies
flasklogging
Required Imports
from flask import jsonify
import logging
Usage Example
# This is a Flask route handler, typically called via HTTP request
# Example HTTP request:
# GET /api/analysis-sessions/abc123-session-id/export
# Headers: Authorization: Bearer <token>
# In Flask application setup:
from flask import Flask, jsonify
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
DATA_ANALYSIS_AVAILABLE = True
# Assuming data_analysis_service and auth decorators are configured
@app.route('/api/analysis-sessions/<session_id>/export', methods=['GET'])
@require_auth
def export_analysis_content(session_id):
if not DATA_ANALYSIS_AVAILABLE:
return jsonify({'error': 'Data analysis service not available'}), 503
user_email = get_current_user()
session = data_analysis_service.get_analysis_session(session_id)
if not session or session.user_id != user_email:
return jsonify({'error': 'Session not found or access denied'}), 404
try:
content = data_analysis_service.get_session_content_for_export(session_id)
return jsonify({'success': True, 'content': content})
except Exception as e:
logger.error(f"Error exporting analysis content: {e}")
return jsonify({'error': str(e)}), 500
# Client usage example (JavaScript/Python requests):
# import requests
# response = requests.get(
# 'http://localhost:5000/api/analysis-sessions/abc123/export',
# headers={'Authorization': 'Bearer <token>'}
# )
# data = response.json()
# if data.get('success'):
# content = data['content']
Best Practices
- Always check DATA_ANALYSIS_AVAILABLE before attempting to use the data analysis service
- Verify both session existence and user ownership before allowing access to prevent unauthorized data access
- Use proper HTTP status codes: 503 for service unavailable, 404 for not found/unauthorized, 500 for server errors
- Log errors with sufficient context for debugging while avoiding sensitive data in logs
- The require_auth decorator must be properly configured to extract and validate user authentication
- Ensure data_analysis_service.get_session_content_for_export() returns content in a JSON-serializable format
- Consider implementing rate limiting for export endpoints to prevent abuse
- The session object must have a user_id attribute that matches the authenticated user's email
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_analysis_session 81.7% similar
-
function export_report 76.2% similar
-
function create_data_section_analysis_session 76.2% similar
-
function upload_analysis_dataset 76.0% similar
-
function save_data_section_analysis 75.4% similar