class DocumentExportService
Service class for exporting documents in various formats with enhanced formatting, providing both full structured exports and content-only exports.
/tf/active/vicechatdev/vice_ai/services.py
820 - 873
moderate
Purpose
DocumentExportService is responsible for transforming document data into exportable formats. It provides two main export modes: (1) comprehensive structured export including metadata, sections, references, and version information suitable for JSON/API responses, and (2) content-only export optimized for document generation formats like PDF or Word. The service acts as a facade over DocumentService, aggregating document data with text sections and references into cohesive export structures.
Source Code
class DocumentExportService:
"""Service for exporting documents with enhanced formatting"""
def __init__(self, document_service: DocumentService):
self.document_service = document_service
def export_document_to_dict(self, document_id: str) -> Optional[Dict]:
"""Export document as structured dictionary for various formats"""
document, text_sections = self.document_service.get_document_with_text_sections(document_id)
if not document:
return None
# Build structured export
export_data = {
'document': document.to_dict(),
'sections': [],
'references': self.document_service.get_document_references(document_id),
'export_timestamp': datetime.now().isoformat()
}
for doc_section, text_section in zip(document.sections, text_sections):
section_data = {
'document_section': doc_section.to_dict(),
'text_section': text_section.to_dict(),
'versions_count': len(
self.document_service.text_section_service.get_text_section_versions(text_section.id)
)
}
export_data['sections'].append(section_data)
return export_data
def export_document_content_only(self, document_id: str) -> Optional[Dict]:
"""Export only content for PDF/Word generation"""
document, text_sections = self.document_service.get_document_with_text_sections(document_id)
if not document:
return None
content_sections = []
for text_section in text_sections:
content_sections.append({
'id': text_section.id,
'title': text_section.title,
'type': text_section.section_type.value,
'level': text_section.level,
'content': text_section.current_content
})
return {
'title': document.title,
'description': document.description,
'sections': content_sections,
'references': self.document_service.get_document_references(document_id)
}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
document_service: An instance of DocumentService that provides access to document data, text sections, and references. This is the primary dependency for retrieving all document-related information needed for exports.
Return Value
Instantiation returns a DocumentExportService object. The export_document_to_dict method returns an Optional[Dict] containing comprehensive document data including document metadata, sections with version counts, references, and export timestamp, or None if document not found. The export_document_content_only method returns an Optional[Dict] with simplified content structure containing title, description, flattened section content, and references, or None if document not found.
Class Interface
Methods
__init__(self, document_service: DocumentService)
Purpose: Initialize the DocumentExportService with a DocumentService dependency
Parameters:
document_service: DocumentService instance that provides access to document data, text sections, and references
Returns: None - constructor initializes the service instance
export_document_to_dict(self, document_id: str) -> Optional[Dict]
Purpose: Export a complete document as a structured dictionary including all metadata, sections with version counts, references, and export timestamp
Parameters:
document_id: String identifier of the document to export
Returns: Dictionary containing 'document' (document metadata), 'sections' (list of section data with document_section, text_section, and versions_count), 'references' (document references), and 'export_timestamp' (ISO format timestamp). Returns None if document not found.
export_document_content_only(self, document_id: str) -> Optional[Dict]
Purpose: Export only the content portions of a document optimized for PDF/Word generation, excluding metadata and version information
Parameters:
document_id: String identifier of the document to export
Returns: Dictionary containing 'title' (document title), 'description' (document description), 'sections' (list of simplified section objects with id, title, type, level, and content), and 'references' (document references). Returns None if document not found.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
document_service |
DocumentService | Instance of DocumentService used to retrieve document data, text sections, references, and version information | instance |
Dependencies
datetimetyping
Required Imports
from typing import Dict, Optional
from datetime import datetime
Usage Example
# Instantiate the service
from document_service import DocumentService
from document_export_service import DocumentExportService
# Assume document_service is already configured
document_service = DocumentService(db_manager)
export_service = DocumentExportService(document_service)
# Export full document structure
full_export = export_service.export_document_to_dict('doc-123')
if full_export:
print(f"Exported document: {full_export['document']['title']}")
print(f"Number of sections: {len(full_export['sections'])}")
print(f"Export timestamp: {full_export['export_timestamp']}")
# Export content only for PDF generation
content_export = export_service.export_document_content_only('doc-123')
if content_export:
print(f"Title: {content_export['title']}")
for section in content_export['sections']:
print(f"Section: {section['title']} - {section['type']}")
Best Practices
- Always check if the returned dictionary is None before accessing its contents, as documents may not exist
- The service is stateless and thread-safe - it can be instantiated once and reused for multiple exports
- Use export_document_to_dict for comprehensive exports including metadata and version history
- Use export_document_content_only for document generation workflows (PDF, Word) where only content matters
- The export_timestamp in full exports can be used for cache invalidation or audit trails
- Ensure the underlying DocumentService has proper error handling and database connection management
- The service depends on Document, DocumentSection, and TextSection models having to_dict() methods implemented
- Consider caching export results if the same document is exported multiple times without changes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DocumentService 66.2% similar
-
function export_to_docx_v1 64.3% similar
-
function export_to_pdf_v1 64.1% similar
-
function export_to_docx 62.6% similar
-
function export_to_pdf 62.2% similar