### Custom Prefix Configuration with Meta Class Source: https://context7.com/django-compressor/django-appconf/llms.txt This example shows how to define custom prefixes for application settings using an inner Meta class within AppConf. This allows for greater control over setting names, independent of the app's module structure. It illustrates overriding settings in the project's settings.py. ```python from appconf import AppConf class AcmeAppConf(AppConf): SETTING_1 = "default_value" API_KEY = "" MAX_RETRIES = 3 class Meta: prefix = 'acme' # Settings become: # settings.ACME_SETTING_1 = "default_value" # settings.ACME_API_KEY = "" # settings.ACME_MAX_RETRIES = 3 # Override in your project's settings.py: # ACME_API_KEY = "your-api-key-here" # ACME_MAX_RETRIES = 5 # Access in your code from django.conf import settings api_key = settings.ACME_API_KEY retries = settings.ACME_MAX_RETRIES ``` -------------------------------- ### Override Default Prefix in AppConf (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This example shows how to customize the prefix used for settings defined in an AppConf class. By specifying a 'prefix' within an inner 'Meta' class, you can control how these settings are referenced in Django's global settings. ```python from appconf import AppConf class AcmeAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) class Meta: prefix = 'acme' ``` -------------------------------- ### Specify Custom Settings Holder in AppConf (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This example demonstrates how to configure AppConf to use a different settings object than the default 'django.conf.settings'. This is achieved by setting the 'holder' attribute in the inner 'Meta' class to a dotted import path of the desired settings module. ```python from appconf import AppConf class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) class Meta: prefix = 'acme' holder = 'acme.conf.settings' ``` -------------------------------- ### Configure Callbacks for Setting Transformation and Validation Source: https://context7.com/django-compressor/django-appconf/llms.txt This snippet demonstrates how to use `configure_` callback methods within AppConf to transform or validate individual settings. These methods ensure data integrity and correct formatting before settings are applied, handling type conversions and custom validation logic. ```python from appconf import AppConf from django.core.exceptions import ImproperlyConfigured class MyAppConf(AppConf): DEBUG_MODE = False DATABASE_URL = "sqlite:///db.sqlite3" ALLOWED_HOSTS = [] def configure_debug_mode(self, value): # Ensure DEBUG_MODE is always a boolean if isinstance(value, str): return value.lower() in ('true', '1', 'yes') return bool(value) def configure_database_url(self, value): # Validate database URL format if not value.startswith(('postgresql://', 'mysql://', 'sqlite://')): raise ImproperlyConfigured( f"Invalid DATABASE_URL: {value}. Must start with " "postgresql://, mysql://, or sqlite://" ) return value def configure_allowed_hosts(self, value): # Convert comma-separated string to list if isinstance(value, str): return [host.strip() for host in value.split(',') if host.strip()] return list(value) # In settings.py: # MYAPP_DEBUG_MODE = "true" # Will be converted to True # MYAPP_ALLOWED_HOSTS = "example.com, api.example.com" # # Will become ["example.com", "api.example.com"] ``` -------------------------------- ### AppConf Inheritance and Customization in Python Source: https://context7.com/django-compressor/django-appconf/llms.txt Demonstrates how to inherit from BaseAppConf to customize or extend application settings. This pattern allows for overriding parent defaults and introducing new settings, facilitating modular configuration management. The 'Meta' class is used to define a prefix for the settings. ```python from appconf import BaseAppConf # Inherit and customize class ExtendedAppConf(BaseAppConf): SIMPLE_VALUE = False # Override parent default NEW_SETTING = "additional" class Meta: prefix = 'extended' # Multiple inheritance levels class SpecializedAppConf(ExtendedAppConf): ADVANCED_FEATURE = True def configure(self): configured = self.configured_data # Access inherited settings if configured['SIMPLE_VALUE']: configured['MODE'] = "simple" else: configured['MODE'] = "advanced" return configured class Meta: prefix = 'specialized' ``` -------------------------------- ### Custom Configuration Method - Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/reference.rst Shows how to create custom configuration methods for app settings. These methods receive the setting's value and must return the configured value. This allows for conditional logic or transformations based on other settings or environment. ```python class MyAppConf(AppConf): DEPLOYMENT_MODE = "dev" def configure_deployment_mode(self, value): if on_production(): value = "prod" return value ``` -------------------------------- ### Runtime Configuration with AppConf Instances in Python Source: https://context7.com/django-compressor/django-appconf/llms.txt Illustrates how to create AppConf instances with custom keyword arguments to dynamically set configuration values at runtime. Uppercase attributes of the instance are automatically propagated to Django's settings object with the specified prefix. Lowercase attributes are instance-specific and not propagated. ```python from appconf import AppConf from django.conf import settings class DynamicAppConf(AppConf): DEFAULT_TIMEOUT = 30 RETRY_COUNT = 3 class Meta: prefix = 'dynamic' # Create instance with custom values config = DynamicAppConf( DEFAULT_TIMEOUT=60, RETRY_COUNT=5, NEW_RUNTIME_VALUE="custom" ) # Values are set on the settings object print(settings.DYNAMIC_DEFAULT_TIMEOUT) # 60 print(settings.DYNAMIC_RETRY_COUNT) # 5 print(settings.DYNAMIC_NEW_RUNTIME_VALUE) # "custom" # Can also use setattr for uppercase attributes config.MAX_CONNECTIONS = 100 print(settings.DYNAMIC_MAX_CONNECTIONS) # 100 # Lowercase attributes are instance-only config.internal_state = "not propagated" # settings.internal_state does not exist ``` -------------------------------- ### Custom Configuration Handling with AppConf in Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/usage.rst Explains how to use the `configure` method in AppConf for advanced custom configuration, especially when settings depend on each other. It shows how to access configured data via `self.configured_data`. ```python from django.conf import settings from appconf import AppConf class MyCustomAppConf(AppConf): ENABLED = True MODE = 'development' def configure_enabled(self, value): return value and not settings.DEBUG def configure(self): mode = self.configured_data['MODE'] enabled = self.configured_data['ENABLED'] if not enabled and mode != 'development': print "WARNING: app not enabled in %s mode!" % mode return self.configured_data ``` -------------------------------- ### AppConf Meta Options - Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/reference.rst Illustrates the use of the inner Meta class within AppConf to specify configuration options. This includes setting a prefix for settings, defining required settings, specifying the settings holder, and enabling proxying. ```python class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) class Meta: proxy = False prefix = 'myapp' required = ['SETTING_3', 'SETTING_4'] holder = 'django.conf.settings' ``` -------------------------------- ### Define Default Settings with AppConf (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This snippet demonstrates how to define default settings for a Django app using the AppConf class. It shows setting simple string and tuple values. These defaults are automatically prefixed based on the app's label. ```python from appconf import AppConf class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) ``` -------------------------------- ### Define AppConf Settings - Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/reference.rst Demonstrates how to define application settings using the AppConf class. Settings are defined as class attributes. Tuples can be used for multi-line string settings. ```python class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) ``` -------------------------------- ### Instantiate AppConf Directly in Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/usage.rst Demonstrates how to directly instantiate an AppConf class to access application-specific settings without a prefix. This method is useful for accessing settings within your Django application. ```python from myapp.models import MyAppConf myapp_settings = MyAppConf() print myapp_settings.SETTING_1 ``` -------------------------------- ### Enforce Required Settings with Meta (Python) Source: https://context7.com/django-compressor/django-appconf/llms.txt Illustrates how to enforce that specific settings must be explicitly configured by the user using the `required` attribute within the `Meta` class of `AppConf`. If any of these required settings are missing, Django will raise an `ImproperlyConfigured` exception during startup. ```python from appconf import AppConf from django.core.exceptions import ImproperlyConfigured class MyAppConf(AppConf): API_KEY = None # Default is None, must be overridden SECRET_TOKEN = None BASE_URL = "http://localhost" # Optional with default TIMEOUT = 30 # Optional with default class Meta: required = ['API_KEY', 'SECRET_TOKEN'] # In settings.py - Missing required settings will cause startup failure: # MYAPP_API_KEY = "your-api-key" # MYAPP_SECRET_TOKEN = "your-secret-token" # Without these settings in settings.py, Django will fail to start with: # ImproperlyConfigured: The required setting MYAPP_API_KEY is missing. # Correct usage in settings.py: MYAPP_API_KEY = "sk_live_abc123xyz" MYAPP_SECRET_TOKEN = "token_def456uvw" MYAPP_TIMEOUT = 60 # Optional override ``` -------------------------------- ### Configuration Inheritance in AppConf (Python) Source: https://context7.com/django-compressor/django-appconf/llms.txt Demonstrates how to implement configuration inheritance in `AppConf` by having one `AppConf` class inherit from another. Child classes automatically inherit all settings, callbacks, and `Meta` options from their parent classes, allowing for extension and overriding of default configurations. ```python from appconf import AppConf # Base configuration class BaseAppConf(AppConf): SIMPLE_VALUE = True TIMEOUT = 30 def configure_timeout(self, value): return max(10, value) # Minimum 10 seconds ``` -------------------------------- ### Configure AppConf Setting Based on Global Settings in Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/usage.rst Demonstrates how to configure a setting based on global Django settings, such as `settings.DEBUG`, using a `configure_` method. This allows dynamic adjustment of settings. ```python from django.conf import settings from appconf import AppConf class MyCustomAppConf(AppConf): ENABLED = True def configure_enabled(self, value): return value and not settings.DEBUG ``` -------------------------------- ### Enable Proxy Mode for Unified Access (Python) Source: https://context7.com/django-compressor/django-appconf/llms.txt Explains how to enable proxy mode in `AppConf` using `Meta.proxy = True`. This mode allows access to all attributes of the settings holder (both custom and global Django settings) directly through the `AppConf` instance, providing a single interface for configuration retrieval. ```python from appconf import AppConf class ProxyAppConf(AppConf): CUSTOM_VALUE = "my_default" FEATURE_FLAG = True class Meta: proxy = True # Create instance config = ProxyAppConf() # Access custom settings (without prefix) print(config.CUSTOM_VALUE) # "my_default" print(config.FEATURE_FLAG) # True # Access any Django settings through proxy print(config.DEBUG) # Same as settings.DEBUG print(config.INSTALLED_APPS) # Same as settings.INSTALLED_APPS print(config.SECRET_KEY) # Same as settings.SECRET_KEY # Still accessible with prefix via settings from django.conf import settings print(settings.TESTS_CUSTOM_VALUE) # "my_default" ``` -------------------------------- ### Enable Proxy for AppConf in Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/usage.rst Shows how to enable proxy behavior for an AppConf class by setting `proxy = True` in the inner `Meta` class. This allows AppConf instances to act as proxies for global settings, enabling checks against `INSTALLED_APPS`. ```python from appconf import AppConf class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) class Meta: proxy = True myapp_settings = MyAppConf() if "myapp" in myapp_settings.INSTALLED_APPS: print "yay, myapp is installed!" ``` -------------------------------- ### Override configure() Method for Complex Logic (Python) Source: https://context7.com/django-compressor/django-appconf/llms.txt Demonstrates overriding the `configure()` method in `AppConf` to implement complex configuration logic. This method allows access to all configured data and dynamic addition of new settings, including computed or environment-based values. It takes configured data as input and returns the modified configuration. ```python from appconf import AppConf import os class MyAppConf(AppConf): BASE_URL = "http://localhost:8000" API_VERSION = "v1" ENABLE_CACHE = False CACHE_TTL = 300 def configure(self): # Access configured data configured = self.configured_data # Compute derived settings base_url = configured['BASE_URL'].rstrip('/') api_version = configured['API_VERSION'] configured['API_ENDPOINT'] = f"{base_url}/api/{api_version}" # Add environment-based configuration if os.getenv('PRODUCTION'): configured['ENABLE_CACHE'] = True configured['CACHE_TTL'] = 3600 # Add computed settings configured['FULL_CONFIG'] = { 'endpoint': configured['API_ENDPOINT'], 'cache': { 'enabled': configured['ENABLE_CACHE'], 'ttl': configured['CACHE_TTL'] } } return configured # Results in settings: # settings.MYAPP_BASE_URL = "http://localhost:8000" # settings.MYAPP_API_VERSION = "v1" # settings.MYAPP_API_ENDPOINT = "http://localhost:8000/api/v1" # settings.MYAPP_ENABLE_CACHE = False (or True in production) # settings.MYAPP_CACHE_TTL = 300 (or 3600 in production) # settings.MYAPP_FULL_CONFIG = {...} ``` -------------------------------- ### Define App Configuration with AppConf Base Class Source: https://context7.com/django-compressor/django-appconf/llms.txt This snippet demonstrates how to define default application settings using the AppConf base class. Settings are automatically prefixed with the app label and can be accessed via Django's settings object. It shows basic attribute assignment for settings. ```python from appconf import AppConf # Define configuration in your app's models.py class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = "two" TIMEOUT = 30 ENABLE_FEATURE = True # Settings are automatically available as: # settings.MYAPP_SETTING_1 = "one" # settings.MYAPP_SETTING_2 = "two" # settings.MYAPP_TIMEOUT = 30 # settings.MYAPP_ENABLE_FEATURE = True # Usage in your views or other modules from django.conf import settings def my_view(request): timeout_value = settings.MYAPP_TIMEOUT if settings.MYAPP_ENABLE_FEATURE: # Feature-specific logic pass return HttpResponse(f"Timeout is {timeout_value}") ``` -------------------------------- ### Override AppConf Settings Programmatically in Python Source: https://github.com/django-compressor/django-appconf/blob/develop/docs/usage.rst Illustrates how to programmatically override AppConf settings by passing values during the instantiation of the AppConf class. This allows for dynamic configuration changes. ```python from myapp.models import MyAppConf myapp_settings = MyAppConf(SETTING_1='something completely different') if 'different' in myapp_settings.SETTING_1: print "yay, I'm different!" ``` -------------------------------- ### Use Custom Settings Holder (Python) Source: https://context7.com/django-compressor/django-appconf/llms.txt Shows how to direct `AppConf` to use a custom settings object instead of Django's global `settings` by specifying a `holder` attribute in the `Meta` class. This is beneficial for creating isolated configuration namespaces or for testing purposes. ```python from appconf import AppConf # Define a custom settings holder class CustomSettings: EXISTING_VALUE = "preset" custom_settings = CustomSettings() # Configure AppConf to use custom holder class MyAppConf(AppConf): SIMPLE_VALUE = True CUSTOM_SETTING = "default" class Meta: holder = 'myapp.config.custom_settings' prefix = 'myapp' # Settings are applied to custom_settings instead of django.conf.settings: # custom_settings.MYAPP_SIMPLE_VALUE = True # custom_settings.MYAPP_CUSTOM_SETTING = "default" # Usage: from myapp.config import custom_settings print(custom_settings.MYAPP_SIMPLE_VALUE) # True print(custom_settings.EXISTING_VALUE) # "preset" ``` -------------------------------- ### Import AppConf Settings in App Module (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This code shows the recommended practice for reusable Django apps: placing AppConf subclasses in a 'conf.py' file and importing Django's settings from there. Other modules within the app can then import settings from this custom module. ```python from django.conf import settings from appconf import AppConf class MyAppConf(AppConf): SETTING_1 = "one" SETTING_2 = ( "two", ) ``` -------------------------------- ### Dynamic Object Import Utility in Python Source: https://context7.com/django-compressor/django-appconf/llms.txt Shows how to use the `import_attribute` utility function to dynamically import Python objects (modules, classes, functions) from string paths. It supports optional exception handlers for graceful error management when an import fails, returning `None` or a custom handler's return value. ```python from appconf.utils import import_attribute # Basic usage - import Django settings settings = import_attribute('django.conf.settings') print(settings.DEBUG) # Import a class AppConf = import_attribute('appconf.AppConf') # Import a function get_user_model = import_attribute('django.contrib.auth.get_user_model') User = get_user_model() # With exception handler for graceful error handling def handle_import_error(path, exctype, excvalue, tb): print(f"Failed to import {path}: {excvalue}") return None result = import_attribute( 'non.existent.module.attribute', exception_handler=handle_import_error ) # Prints: Failed to import non.existent.module.attribute: No module named 'non' # Returns: None # Used internally by AppConf for custom holders holder = import_attribute('myapp.settings.custom_holder') ``` -------------------------------- ### Override AppConf Setting in Django Settings (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This snippet illustrates how to override a default setting defined by an AppConf class within your Django project's main settings file. The setting is accessed using the prefixed name (e.g., MYAPP_SETTING_1 or ACME_SETTING_1). ```python ACME_SETTING_1 = "uno" ``` -------------------------------- ### Access AppConf Settings in Django View (Python) Source: https://github.com/django-compressor/django-appconf/blob/develop/README.rst This snippet demonstrates how to access settings managed by django-appconf within a Django view. It imports the custom settings object (e.g., from 'myapp.conf') and then uses the prefixed setting name to retrieve its value. ```python from django.http import HttpResponse from myapp.conf import settings def index(request): text = 'Setting 1 is: %s' % settings.MYAPP_SETTING_1 return HttpResponse(text) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.