function get_document_v4
Flask API endpoint that retrieves a specific document with its text and data sections, including optional sharing information, for authenticated users.
/tf/active/vicechatdev/vice_ai/new_app.py
1027 - 1074
moderate
Purpose
This endpoint serves as the primary API for fetching complete document details including all associated text sections and data sections. It enforces ownership validation, enriches sections with position information, and optionally includes sharing/usage metadata. The function is designed for document management systems where users need to view and manage their documents with detailed section information.
Source Code
def get_document(document_id):
"""Get a specific document with its text sections and sharing information"""
user_email = get_current_user()
document = document_service.get_document(document_id)
if not document or document.owner != user_email:
return jsonify({'error': 'Document not found or access denied'}), 404
# Get document with all sections (text and data)
result_tuple = document_service.get_document_with_all_sections(document_id)
if not result_tuple:
return jsonify({'error': 'Failed to load document sections'}), 500
document, text_sections, data_sections = result_tuple
# Create position lookup from document.sections
position_map = {ds.section_id: ds.position for ds in document.sections}
include_sharing_info = request.args.get('include_sharing', 'false').lower() == 'true'
result = document.to_dict()
# Add position to each section dict
result['text_sections'] = []
for section in text_sections:
section_dict = section.to_dict()
section_dict['position'] = position_map.get(section.id, 999)
result['text_sections'].append(section_dict)
result['data_sections'] = []
for section in data_sections:
section_dict = section.to_dict()
section_dict['position'] = position_map.get(section.id, 999)
result['data_sections'].append(section_dict)
# Add sharing information if requested
if include_sharing_info:
for i, section in enumerate(text_sections):
usage_info = document_service.get_text_section_usage(section.id, user_email)
# Add sharing information to the section data
result['text_sections'][i]['usage_info'] = {
'shared_in_documents': len(usage_info),
'is_shared': len(usage_info) > 1,
'usage_details': usage_info
}
# Clean NaN/Inf values before returning
return jsonify(clean_for_json(result))
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_id |
- | - | positional_or_keyword |
Parameter Details
document_id: String identifier for the document to retrieve. This is extracted from the URL path parameter. Must correspond to an existing document owned by the authenticated user.
Return Value
Returns a JSON response containing the complete document object with nested text_sections and data_sections arrays. Each section includes its position and content. If include_sharing query parameter is true, text sections also include usage_info with shared_in_documents count, is_shared boolean, and usage_details array. Returns 404 error if document not found or access denied, 500 error if sections fail to load. All numeric values are cleaned to remove NaN/Inf before serialization.
Dependencies
flasktypingmodelsservices
Required Imports
from flask import jsonify, request
from models import Document, TextSection, DataSection, DocumentSection
from services import DocumentService
Usage Example
# Example API call to retrieve a document
import requests
# Basic document retrieval
response = requests.get(
'http://localhost:5000/api/documents/doc-123',
headers={'Authorization': 'Bearer <token>'}
)
document = response.json()
print(f"Document: {document['title']}")
print(f"Text sections: {len(document['text_sections'])}")
print(f"Data sections: {len(document['data_sections'])}")
# Retrieve with sharing information
response = requests.get(
'http://localhost:5000/api/documents/doc-123?include_sharing=true',
headers={'Authorization': 'Bearer <token>'}
)
document_with_sharing = response.json()
for section in document_with_sharing['text_sections']:
if section.get('usage_info', {}).get('is_shared'):
print(f"Section {section['id']} is shared in {section['usage_info']['shared_in_documents']} documents")
Best Practices
- Always authenticate requests using the require_auth decorator before accessing this endpoint
- Use the include_sharing query parameter sparingly as it adds additional database queries for usage information
- Handle both 404 (not found/access denied) and 500 (server error) responses appropriately in client code
- The position_map ensures sections are properly ordered; default position 999 is used for sections without explicit positioning
- The clean_for_json function is critical for preventing JSON serialization errors with NaN/Inf values from data sections
- Document ownership is strictly enforced - users can only access their own documents
- Text sections and data sections are returned as separate arrays for easier client-side processing
- The usage_info provides valuable metadata for understanding section reuse across documents
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_text_sections 83.4% similar
-
function get_document_v3 82.8% similar
-
function get_available_sections_for_document 79.1% similar
-
function api_get_document 78.7% similar
-
function get_text_section_versions 77.3% similar