🔍 Code Extractor

function export_analysis_content

Maturity: 47

Flask API endpoint that exports analysis session content for integration into documents, with authentication and authorization checks.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
6096 - 6116
Complexity:
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

  • flask
  • logging

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_analysis_session 81.7% similar

    Flask API endpoint that retrieves details of a specific data analysis session for an authenticated user, ensuring the user has access to the requested session.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function export_report 76.2% similar

    Flask route handler that exports an analysis report for a given session in either PDF or Word format, retrieving session data and generating a downloadable file.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function create_data_section_analysis_session 76.2% similar

    Flask API endpoint that creates or retrieves an analysis session for a specific data section, ensuring user ownership and linking the session to the data section.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function upload_analysis_dataset 76.0% similar

    Flask API endpoint that handles file upload for data analysis sessions, accepting CSV and Excel files, validating user access, and processing the dataset through a data analysis service.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function save_data_section_analysis 75.4% similar

    Flask API endpoint that saves analysis results (plots, conclusions, and analysis data) from a data analysis session to a data section in the database.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse