🔍 Code Extractor

function get_version

Maturity: 53

Returns a list of valid status codes that a document can transition to from its current status, using either configured transitions from settings or predefined fallback rules.

File:
/tf/active/vicechatdev/CDocs/models/document_status.py
Lines:
97 - 134
Complexity:
simple

Purpose

This function implements a state machine for document status transitions. It ensures that documents can only move between valid states (e.g., DRAFT → IN_REVIEW → APPROVED → PUBLISHED). It first checks for custom transition rules in the application settings, then falls back to hardcoded transition rules if settings are unavailable. This is useful for workflow management systems, document lifecycle management, and enforcing business rules around status changes.

Source Code

def get_valid_transitions(current_status: str) -> list:
    """
    Get all valid status transitions from current status
    
    Parameters
    ----------
    current_status : str
        Current document status
        
    Returns
    -------
    list
        List of valid status codes to which the document can transition
    """
    # Convert to uppercase for consistency with settings
    upper_status = current_status.upper()
    
    # Use the valid transitions from settings
    if hasattr(settings, 'VALID_STATUS_TRANSITIONS') and upper_status in settings.VALID_STATUS_TRANSITIONS:
        return settings.VALID_STATUS_TRANSITIONS[upper_status]
    
    # Fallback to predefined transitions if settings not available
    if upper_status == STATUS_DRAFT:
        return [STATUS_IN_REVIEW, STATUS_APPROVED, STATUS_PUBLISHED, STATUS_ARCHIVED]
    elif upper_status == STATUS_IN_REVIEW:
        return [STATUS_DRAFT, STATUS_IN_APPROVAL, STATUS_ARCHIVED]
    elif upper_status == STATUS_IN_APPROVAL:
        return [STATUS_DRAFT, STATUS_APPROVED, STATUS_ARCHIVED]
    elif upper_status == STATUS_APPROVED:
        return [STATUS_DRAFT, STATUS_IN_REVIEW, STATUS_PUBLISHED, STATUS_ARCHIVED]
    elif upper_status == STATUS_PUBLISHED:
        return [STATUS_EFFECTIVE, STATUS_ARCHIVED, STATUS_OBSOLETE]
    elif upper_status == STATUS_EFFECTIVE:
        return [STATUS_ARCHIVED, STATUS_OBSOLETE]
    elif upper_status == STATUS_ARCHIVED:
        return [STATUS_DRAFT]
    else:  # OBSOLETE or unknown
        return []

Parameters

Name Type Default Kind
current_status str - positional_or_keyword

Parameter Details

current_status: The current status of the document as a string. Can be in any case (will be converted to uppercase internally). Expected values include status codes like 'DRAFT', 'IN_REVIEW', 'IN_APPROVAL', 'APPROVED', 'PUBLISHED', 'EFFECTIVE', 'ARCHIVED', 'OBSOLETE', or any custom status defined in settings.VALID_STATUS_TRANSITIONS.

Return Value

Type: list

Returns a list of strings representing valid status codes that the document can transition to from the current status. Returns an empty list if the current status is 'OBSOLETE', unknown, or has no valid transitions defined. Each element in the list is a status code constant (e.g., ['STATUS_IN_REVIEW', 'STATUS_APPROVED']).

Dependencies

  • CDocs.config

Required Imports

from CDocs.config import settings

Usage Example

# Assuming status constants are defined
STATUS_DRAFT = 'DRAFT'
STATUS_IN_REVIEW = 'IN_REVIEW'
STATUS_APPROVED = 'APPROVED'
STATUS_PUBLISHED = 'PUBLISHED'
STATUS_ARCHIVED = 'ARCHIVED'
STATUS_IN_APPROVAL = 'IN_APPROVAL'
STATUS_EFFECTIVE = 'EFFECTIVE'
STATUS_OBSOLETE = 'OBSOLETE'

from CDocs.config import settings

# Get valid transitions from DRAFT status
valid_transitions = get_valid_transitions('draft')
print(valid_transitions)
# Output: ['IN_REVIEW', 'APPROVED', 'PUBLISHED', 'ARCHIVED']

# Check if a specific transition is valid
current = 'IN_REVIEW'
target = 'APPROVED'
if target in get_valid_transitions(current):
    print(f"Can transition from {current} to {target}")

# Handle unknown status
transitions = get_valid_transitions('UNKNOWN_STATUS')
print(transitions)  # Output: []

Best Practices

  • Always check if the returned list is empty before attempting a status transition to handle unknown or terminal statuses
  • The function is case-insensitive for input but returns status codes in the format defined by the constants or settings
  • Consider defining settings.VALID_STATUS_TRANSITIONS for production use to centralize transition rules and make them configurable
  • Ensure all status constants (STATUS_DRAFT, STATUS_IN_REVIEW, etc.) are properly defined in the module scope before calling this function
  • Use this function before attempting any status change to validate the transition is allowed
  • The fallback logic provides a safety net but should not be relied upon in production; always configure settings.VALID_STATUS_TRANSITIONS

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_next_status 75.6% similar

    Determines the next logical status in a document lifecycle workflow based on the current status, using either configured transitions or a predefined fallback flow.

    From: /tf/active/vicechatdev/CDocs/models/document_status.py
  • function is_valid_status_transition 74.1% similar

    Validates whether a transition from one status to another is allowed based on predefined valid status transitions.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_status_after_approval 61.4% similar

    Determines the next status for a controlled document based on an approval decision (APPROVED or REJECTED), transitioning documents through their lifecycle workflow.

    From: /tf/active/vicechatdev/CDocs/controllers/document_controller.py
  • function is_valid_document_status 60.9% similar

    Validates whether a given status code exists in the DOCUMENT_STATUS_CONFIG configuration.

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function is_editable_status 58.0% similar

    Validates whether a given document status string allows editing by checking it against a predefined list of editable statuses.

    From: /tf/active/vicechatdev/CDocs/models/document_status.py
← Back to Browse