function get_status_color
Retrieves the color code associated with a given document status code from a configuration dictionary, returning a default gray color if the status is not found.
/tf/active/vicechatdev/CDocs/settings_prod.py
784 - 788
simple
Purpose
This function provides a centralized way to map document status codes to their corresponding color representations (hex color codes). It's typically used in UI rendering to visually distinguish different document statuses. The function ensures consistent color coding across the application by referencing a global DOCUMENT_STATUS_CONFIG dictionary and provides a fallback default color for unknown statuses.
Source Code
def get_status_color(status_code):
"""Get the color for a status code."""
if status_code in DOCUMENT_STATUS_CONFIG:
return DOCUMENT_STATUS_CONFIG[status_code].get('color', '#6c757d')
return '#6c757d' # Default gray
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
status_code |
- | - | positional_or_keyword |
Parameter Details
status_code: The status code identifier for a document. This can be any hashable type (typically string or integer) that serves as a key in the DOCUMENT_STATUS_CONFIG dictionary. Examples might include 'draft', 'published', 'archived', or numeric codes like 1, 2, 3 depending on the application's status system.
Return Value
Returns a string representing a hex color code (e.g., '#6c757d'). If the status_code exists in DOCUMENT_STATUS_CONFIG and has a 'color' key, that color value is returned. Otherwise, returns '#6c757d' (a gray color) as the default. The return type is always a string.
Usage Example
# First, define the required configuration
DOCUMENT_STATUS_CONFIG = {
'draft': {'color': '#ffc107', 'label': 'Draft'},
'published': {'color': '#28a745', 'label': 'Published'},
'archived': {'color': '#6c757d', 'label': 'Archived'}
}
# Now use the function
color = get_status_color('draft')
print(color) # Output: '#ffc107'
color = get_status_color('published')
print(color) # Output: '#28a745'
color = get_status_color('unknown_status')
print(color) # Output: '#6c757d' (default gray)
Best Practices
- Ensure DOCUMENT_STATUS_CONFIG is properly initialized before calling this function to avoid NameError
- The DOCUMENT_STATUS_CONFIG dictionary should be defined as a module-level constant for consistency
- Consider validating that color values in DOCUMENT_STATUS_CONFIG are valid hex color codes
- The function is safe to call with any status_code value as it always returns a valid color (default fallback)
- For type safety, consider adding type hints: def get_status_color(status_code: str) -> str:
- If DOCUMENT_STATUS_CONFIG is large or frequently accessed, ensure it's efficiently structured (dictionary lookup is O(1))
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_status_name 72.8% similar
-
function get_document_status_code 72.0% similar
-
function is_valid_document_status 64.7% similar
-
function get_version 54.5% similar
-
function get_next_status 54.1% similar