Smarter Waffle

smarter.lib.django.waffle

Enhanced, managed Django-waffle wrapper with short-lived Redis-based caching and database readiness checks.

Features:

  • Caching: Integrates short-lived Redis-based caching to optimize feature flag (switch) checks.

  • Database Readiness Handling: Implements safeguards to prevent errors when the database is not ready.

  • Feature Flag Management: Centralized mechanism to check if a feature flag (switch) is active.

  • Custom Django Admin: Customized Django Admin class for managing waffle switches.

  • Fixed Set of Switches: Defines a fixed set of waffle switches for the Smarter API.

Important

These are managed feature flags; add any new switches to the SmarterWaffleSwitches class. These switches are verified duing deployments to ensure that they exist in the database. Missing switches are automatically created with a default inactive state.

Important

django-waffle relies on database tables as well as Redis for storing and caching feature flags. If the database is not ready, waffle switch values will default to inactive (False) to prevent application errors.

Example

from smarter.lib.django.waffle import switch_is_active

if switch_is_active(SmarterWaffleSwitches.ACCOUNT_LOGGING):
    print("API logging is enabled.")

Dependencies:

class smarter.lib.django.waffle.SmarterSwitchAdmin(model, admin_site)[source]

Bases: SwitchAdmin

Customized Django Admin console for managing Waffle switches. This class restricts access to the module to superusers only.

has_module_permission(request)[source]

Return True if the given request has any permission in the given app label.

Can be overridden by the user in subclasses. In such case it should return True if the given request has permission to view the module on the admin index page and access the module’s index page. Overriding it does not restrict access to the add, change or delete views. Use ModelAdmin.has_(add|change|delete)_permission for that.

property media
ordering = ('name',)
class smarter.lib.django.waffle.SmarterWaffleSwitch(name, comment, default)[source]

Bases: object

__init__(name, comment, default)
comment: str
default: bool
name: str
to_json()[source]
Return type:

dict[str, str | bool]

class smarter.lib.django.waffle.SmarterWaffleSwitches[source]

Bases: object

Enumerated data type for predefined, managed Smarter waffle switches.

This class defines the fixed set of feature flags (Waffle switches) used by the Smarter Platform. Each class attribute represents a unique, centrally managed switch. These switches are automatically verified and created (if missing) during deployments, ensuring consistency and preventing runtime errors due to missing flags.

Note

Only switches defined in this class are considered valid for use in the Smarter codebase. To add a new feature flag, declare it as a class attribute here.

Example usage:

from smarter.lib.django.waffle import SmarterWaffleSwitches, switch_is_active

if switch_is_active(SmarterWaffleSwitches.API_LOGGING):
    print("API logging is enabled.")
ACCOUNT_LOGGING = 'log_account'

Enables logging throughout the smarter.app.account namespace.

ACCOUNT_MIXIN_LOGGING = 'log_account_mixin'

Enables logging within the smarter.apps.account.mixins.AccountMixin class.

ALLOW_API_GET = 'allow_api_get'

Allows GET requests to the API endpoints, which are normally restricted to POST requests.

API_LOGGING = 'log_api'

Enables logging throughout the smarter.api namespace.

CACHE_LOGGING = 'log_caching'

Enables detailed logging for caching operations including cache hits, misses, and errors.

CHATAPP_LOGGING = 'log_chatapp'

For the React Chat UI component. Enables debug-level javascript console logging inside the browser

CHATBOT_HELPER_LOGGING = 'log_chatbothelper'

Enables logging within the smarter.apps.chatbot.model.ChatBotHelper class.

CHATBOT_LOGGING = 'log_chatbot'

Enables logging throughout the smarter.app.chatbot namespace.

CONNECTION_LOGGING = 'log_connection'

Enables logging throughout the smarter.app.connection namespace.

CSRF_SUPPRESS_FOR_CHATBOTS = 'disable_csrf_middleware_for_chatbots'

Disables CSRF middleware checks for chat completion endpoints.

ENABLE_ACCOUNT_REGISTRATION = 'enable_account_registration'

Enables account registration link.

ENABLE_DEBUG_MODE = 'enable_debug_mode'

Enables debug mode for the entire Smarter application, which may include additional logging and diagnostic information.

ENABLE_FILE_DROP_ZONE = 'enable_file_drop_zone'
ENABLE_JOURNAL = 'enable_journal'

Enables the Smarter Journal feature.

Enables additional links in the login page footer, such as ‘Legal’ and ‘Contact’.

ENABLE_LOG_VIEW_IN_BROWSER = 'enable_log_view_in_browser'

Enables the ‘View Logs in Browser’ feature for easier access to log files during development and debugging.

ENABLE_MIDDLEWARE_CORS = 'enable_middleware_cors'

Enables SmarterCorsMiddleware

ENABLE_MIDDLEWARE_EXCESSIVE_404 = 'enable_middleware_block_excessive_404'

Enables SmarterBlockExcessive404Middleware

ENABLE_MIDDLEWARE_SECURITY = 'enable_middleware_security'

Enables SmarterSecurityMiddleware

ENABLE_MIDDLEWARE_SENSITIVE_FILES = 'enable_middleware_block_sensitive_files'

Enables SmarterBlockSensitiveFilesMiddleware

ENABLE_MULTITENANT_AUTHENTICATION = 'enable_multitenant_authentication'

Enables multi-tenant authentication support for hosted Smarter platforms.

ENABLE_NEW_USER_PASSWORD_EMAIL = 'enable_new_user_password_email'

Enables sending textemail with password to new users.

ENABLE_OAUTH2 = 'enable_oauth2'

Enables OAuth2 authentication support.

ENABLE_REACTAPP_DEBUG_MODE = 'enable_reactapp_debug_mode'

Enables React app debug mode within the Smarter React Chat component.

ENABLE_SMARTER_PAGE_CACHING = 'enable_smarter_page_caching'

Enables the Smarter user-based page caching decorator for user-facing pages to improve performance.

ENABLE_TERMINAL_APP = 'enable_terminal_emulator_app'

Enables the terminal emulator feature.

ENABLE_VECTORSTORE = 'enable_vectorstore'

Enables the vectorstore feature, which may include support for vector databases and related functionality.

MANIFEST_LOGGING = 'log_manifest_brokers'

Enables detailed diagnostic logging for manifest initialization, validation and brokered operations.

MIDDLEWARE_LOGGING = 'log_middleware'

Enables detailed diagnostic logging for all middleware operations.

PLUGIN_LOGGING = 'log_plugin'

Enables logging throughout the smarter.app.plugin namespace.

PROMPT_LOGGING = 'log_prompt'

Enables logging throughout the smarter.app.prompt namespace.

PROVIDER_LOGGING = 'log_provider'

Enables logging throughout the smarter.app.provider namespace.

RECEIVER_LOGGING = 'log_receivers'

Enables logging in all Django signal receivers throughout the Smarter codebase.

REQUEST_MIXIN_LOGGING = 'log_request_mixin'

Enables detailed diagnostic logging for the SmarterRequestMixin class.

SECRET_LOGGING = 'log_secret'

Enables logging throughout the smarter.app.secret namespace.

TASK_LOGGING = 'log_tasks'

Enables logging in all Celery tasks throughout the Smarter codebase.

VALIDATOR_LOGGING = 'log_validators'

Enables logging in all Django model field validators throughout the Smarter codebase.

VECTORSTORE_LOGGING = 'log_vectorstore'

Enables logging throughout the smarter.app.vectorstore namespace.

VIEW_LOGGING = 'log_views'

Enables logging in all Django views throughout the Smarter codebase.

property all

Return all switches.

switches = {'allow_api_get': SmarterWaffleSwitch(name='allow_api_get', comment='Allows GET requests to the API endpoints, which are normally restricted to POST requests.', default=False), 'disable_csrf_middleware_for_chatbots': SmarterWaffleSwitch(name='disable_csrf_middleware_for_chatbots', comment='Disables CSRF middleware checks for chat completion endpoints.', default=False), 'enable_account_registration': SmarterWaffleSwitch(name='enable_account_registration', comment='Enables account registration link.', default=False), 'enable_debug_mode': SmarterWaffleSwitch(name='enable_debug_mode', comment='Enables debug mode for the entire Smarter application, which may include additional logging and diagnostic information.', default=False), 'enable_file_drop_zone': SmarterWaffleSwitch(name='enable_file_drop_zone', comment='Enables the file drop zone feature in the authenticated dashboard.', default=False), 'enable_journal': SmarterWaffleSwitch(name='enable_journal', comment='Enables the Smarter Journal feature.', default=False), 'enable_log_view_in_browser': SmarterWaffleSwitch(name='enable_log_view_in_browser', comment="Enables the 'View Logs in Browser' feature for easier access to log files during development and debugging.", default=False), 'enable_login_footer_links': SmarterWaffleSwitch(name='enable_login_footer_links', comment="Enables additional links in the login page footer, such as 'Legal' and 'Contact'.", default=False), 'enable_middleware_block_excessive_404': SmarterWaffleSwitch(name='enable_middleware_block_excessive_404', comment='Enables SmarterBlockExcessive404Middleware', default=False), 'enable_middleware_block_sensitive_files': SmarterWaffleSwitch(name='enable_middleware_block_sensitive_files', comment='Enables SmarterBlockSensitiveFilesMiddleware', default=False), 'enable_middleware_cors': SmarterWaffleSwitch(name='enable_middleware_cors', comment='Enables SmarterCorsMiddleware', default=True), 'enable_middleware_security': SmarterWaffleSwitch(name='enable_middleware_security', comment='Enables SmarterSecurityMiddleware', default=False), 'enable_multitenant_authentication': SmarterWaffleSwitch(name='enable_multitenant_authentication', comment='Enables multi-tenant authentication support for hosted Smarter platforms.', default=False), 'enable_new_user_password_email': SmarterWaffleSwitch(name='enable_new_user_password_email', comment='Enables sending textemail with password to new users.', default=False), 'enable_oauth2': SmarterWaffleSwitch(name='enable_oauth2', comment='Enables OAuth2 authentication support.', default=False), 'enable_reactapp_debug_mode': SmarterWaffleSwitch(name='enable_reactapp_debug_mode', comment='Enables React app debug mode within the Smarter React Chat component.', default=True), 'enable_smarter_page_caching': SmarterWaffleSwitch(name='enable_smarter_page_caching', comment='Enables the Smarter user-based page caching decorator for user-facing pages to improve performance.', default=True), 'enable_terminal_emulator_app': SmarterWaffleSwitch(name='enable_terminal_emulator_app', comment='Enables the terminal emulator feature in the authenticated dashboard.', default=False), 'enable_vectorstore': SmarterWaffleSwitch(name='enable_vectorstore', comment='Enables the Smarter Vectorstore Django app, which may includes support for vector databases. Requires app restart.', default=False), 'log_account': SmarterWaffleSwitch(name='log_account', comment='Enables logging throughout the smarter.app.account namespace.', default=True), 'log_account_mixin': SmarterWaffleSwitch(name='log_account_mixin', comment='Enables logging within the smarter.apps.account.mixins.AccountMixin class.', default=False), 'log_api': SmarterWaffleSwitch(name='log_api', comment='Enables logging throughout the smarter.api namespace.', default=True), 'log_caching': SmarterWaffleSwitch(name='log_caching', comment='Enables detailed logging for caching operations including cache hits, misses, and errors.', default=True), 'log_chatapp': SmarterWaffleSwitch(name='log_chatapp', comment='For the React Chat UI component. Enables debug-level javascript console logging inside the browser', default=True), 'log_chatbot': SmarterWaffleSwitch(name='log_chatbot', comment='Enables logging throughout the smarter.app.chatbot namespace.', default=True), 'log_chatbothelper': SmarterWaffleSwitch(name='log_chatbothelper', comment='Enables logging within the smarter.apps.chatbot.model.ChatBotHelper class.', default=True), 'log_connection': SmarterWaffleSwitch(name='log_connection', comment='Enables logging throughout the smarter.app.connection namespace.', default=True), 'log_manifest_brokers': SmarterWaffleSwitch(name='log_manifest_brokers', comment='Enables detailed diagnostic logging for manifest initialization, validation and brokered operations.', default=True), 'log_middleware': SmarterWaffleSwitch(name='log_middleware', comment='Enables detailed diagnostic logging for all middleware operations.', default=False), 'log_plugin': SmarterWaffleSwitch(name='log_plugin', comment='Enables logging throughout the smarter.app.plugin namespace.', default=True), 'log_prompt': SmarterWaffleSwitch(name='log_prompt', comment='Enables logging throughout the smarter.app.prompt namespace.', default=True), 'log_provider': SmarterWaffleSwitch(name='log_provider', comment='Enables logging throughout the smarter.app.provider namespace.', default=True), 'log_receivers': SmarterWaffleSwitch(name='log_receivers', comment='Enables logging in all Django signal receivers throughout the Smarter codebase.', default=True), 'log_request_mixin': SmarterWaffleSwitch(name='log_request_mixin', comment='Enables detailed diagnostic logging for the SmarterRequestMixin class.', default=False), 'log_secret': SmarterWaffleSwitch(name='log_secret', comment='Enables logging throughout the smarter.app.secret namespace.', default=True), 'log_tasks': SmarterWaffleSwitch(name='log_tasks', comment='Enables logging in all Celery tasks throughout the Smarter codebase.', default=True), 'log_validators': SmarterWaffleSwitch(name='log_validators', comment='Enables logging in all Django model field validators throughout the Smarter codebase.', default=False), 'log_vectorstore': SmarterWaffleSwitch(name='log_vectorstore', comment='Enables logging throughout the smarter.app.vectorstore namespace.', default=True), 'log_views': SmarterWaffleSwitch(name='log_views', comment='Enables logging in all Django views throughout the Smarter codebase.', default=True)}
smarter.lib.django.waffle.smarter_waffle_switches = <smarter.lib.django.waffle.SmarterWaffleSwitches object>

Singleton instance of SmarterWaffleSwitches to be used throughout the codebase.

smarter.lib.django.waffle.switch_is_active(switch_name)[source]

Check if a Waffle switch is active, with caching and database readiness checks.

Important

This is the preferred method for checking Waffle switches in the Smarter codebase. It includes caching to optimize performance and handles database readiness to prevent errors.

Example

from smarter.lib.django.waffle import SmarterWaffleSwitches, switch_is_active

if switch_is_active(SmarterWaffleSwitches.API_LOGGING):
    print("API logging is enabled.")
Parameters:

switch_name (str) – The name of the Waffle switch to check.

Returns:

True if the switch is active, False otherwise.

Return type:

bool