🔍 Code Extractor

function get_text_section_versions

Maturity: 48

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

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
712 - 725
Complexity:
moderate

Purpose

This endpoint provides version history functionality for text sections in a document management system. It authenticates the user, verifies they own the requested text section, and returns all saved versions of that section. This is useful for tracking changes, implementing undo/redo functionality, or auditing document modifications over time.

Source Code

def get_text_section_versions(section_id):
    """Get all versions of a text section"""
    user_email = get_current_user()
    
    # Verify ownership
    text_section = text_section_service.get_text_section(section_id)
    if not text_section or text_section.owner != user_email:
        return jsonify({'error': 'Text section not found or access denied'}), 404
    
    versions = text_section_service.get_text_section_versions(section_id)
    
    return jsonify({
        'versions': [version.to_dict() for version in versions]
    })

Parameters

Name Type Default Kind
section_id - - positional_or_keyword

Parameter Details

section_id: String identifier (likely UUID) of the text section whose versions should be retrieved. This is extracted from the URL path parameter and used to look up the text section and its version history.

Return Value

Returns a Flask JSON response. On success (200): {'versions': [list of version dictionaries from TextSectionVersion.to_dict()]}. On failure (404): {'error': 'Text section not found or access denied'} when the section doesn't exist or the authenticated user is not the owner.

Dependencies

  • flask
  • models
  • services

Required Imports

from flask import jsonify
from models import TextSection
from models import TextSectionVersion
from services import TextSectionService

Usage Example

# Assuming Flask app setup with authentication
# GET request to: /api/text-sections/abc-123-def-456/versions
# Headers: Authorization token (handled by require_auth decorator)

# Example response on success:
# {
#   "versions": [
#     {
#       "id": "version-1",
#       "section_id": "abc-123-def-456",
#       "content": "Original text",
#       "created_at": "2024-01-01T10:00:00",
#       "version_number": 1
#     },
#     {
#       "id": "version-2",
#       "section_id": "abc-123-def-456",
#       "content": "Updated text",
#       "created_at": "2024-01-02T15:30:00",
#       "version_number": 2
#     }
#   ]
# }

# Example client-side usage:
import requests

response = requests.get(
    'https://api.example.com/api/text-sections/abc-123-def-456/versions',
    headers={'Authorization': 'Bearer YOUR_TOKEN'}
)

if response.status_code == 200:
    versions = response.json()['versions']
    for version in versions:
        print(f"Version {version['version_number']}: {version['content']}")
else:
    print(f"Error: {response.json()['error']}")

Best Practices

  • Always verify ownership before returning sensitive data - this function correctly checks that text_section.owner matches the authenticated user
  • The function returns 404 for both 'not found' and 'access denied' cases, which is good security practice to avoid information leakage
  • Ensure the require_auth decorator is properly implemented to prevent unauthorized access
  • The text_section_service should handle database errors gracefully and return None for non-existent sections
  • Consider implementing pagination if version lists can grow very large
  • The to_dict() method on TextSectionVersion should sanitize any sensitive data before serialization
  • Consider adding query parameters for filtering versions by date range or limiting the number of versions returned
  • Log access attempts for security auditing purposes
  • Ensure proper database indexing on section_id and owner fields for performance

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_text_section 88.1% similar

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

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function revert_text_section 80.9% similar

    Flask API endpoint that reverts a text section to a specific previous version, verifying user ownership before performing the revert operation.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_text_section_usage 80.8% 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_document_text_sections 78.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_document_v3 78.0% similar

    Flask API endpoint that retrieves all versions of a specific document, verifying user ownership before returning the version history.

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