🔍 Code Extractor

function get_text_section

Maturity: 51

Flask API endpoint that retrieves a specific text section by ID with optional version history and usage information, enforcing ownership-based access control.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
602 - 627
Complexity:
moderate

Purpose

This endpoint serves as a REST API route to fetch detailed information about a text section. It validates user authentication and ownership, then returns the text section data. Optionally includes version history and usage statistics across documents based on query parameters. Returns 404 if section not found, 403 if user lacks access rights.

Source Code

def get_text_section(section_id):
    """Get a specific text section with optional usage information"""
    text_section = text_section_service.get_text_section(section_id)
    
    if not text_section:
        return jsonify({'error': 'Text section not found'}), 404
    
    # Check ownership
    if text_section.owner != get_current_user():
        return jsonify({'error': 'Access denied'}), 403
    
    # Include versions if requested
    include_versions = request.args.get('include_versions', 'false').lower() == 'true'
    include_usage = request.args.get('include_usage', 'false').lower() == 'true'
    
    result = text_section.to_dict()
    
    if include_versions:
        versions = text_section_service.get_text_section_versions(section_id)
        result['versions'] = [version.to_dict() for version in versions]
    
    if include_usage:
        usage_info = document_service.get_text_section_usage(section_id, get_current_user())
        result['usage'] = usage_info
    
    return jsonify(result)

Parameters

Name Type Default Kind
section_id - - positional_or_keyword

Parameter Details

section_id: String identifier for the text section to retrieve. Passed as a URL path parameter. Used to query the text_section_service for the specific section.

include_versions: Optional query parameter (boolean as string 'true'/'false', default 'false'). When 'true', includes all version history of the text section in the response.

include_usage: Optional query parameter (boolean as string 'true'/'false', default 'false'). When 'true', includes information about which documents use this text section.

Return Value

Returns a Flask JSON response tuple. On success (200): JSON object containing text section data from to_dict() method, optionally with 'versions' array and 'usage' object. On not found (404): JSON with 'error' key and message 'Text section not found'. On access denied (403): JSON with 'error' key and message 'Access denied'.

Dependencies

  • flask
  • models
  • services

Required Imports

from flask import jsonify
from flask import request
from services import TextSectionService
from services import DocumentService

Usage Example

# Example API calls:
# Basic retrieval:
# GET /api/text_sections/abc123
# Response: {"id": "abc123", "content": "...", "owner": "user@example.com", ...}

# With versions:
# GET /api/text_sections/abc123?include_versions=true
# Response: {"id": "abc123", ..., "versions": [{"version": 1, ...}, {"version": 2, ...}]}

# With usage info:
# GET /api/text_sections/abc123?include_usage=true
# Response: {"id": "abc123", ..., "usage": {"documents": [...], "count": 5}}

# Combined:
# GET /api/text_sections/abc123?include_versions=true&include_usage=true

# Using with requests library:
import requests
response = requests.get(
    'http://localhost:5000/api/text_sections/abc123',
    params={'include_versions': 'true', 'include_usage': 'true'},
    headers={'Authorization': 'Bearer <token>'}
)
data = response.json()

Best Practices

  • Always authenticate requests - the require_auth decorator must be properly configured
  • Validate section_id format before passing to service layer to prevent injection attacks
  • The function enforces ownership checks - only the owner can access their text sections
  • Use include_versions and include_usage sparingly as they add database queries and increase response time
  • Handle 403 and 404 responses appropriately in client code
  • The endpoint supports both /api/text_sections/ and /api/text-sections/ URL patterns for flexibility
  • Query parameters are case-insensitive for boolean values ('true', 'True', 'TRUE' all work)
  • Consider implementing pagination if versions array can be large
  • Ensure text_section_service and document_service are properly initialized before app starts
  • The to_dict() methods should sanitize sensitive data before returning

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_text_section_usage 88.5% similar

    Flask API endpoint that retrieves usage information for a specific text section, showing which documents reference it. Requires authentication and verifies section ownership before returning usage data.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_text_section_versions 88.1% similar

    Flask API endpoint that retrieves all historical versions of a specific text section, with ownership verification and authentication required.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_text_sections 82.7% similar

    Flask API endpoint that retrieves text sections for the authenticated user with optional filtering by type, search query, tags, and uniqueness.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_document_text_sections 80.7% similar

    Flask API endpoint that retrieves all text and data sections for a specific document, verifying user ownership and returning sections sorted by position.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_section_chat_history 80.6% similar

    Flask API endpoint that retrieves chat history for a specific text section, verifying user ownership before returning messages.

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