function get_document_v4
Retrieves all versions of a controlled document from a Neo4j graph database, including version metadata and current version status.
/tf/active/vicechatdev/CDocs single class/db/db_operations.py
886 - 956
moderate
Purpose
This function queries a Neo4j database to fetch all versions associated with a specific controlled document identified by its UID. It retrieves version information including version numbers, creation dates, creator names, and file names, while also identifying which version is the current one. The function normalizes field names to provide consistent snake_case alternatives alongside camelCase properties, making the data easier to work with in Python applications. It's designed for document management systems that track version history and need to display or process multiple versions of controlled documents.
Source Code
def get_document_versions_from_db(document_uid):
"""
Get all versions for a document from the database.
Parameters
----------
document_uid : str
Document UID
Returns
-------
list
List of version dictionaries
"""
try:
logger.info(f"Getting versions for document {document_uid}")
# Query for versions
query = """
MATCH (d:ControlledDocument)-[:HAS_VERSION]->(v:DocumentVersion)
WHERE d.UID = $doc_uid
OPTIONAL MATCH (d)-[:CURRENT_VERSION]->(cv:DocumentVersion)
RETURN v as version,
(CASE WHEN v.UID = cv.UID THEN true ELSE false END) as is_current
ORDER BY v.versionNumber DESC
"""
params = {"doc_uid": document_uid}
result = run_query(query, params)
if not result:
logger.warning(f"No versions found for document {document_uid}")
return []
# Process results
versions = []
for record in result:
version_data = record.get("version")
is_current = record.get("is_current", False)
if version_data:
# Make a copy of the version data
version = dict(version_data)
# Add additional properties
version["is_current"] = is_current
# Ensure key fields exist with consistent names
if "versionNumber" in version and "version_number" not in version:
version["version_number"] = version["versionNumber"]
if "createdDate" in version and "created_date" not in version:
version["created_date"] = version["createdDate"]
if "creatorName" in version and "created_by_name" not in version:
version["created_by_name"] = version["creatorName"]
if "fileName" in version and "file_name" not in version:
version["file_name"] = version["fileName"]
versions.append(version)
logger.info(f"Found {len(versions)} versions for document {document_uid}")
return versions
except Exception as e:
logger.error(f"Error getting document versions from DB: {e}")
import traceback
logger.error(traceback.format_exc())
return []
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
document_uid |
- | - | positional_or_keyword |
Parameter Details
document_uid: A string representing the unique identifier (UID) of the controlled document whose versions should be retrieved. This UID must match the UID property of a ControlledDocument node in the Neo4j database. Expected to be a non-empty string, typically a UUID or similar unique identifier.
Return Value
Returns a list of dictionaries, where each dictionary represents a document version. Each dictionary contains version properties from the Neo4j DocumentVersion node (such as UID, versionNumber, createdDate, creatorName, fileName) plus an additional 'is_current' boolean field indicating if that version is the current version. The function also adds snake_case aliases for common fields (version_number, created_date, created_by_name, file_name). The list is ordered by version number in descending order (newest first). Returns an empty list if no versions are found or if an error occurs.
Dependencies
loggingneo4jtraceback
Required Imports
import logging
import traceback
from neo4j import Driver, Session, Transaction, Record
from neo4j.exceptions import ServiceUnavailable, ClientError
Conditional/Optional Imports
These imports are only needed under specific conditions:
import traceback
Condition: Used only in exception handling to log detailed error traces
Required (conditional)Usage Example
import logging
from CDocs.db import get_driver
# Setup logger
logger = logging.getLogger(__name__)
# Assuming run_query function is available
def run_query(query, params):
driver = get_driver()
with driver.session() as session:
result = session.run(query, params)
return [record for record in result]
# Get versions for a document
document_uid = "doc-12345-abcde"
versions = get_document_versions_from_db(document_uid)
if versions:
print(f"Found {len(versions)} versions")
for version in versions:
version_num = version.get('version_number', 'Unknown')
is_current = version.get('is_current', False)
created_date = version.get('created_date', 'Unknown')
print(f"Version {version_num} - Current: {is_current} - Created: {created_date}")
else:
print("No versions found")
Best Practices
- Always check if the returned list is empty before processing versions to handle cases where no versions exist
- The function returns an empty list on errors rather than raising exceptions, so check logs for error details if unexpected empty results occur
- The function provides both camelCase (from database) and snake_case field names for flexibility; choose one naming convention consistently in your code
- The 'is_current' field is added by the function and is not stored in the database; it's computed during the query
- Ensure the document_uid parameter is valid and exists in the database to avoid unnecessary queries
- The function logs extensively; ensure logging is properly configured to capture info and error messages
- Results are ordered by version number descending, so the first item in the list is the newest version
- The run_query function dependency must be properly implemented with error handling and connection management
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_v7 97.0% similar
-
function get_document_v8 87.2% similar
-
function get_document_v3 81.6% similar
-
function get_document_v2 79.3% similar
-
function get_document_v1 76.9% similar