class DocumentVersionRelationMixin
A mixin class that provides document version relationship functionality, enabling objects to store and retrieve associations with DocumentVersion and Document objects.
/tf/active/vicechatdev/CDocs single class/models/workflow_base.py
719 - 758
moderate
Purpose
This mixin is designed to be inherited by other classes that need to maintain a relationship with a DocumentVersion. It provides methods and properties to get/set the document version UID, retrieve the associated DocumentVersion object, and navigate up to the parent Document. The mixin assumes the inheriting class has a `_data` dictionary attribute for storing data and a `_modified` flag for tracking changes.
Source Code
class DocumentVersionRelationMixin:
"""Mixin to add document version relationship functionality."""
def get_document_version_uid(self) -> Optional[str]:
"""Get the document version UID."""
return self._data.get('document_version_uid')
@property
def document_version_uid(self) -> Optional[str]:
"""Get the document version UID."""
return self._data.get('document_version_uid')
@document_version_uid.setter
def document_version_uid(self, value: str) -> None:
"""Set the document version UID."""
self._data['document_version_uid'] = value
self._modified = True
def get_document_version(self):
"""Get the associated document version."""
version_uid = self.get_document_version_uid()
if not version_uid:
return None
# Get the document version node
version_data = db.get_node_by_uid(version_uid)
if not version_data:
return None
# Import here to avoid circular imports
from CDocs.models.document import DocumentVersion
return DocumentVersion(version_data)
def get_document(self):
"""Get the associated document."""
version = self.get_document_version()
if not version:
return None
return version.get_document()
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
- | - |
Parameter Details
bases: This is a mixin class and does not have __init__ parameters. It expects the inheriting class to provide `_data` (dict) and `_modified` (bool) attributes. The `_data` dictionary should support storing 'document_version_uid' key.
Return Value
The class itself returns a mixin instance when inherited. Key method returns: get_document_version_uid() and document_version_uid property return Optional[str] (the UID or None), get_document_version() returns Optional[DocumentVersion] object, and get_document() returns Optional[Document] object. All retrieval methods return None if the relationship is not established or if the referenced objects don't exist in the database.
Class Interface
Methods
get_document_version_uid(self) -> Optional[str]
Purpose: Retrieves the document version UID from the internal data dictionary
Returns: The document version UID as a string if set, otherwise None
@property document_version_uid(self) -> Optional[str]
property
Purpose: Property getter for the document version UID
Returns: The document version UID as a string if set, otherwise None
@document_version_uid.setter document_version_uid(self, value: str) -> None
property
Purpose: Property setter for the document version UID, marks the object as modified
Parameters:
value: The document version UID string to set
Returns: None
get_document_version(self)
Purpose: Retrieves the associated DocumentVersion object from the database using the stored UID
Returns: A DocumentVersion object if found, otherwise None. Returns None if document_version_uid is not set or if the database lookup fails.
get_document(self)
Purpose: Navigates through the DocumentVersion to retrieve the parent Document object
Returns: A Document object if found, otherwise None. Returns None if the document version is not set or if the version's parent document cannot be retrieved.
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
_data |
Dict[str, Any] | Internal dictionary for storing data, must be provided by the inheriting class. Stores the 'document_version_uid' key among potentially other data. | instance |
_modified |
bool | Flag indicating whether the object has been modified, must be provided by the inheriting class. Set to True when document_version_uid is changed. | instance |
Dependencies
typingCDocs.db.db_operationsCDocs.models.document
Required Imports
from typing import Optional
from CDocs.db import db_operations as db
Conditional/Optional Imports
These imports are only needed under specific conditions:
from CDocs.models.document import DocumentVersion
Condition: Imported lazily inside get_document_version() method to avoid circular imports. Required when calling get_document_version().
Required (conditional)Usage Example
# Example of a class using this mixin
class MyEntity(DocumentVersionRelationMixin):
def __init__(self, data=None):
self._data = data or {}
self._modified = False
# Create an instance
entity = MyEntity()
# Set document version UID
entity.document_version_uid = 'version-123-uid'
# Get document version UID (two ways)
uid = entity.get_document_version_uid()
uid = entity.document_version_uid
# Get the associated DocumentVersion object
version = entity.get_document_version()
if version:
print(f'Found version: {version}')
# Navigate to the parent Document
document = entity.get_document()
if document:
print(f'Found document: {document}')
Best Practices
- This is a mixin class and should not be instantiated directly. It must be inherited by classes that provide `_data` and `_modified` attributes.
- The `_data` attribute must be a dictionary that supports item assignment and retrieval.
- The `_modified` flag is automatically set to True when document_version_uid is changed via the setter.
- Use the property setter (document_version_uid = value) to ensure the _modified flag is properly updated.
- The get_document_version() method performs a database lookup, so cache the result if you need to access it multiple times.
- All retrieval methods (get_document_version, get_document) return None if the relationship is not set or if referenced objects don't exist, so always check for None before using the result.
- The DocumentVersion import is done lazily inside get_document_version() to avoid circular import issues. This is intentional and should not be moved to the top of the file.
- Method call order: Set document_version_uid first, then call get_document_version() or get_document() to navigate the relationship chain.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DocumentVersion_v1 68.7% similar
-
class DocumentVersion 63.4% similar
-
class DocumentVersion_v1 62.9% similar
-
class Document 62.2% similar
-
function get_document_v3 58.1% similar