🔍 Code Extractor

function create_approval_panel_v1

Maturity: 44

Factory function that creates and initializes an ApprovalPanel instance for managing document approvals, with error handling and fallback to a minimal panel on failure.

File:
/tf/active/vicechatdev/CDocs/ui/approval_panel.py
Lines:
1959 - 2008
Complexity:
moderate

Purpose

This function serves as a factory to instantiate an ApprovalPanel component for document approval workflows. It supports both standalone and embedded modes, handles session management, integrates with parent applications, and provides robust error handling with a fallback minimal panel. It automatically detects embedded mode, synchronizes user state from parent applications, and triggers initial data loading.

Source Code

def create_approval_panel(session_manager=None, parent_app=None, embedded=False):
    """Create and initialize a approval panel instance"""
    try:
        import panel as pn
        
        # Create template for approval panel (only used in standalone mode)
        approval_bp = pn.template.BootstrapTemplate(
            title="Approval Management",
            header_background="#3DCAB1"
        )
        
        # Create and initialize panel
        panel = ApprovalPanel(
            template=approval_bp,
            session_manager=session_manager,
            parent_app=parent_app,
            embedded=embedded or parent_app is not None  # Auto-detect embedded mode
        )
        
        # If parent_app has a current_user, set it directly
        if parent_app and hasattr(parent_app, 'current_user') and parent_app.current_user:
            panel.user = parent_app.current_user
            
            # Reload data if method exists
            if hasattr(panel, '_load_pending_approvals'):
                panel._load_pending_approvals()
        
        return panel
    except Exception as e:
        import logging
        logger = logging.getLogger('CDocs.ui.approval_panel')
        logger.error(f"Error creating approval panel: {e}")
        import traceback
        logger.error(traceback.format_exc())
        
        # Return a minimal functioning panel
        import panel as pn
        minimal_panel = pn.Column(
            pn.pane.Markdown("# Approval Management"),
            pn.pane.Markdown(f"**Error loading approval panel:** {str(e)}")
        )
        minimal_panel.main_content = minimal_panel
        
        # Add minimal set_user method to avoid errors
        def minimal_set_user(obj, user):
            obj.user = user
        import types
        minimal_panel.set_user = types.MethodType(minimal_set_user, minimal_panel)
        
        return minimal_panel

Parameters

Name Type Default Kind
session_manager - None positional_or_keyword
parent_app - None positional_or_keyword
embedded - False positional_or_keyword

Parameter Details

session_manager: Optional SessionManager instance for handling user sessions and authentication. If None, the panel may operate without session management capabilities. Expected type: SessionManager or None.

parent_app: Optional reference to a parent application object that contains this panel. Used to access parent application state like current_user. If provided, the panel operates in embedded mode. Expected type: Application object with 'current_user' attribute or None.

embedded: Boolean flag indicating whether the panel should run in embedded mode (within another application) or standalone mode. Defaults to False. Auto-detected as True if parent_app is provided. Expected type: bool.

Return Value

Returns an initialized ApprovalPanel instance with configured template, session manager, and user state. On error, returns a minimal Panel Column object with error message display and a stub set_user method to prevent downstream errors. Return type: ApprovalPanel or pn.Column.

Dependencies

  • panel
  • logging
  • traceback
  • types

Required Imports

import panel as pn
import logging
import traceback
import types

Conditional/Optional Imports

These imports are only needed under specific conditions:

import panel as pn

Condition: Always required - imported at function start

Required (conditional)
import logging

Condition: Required for error logging in exception handler

Required (conditional)
import traceback

Condition: Required for detailed error logging in exception handler

Required (conditional)
import types

Condition: Required for creating bound methods on minimal fallback panel

Required (conditional)

Usage Example

# Standalone mode
approval_panel = create_approval_panel()

# With session manager
from CDocs.auth.session_manager import SessionManager
session_mgr = SessionManager()
approval_panel = create_approval_panel(session_manager=session_mgr)

# Embedded in parent application
class MyApp:
    def __init__(self):
        self.current_user = DocUser(username='john_doe')
        self.approval_panel = create_approval_panel(parent_app=self, embedded=True)

# Access the panel's main content
app = MyApp()
app.approval_panel.main_content.servable()

# Set user manually if needed
approval_panel.set_user(user_object)

Best Practices

  • Always handle the returned panel gracefully as it may be either a full ApprovalPanel or a minimal fallback panel
  • Check logs for 'CDocs.ui.approval_panel' logger to diagnose initialization failures
  • When using parent_app, ensure it has a 'current_user' attribute before passing it
  • The embedded parameter is auto-detected from parent_app, so explicit setting is usually unnecessary
  • The minimal fallback panel includes a set_user method to maintain API compatibility
  • If the panel has a _load_pending_approvals method, it will be called automatically when parent_app provides a current_user
  • Consider wrapping the panel creation in additional try-except blocks for critical applications

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_approval_panel 96.8% similar

    Factory function that creates and initializes an ApprovalPanel instance with error handling, supporting both standalone and embedded modes for document approval management.

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel_bis.py
  • function create_review_panel 79.5% similar

    Factory function that creates and initializes a ReviewPanel instance with error handling, supporting both standalone and embedded modes for document review management.

    From: /tf/active/vicechatdev/CDocs/ui/review_panel.py
  • class ApprovalPanel_v1 72.2% similar

    Approval management interface component

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel.py
  • class ApprovalPanel 71.3% similar

    Approval management interface component

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel_bis.py
  • function create_admin_panel 69.3% similar

    Factory function that creates and returns a configured AdminPanel instance with optional template, session management, parent application reference, and embedding mode.

    From: /tf/active/vicechatdev/CDocs/ui/admin_panel.py
← Back to Browse