function _view_document
Views and downloads the current version of a document, with special handling for FileCloud-stored documents versus locally stored documents.
/tf/active/vicechatdev/document_controller_backup.py
2389 - 2425
moderate
Purpose
This method is part of a document management system that handles viewing/downloading document versions. It checks if the current document version is stored in FileCloud (external storage) or locally, then retrieves and initiates a download of the document file. It provides error handling and user notifications through a notification area UI component.
Source Code
def _view_document(self, event=None):
"""View the current document version"""
if not self.current_version:
self.notification_area.object = "**Error:** No document version available"
return
# Get version UID
version_uid = self.current_version.get('UID')
# For FileCloud stored documents, we retrieve from there
try:
# Is this stored in FileCloud?
storage_type = self.current_version.get('storage_type', '')
if (storage_type == 'FILECLOUD' and self.current_version.get('filecloud_link')):
# Get the file from FileCloud using the link
from CDocs.controllers.filecloud_controller import download_document_from_filecloud
doc_content = download_document_from_filecloud(
user=self.user,
document_uid=self.document_uid
)
# Handle response correctly based on what download_document_from_filecloud returns
if isinstance(doc_content, bytes):
# Direct bytes content
file_data = io.BytesIO(doc_content)
file_name = self.current_version.get('file_name', 'document.pdf')
pn.state.execute_download(file_data, filename=file_name)
else:
# Handle dictionary response if that's what the function returns
self.notification_area.object = "**Error:** Could not download document from FileCloud"
else:
# Fall back to default document retrieval
self._download_current_version(event)
except Exception as e:
logger.error(f"Error viewing document: {e}")
self.notification_area.object = f"**Error:** {str(e)}"
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
self |
- | - | positional_or_keyword |
event |
- | None | positional_or_keyword |
Parameter Details
self: Instance reference to the containing class. Expected to have attributes: current_version (dict with document metadata), notification_area (UI component for displaying messages), user (user object), and document_uid (unique identifier for the document).
event: Optional event parameter, typically passed by UI event handlers (e.g., button clicks). Defaults to None. Not directly used in the function body but may be passed to _download_current_version.
Return Value
Returns None implicitly. The function performs side effects: either initiates a file download via pn.state.execute_download(), displays error messages in the notification_area, or calls _download_current_version() as a fallback.
Dependencies
paneliologgingCDocs.controllers.filecloud_controllerCDocs
Required Imports
import io
import panel as pn
import logging
Conditional/Optional Imports
These imports are only needed under specific conditions:
from CDocs.controllers.filecloud_controller import download_document_from_filecloud
Condition: Required when storage_type is 'FILECLOUD' and filecloud_link exists in current_version
Required (conditional)Usage Example
# Assuming this is a method in a DocumentViewer class
# Example usage within a Panel application:
import panel as pn
import io
from CDocs.controllers.filecloud_controller import download_document_from_filecloud
class DocumentViewer:
def __init__(self, user, document_uid):
self.user = user
self.document_uid = document_uid
self.notification_area = pn.pane.Markdown('')
self.current_version = {
'UID': 'doc-123-v1',
'storage_type': 'FILECLOUD',
'filecloud_link': 'https://filecloud.example.com/doc123.pdf',
'file_name': 'document_v1.pdf'
}
def _download_current_version(self, event=None):
# Fallback method for local storage
pass
def _view_document(self, event=None):
# ... (the function code here)
pass
# Create viewer instance
viewer = DocumentViewer(user=current_user, document_uid='doc-123')
# Create a button to trigger document viewing
view_button = pn.widgets.Button(name='View Document')
view_button.on_click(viewer._view_document)
# Display in Panel app
app = pn.Column(view_button, viewer.notification_area)
app.show()
Best Practices
- Ensure self.current_version is populated before calling this method to avoid the 'No document version available' error
- The function assumes a logger is configured at module level; ensure logging is properly set up
- This method should only be called within a Panel application context where pn.state.execute_download() is available
- The download_document_from_filecloud function should return bytes for successful downloads; verify this contract is maintained
- Consider adding more specific exception handling instead of catching all exceptions to better diagnose issues
- The notification_area should be a Panel component that supports Markdown formatting (uses ** for bold)
- Ensure proper permissions are checked before allowing document viewing (this function doesn't check permissions itself)
- The fallback to _download_current_version should be implemented in the containing class for non-FileCloud documents
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function _download_current_version 86.1% similar
-
function download_document_version 69.3% similar
-
function download_document_version_v1 67.7% similar
-
function download_document_from_filecloud 67.4% similar
-
function get_document_download_url 65.5% similar