🔍 Code Extractor

function get_document_versions_v1

Maturity: 45

Retrieves all versions of a document from the database given its unique identifier (UID).

File:
/tf/active/vicechatdev/document_controller_backup.py
Lines:
2483 - 2519
Complexity:
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: String representing the unique identifier of the document whose versions should 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

  • logging
  • traceback
  • CDocs.db.db_operations
  • CDocs.config.settings
  • CDocs.db.schema_manager
  • CDocs.models.document
  • CDocs.models.user_extensions
  • CDocs.utils.document_processor
  • CDocs.utils.notifications
  • CDocs.utils.audit_trail
  • CDocs.controllers
  • CDocs.controllers.filecloud_controller
  • CDocs.utils.document_converter
  • CDocs.models.document_status
  • CDocs.utils.filecloud_integration
  • CDocs.models.review
  • CDocs.models.approval

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

Optional
import traceback

Condition: only when an exception occurs and detailed error logging is needed

Optional

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 handles exceptions internally and logs them, so no try-catch is needed when calling it
  • The function uses lazy imports to avoid circular dependencies - this is intentional design
  • Ensure the database connection is properly configured before calling this function
  • The logger 'CDocs.controllers.document_controller' should be configured to capture error logs

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_document_versions 88.2% similar

    Retrieves detailed information about a specific document version by its UID, including associated document context and version status.

    From: /tf/active/vicechatdev/document_controller_backup.py
  • function get_document 80.6% similar

    Retrieves comprehensive details of a controlled document by its UID, with optional inclusion of version history, review cycles, and approval cycles.

    From: /tf/active/vicechatdev/document_controller_backup.py
  • function get_document_download_url 70.8% similar

    Retrieves a download URL for a controlled document, automatically selecting between editable (Word) and PDF formats based on document status or explicit request.

    From: /tf/active/vicechatdev/document_controller_backup.py
  • function download_document_version 70.1% similar

    Downloads a specific version of a controlled document from FileCloud storage, with optional audit trail and watermark inclusion, and logs the download event.

    From: /tf/active/vicechatdev/document_controller_backup.py
  • function set_current_version 67.0% similar

    Sets a specific version of a controlled document as the current/active version, updating the document's currentVersionUID property and logging the change.

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