🔍 Code Extractor

function create_user_tasks_panel

Maturity: 35

Factory function that creates and initializes a UserTasksPanel instance, optionally setting the current user from a parent application.

File:
/tf/active/vicechatdev/CDocs/ui/user_tasks_panel.py
Lines:
569 - 577
Complexity:
simple

Purpose

This function serves as a factory/constructor pattern to create a UserTasksPanel widget for displaying user-specific tasks. It handles initialization and automatically sets the user context if a parent application with a current_user attribute is provided. This is commonly used in dashboard or UI applications to display pending reviews, approvals, and other user-assigned tasks.

Source Code

def create_user_tasks_panel(parent_app=None):
    """Create and initialize a user tasks panel instance"""
    panel = UserTasksPanel(parent_app=parent_app)
    
    # 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.set_user(parent_app.current_user)
    
    return panel

Parameters

Name Type Default Kind
parent_app - None positional_or_keyword

Parameter Details

parent_app: Optional parent application object that may contain a 'current_user' attribute. If provided and contains a valid current_user, the panel will be initialized with that user's context. Defaults to None if not provided.

Return Value

Returns an initialized UserTasksPanel instance. The panel will have its user context set if parent_app contained a valid current_user, otherwise it returns an uninitialized panel that can have its user set later via the set_user() method.

Dependencies

  • panel
  • pandas
  • logging
  • datetime
  • typing
  • CDocs.models.user_extensions
  • CDocs.controllers.review_controller
  • CDocs.controllers.approval_controller
  • CDocs.controllers.document_controller
  • CDocs.config.permissions
  • traceback

Required Imports

import panel as pn
import pandas as pd
import logging
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional
from CDocs.models.user_extensions import DocUser
from CDocs.controllers.review_controller import get_user_assigned_reviews, get_document_review_cycles
from CDocs.controllers.approval_controller import get_user_pending_approvals, get_user_assigned_approvals, get_document_approval_cycles
from CDocs.controllers.document_controller import get_documents
from CDocs.config import permissions
import traceback

Usage Example

# Example 1: Create panel without parent app
panel = create_user_tasks_panel()

# Example 2: Create panel with parent app that has current_user
class MyApp:
    def __init__(self):
        self.current_user = DocUser(username='john_doe', id=123)

app = MyApp()
tasks_panel = create_user_tasks_panel(parent_app=app)

# Example 3: Create panel and set user manually later
panel = create_user_tasks_panel()
user = DocUser(username='jane_smith', id=456)
panel.set_user(user)

# Display in a Panel dashboard
import panel as pn
pn.extension('tabulator')
dashboard = pn.Column(tasks_panel)
dashboard.servable()

Best Practices

  • Always ensure the parent_app object has a valid current_user attribute before passing it to avoid initialization issues
  • If creating multiple panels, consider reusing the parent_app reference to maintain consistent user context
  • The UserTasksPanel class must be defined in the same module or imported before calling this function
  • Handle cases where parent_app is None by setting the user manually after panel creation using panel.set_user()
  • Ensure the CDocs database and models are properly initialized before creating the panel to avoid runtime errors when loading tasks

Similar Components

AI-powered semantic similarity - components with related functionality:

  • class UserTasksPanel 68.8% similar

    Panel showing pending tasks for the current user

    From: /tf/active/vicechatdev/CDocs/ui/user_tasks_panel.py
  • function create_admin_panel 62.8% 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
  • function create_approval_panel_v1 62.2% similar

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

    From: /tf/active/vicechatdev/CDocs/ui/approval_panel.py
  • function create_approval_panel 62.1% 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 62.1% 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
← Back to Browse