function create_user_tasks_panel
Factory function that creates and initializes a UserTasksPanel instance, optionally setting the current user from a parent application.
/tf/active/vicechatdev/CDocs/ui/user_tasks_panel.py
569 - 577
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
panelpandasloggingdatetimetypingCDocs.models.user_extensionsCDocs.controllers.review_controllerCDocs.controllers.approval_controllerCDocs.controllers.document_controllerCDocs.config.permissionstraceback
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class UserTasksPanel 68.8% similar
-
function create_admin_panel 62.8% similar
-
function create_approval_panel_v1 62.2% similar
-
function create_approval_panel 62.1% similar
-
function create_review_panel 62.1% similar