🔍 Code Extractor

function is_valid_status_transition

Maturity: 36

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

File:
/tf/active/vicechatdev/CDocs/settings_prod.py
Lines:
780 - 782
Complexity:
simple

Purpose

This function serves as a validation gate for state machine transitions, checking if a proposed status change is permitted according to the VALID_STATUS_TRANSITIONS dictionary. It's commonly used in workflow systems, order processing, task management, or any system that requires controlled state transitions to maintain data integrity and business logic.

Source Code

def is_valid_status_transition(from_status, to_status):
    """Check if a status transition is valid."""
    return to_status in VALID_STATUS_TRANSITIONS.get(from_status, [])

Parameters

Name Type Default Kind
from_status - - positional_or_keyword
to_status - - positional_or_keyword

Parameter Details

from_status: The current/source status from which the transition is being attempted. Expected to be a string or enum value that exists as a key in the VALID_STATUS_TRANSITIONS dictionary. Represents the starting state in the transition.

to_status: The target/destination status to which the transition is being attempted. Expected to be a string or enum value that should exist in the list of valid transitions for the from_status. Represents the desired end state in the transition.

Return Value

Returns a boolean value: True if the transition from from_status to to_status is valid (i.e., to_status exists in the list of allowed transitions for from_status in VALID_STATUS_TRANSITIONS), False otherwise. If from_status doesn't exist in VALID_STATUS_TRANSITIONS, returns False by default due to the .get() method returning an empty list.

Usage Example

# Define the valid transitions dictionary
VALID_STATUS_TRANSITIONS = {
    'draft': ['pending', 'cancelled'],
    'pending': ['approved', 'rejected'],
    'approved': ['completed'],
    'rejected': ['draft']
}

# Check if transition is valid
if is_valid_status_transition('draft', 'pending'):
    print('Transition allowed')
    # Proceed with status update
else:
    print('Invalid transition')
    # Handle error or reject the transition

# Example with invalid transition
result = is_valid_status_transition('approved', 'draft')
print(f'Can transition from approved to draft: {result}')  # False

# Example with non-existent from_status
result = is_valid_status_transition('unknown', 'pending')
print(f'Unknown status transition: {result}')  # False

Best Practices

  • Always define VALID_STATUS_TRANSITIONS before calling this function to avoid runtime errors
  • Use consistent data types for status values (all strings or all enums) throughout your application
  • Consider using Enum types for status values to prevent typos and improve type safety
  • Call this function before performing any status update to ensure data integrity
  • Log invalid transition attempts for debugging and auditing purposes
  • Consider raising exceptions or returning error messages instead of just False for better error handling
  • Document all valid status transitions in your VALID_STATUS_TRANSITIONS dictionary with comments
  • If using enums, ensure both from_status and to_status are of the same enum type or properly converted

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_version 74.1% similar

    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.

    From: /tf/active/vicechatdev/CDocs/models/document_status.py
  • function is_valid_document_status 66.9% similar

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

    From: /tf/active/vicechatdev/CDocs/settings_prod.py
  • function get_next_status 58.4% 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 get_status_after_approval 56.8% 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_editable_status 56.2% 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