function get_document_v2
Retrieves detailed information about a specific document version by its UID, including associated document context and version status.
/tf/active/vicechatdev/document_controller_backup.py
644 - 692
moderate
Purpose
This function fetches comprehensive details about a document version from a document management system. It retrieves the version object, enriches it with parent document information (number, title, type, department, status), and determines if the version is the current active version. Used for displaying version history, comparing versions, or accessing specific document revisions in a controlled document management workflow.
Source Code
def get_document_version(version_uid: str) -> Dict[str, Any]:
"""
Get document version details by UID.
Args:
version_uid: UID of version to retrieve
Returns:
Dictionary with version details
Raises:
ResourceNotFoundError: If version not found
"""
try:
# Get version instance
version = DocumentVersion(uid=version_uid)
if not version:
raise ResourceNotFoundError(f"Version not found: {version_uid}")
# Get document for context
document = ControlledDocument(uid=version.document_uid)
if not document:
logger.warning(f"Document not found for version {version_uid}")
# Prepare result
result = version.to_dict()
# Add document context
if document:
result["document"] = {
"uid": document.uid,
"doc_number": document.doc_number,
"title": document.title,
"doc_type": document.doc_type,
"department": document.department,
"status": document.status
}
# Check if this is the current version
result["is_current_version"] = document.is_current_version(version_uid)
return result
except ResourceNotFoundError:
# Re-raise not found error
raise
except Exception as e:
logger.error(f"Error retrieving version {version_uid}: {e}")
raise BusinessRuleError(f"Failed to retrieve version: {e}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
version_uid |
str | - | positional_or_keyword |
Parameter Details
version_uid: Unique identifier (UID) string for the document version to retrieve. This should be a valid version UID that exists in the system's database. Expected to be a string format, likely UUID-based.
Return Value
Type: Dict[str, Any]
Returns a dictionary containing complete version details. The dictionary includes all fields from the DocumentVersion object (via to_dict() method) plus enriched data: a 'document' key with nested dictionary containing parent document metadata (uid, doc_number, title, doc_type, department, status), and an 'is_current_version' boolean flag indicating whether this version is the currently active version of the document.
Dependencies
loggingtypingCDocs.models.documentCDocs.controllersCDocs.dbCDocs.config
Required Imports
from typing import Dict, Any
from CDocs.models.document import DocumentVersion, ControlledDocument
from CDocs.controllers import ResourceNotFoundError, BusinessRuleError, log_controller_action
import logging
Usage Example
from CDocs.controllers.document_controller import get_document_version
from CDocs.controllers import ResourceNotFoundError, BusinessRuleError
try:
version_uid = "550e8400-e29b-41d4-a716-446655440000"
version_details = get_document_version(version_uid)
print(f"Version UID: {version_details['uid']}")
print(f"Document Title: {version_details['document']['title']}")
print(f"Document Number: {version_details['document']['doc_number']}")
print(f"Is Current Version: {version_details['is_current_version']}")
if version_details['is_current_version']:
print("This is the active version")
else:
print("This is a historical version")
except ResourceNotFoundError as e:
print(f"Version not found: {e}")
except BusinessRuleError as e:
print(f"Error retrieving version: {e}")
Best Practices
- Always wrap calls in try-except blocks to handle ResourceNotFoundError and BusinessRuleError exceptions
- The function is decorated with log_controller_action for audit trail purposes - ensure logging is properly configured
- Check the 'is_current_version' flag before performing operations that should only apply to active versions
- The function gracefully handles missing parent documents by logging a warning but still returning version data
- Validate version_uid format before calling if possible to avoid unnecessary database queries
- Use the returned document context for display purposes rather than making separate document queries
- Be aware that this function may raise BusinessRuleError for unexpected database or system errors
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_v6 87.4% similar
-
function get_document_v1 83.7% similar
-
function get_document 82.1% similar
-
function get_document_v5 80.6% similar
-
function get_document_metadata_from_filecloud 77.9% similar