function get_document_v6
Retrieves all versions of a document from the database given its unique identifier (UID).
/tf/active/vicechatdev/document_controller_backup.py
2483 - 2519
simple
Purpose
This function serves as a controller method to fetch version history for a specific document in a document management system. It handles error cases (missing UID, database errors) and returns a standardized response dictionary containing the document's version list. It's designed to be used in document version tracking and audit trail features.
Source Code
def get_document_versions(document_uid):
"""
Get all versions for a document.
Parameters
----------
document_uid : str
Document UID
Returns
-------
dict
Dictionary with versions list
"""
if not document_uid:
return {"success": False, "message": "No document UID provided"}
try:
# Import here to avoid circular imports
# Get versions from database
versions = get_document_versions_from_db(document_uid)
# Format response
return {
"success": True,
"document_uid": document_uid,
"versions": versions
}
except Exception as e:
import logging
logger = logging.getLogger("CDocs.controllers.document_controller")
logger.error(f"Error getting document versions: {e}")
import traceback
logger.error(traceback.format_exc())
return {"success": False, "message": str(e)}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_uid |
- | - | positional_or_keyword |
Parameter Details
document_uid: A string representing the unique identifier of the document whose versions are to be retrieved. Must be a non-empty string. If empty or None, the function returns an error response.
Return Value
Returns a dictionary with the following structure: On success: {'success': True, 'document_uid': str, 'versions': list} where 'versions' contains the list of document versions from the database. On failure: {'success': False, 'message': str} where 'message' contains the error description.
Dependencies
loggingtracebackCDocs.db.db_operationsCDocs.config.settingsCDocs.db.schema_managerCDocs.models.documentCDocs.models.user_extensionsCDocs.utils.document_processorCDocs.utils.notificationsCDocs.utils.audit_trailCDocs.controllersCDocs.controllers.filecloud_controllerCDocs.utils.document_converterCDocs.models.document_statusCDocs.utils.filecloud_integrationCDocs.models.reviewCDocs.models.approvalpaneluuidostempfiledatetimeioshutiltypingconfigrandom
Required Imports
from CDocs.db.db_operations import get_document_versions_from_db
Conditional/Optional Imports
These imports are only needed under specific conditions:
import logging
Condition: only when an exception occurs during execution
Required (conditional)import traceback
Condition: only when an exception occurs and detailed error logging is needed
Required (conditional)Usage Example
from CDocs.controllers.document_controller import get_document_versions
# Get versions for a specific document
document_uid = 'doc-12345-abcde'
result = get_document_versions(document_uid)
if result['success']:
print(f"Found {len(result['versions'])} versions for document {result['document_uid']}")
for version in result['versions']:
print(f"Version: {version}")
else:
print(f"Error: {result['message']}")
# Handle missing UID
result = get_document_versions('')
print(result) # {'success': False, 'message': 'No document UID provided'}
Best Practices
- Always check the 'success' field in the returned dictionary before accessing 'versions' to avoid KeyError
- Provide a valid non-empty document_uid to avoid immediate error response
- The function uses lazy imports for logging and traceback to avoid circular dependencies - this is intentional
- Error messages are logged with full stack traces for debugging purposes
- The function returns a consistent dictionary structure for both success and failure cases, making it easy to handle responses uniformly
- Consider implementing caching if this function is called frequently for the same document_uid
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_v2 87.4% similar
-
function get_document_v5 87.3% similar
-
function get_document_v1 81.5% similar
-
function get_document 80.8% similar
-
function compare_document_versions 72.8% similar