Configuration
The openviper.conf package provides OpenViper’s typed settings system.
Settings are declared as frozen dataclasses, loaded from an environment variable,
and can be overridden by .env files or environment variables at runtime.
Overview
Settings are defined by subclassing Settings and decorating
the subclass with @dataclasses.dataclass(frozen=True). The framework loads
settings from the module pointed to by the OPENVIPER_SETTINGS_MODULE environment
variable.
An alternative programmatic configuration path is available via
settings.configure(obj) for tests and embedded use-cases.
Key Classes & Functions
- class openviper.conf.Settings
Base frozen dataclass for all project settings. Subclass and decorate with
@dataclasses.dataclass(frozen=True)to customise for your project. All fields are immutable after construction.Project
Field
Type
Default / Notes
PROJECT_NAMEstr"OpenViper Application"VERSIONstrFramework version (auto-set)
DEBUGboolTrue- setFalsein productionALLOWED_HOSTStuple[str, ...]("localhost", "127.0.0.1")INSTALLED_APPStuple[str, ...]()- dotted paths to app modulesUSE_TZboolTrueTIME_ZONEstr"UTC"MIDDLEWAREtuple[str, ...]Security, CORS, Session, Auth middleware by default
Admin Panel
Field
Type
Default / Notes
ADMIN_TITLEstr"OpenViper Admin"ADMIN_HEADER_TITLEstr"OpenViper"ADMIN_FOOTER_TITLEstr"OpenViper Admin"Security
Field
Type
Default / Notes
SECRET_KEYstr""- must be set via env var in productionSECURE_SSL_REDIRECTboolFalseSECURE_HSTS_SECONDSint0- seconds for HSTS headerSECURE_HSTS_INCLUDE_SUBDOMAINSboolFalseSECURE_HSTS_PRELOADboolFalseSECURE_COOKIESboolFalseX_FRAME_OPTIONSstr"DENY"SECURE_BROWSER_XSS_FILTERboolFalse- deprecated; use CSP insteadSECURE_CONTENT_SECURITY_POLICYConfigMap | NoneNone- CSP header directives dictDatabase
Field
Type
Default / Notes
DATABASESConfigMapMulti-database config; each alias maps to a dict with
OPTIONS(URL,ECHO,POOL_SIZE,MAX_OVERFLOW,POOL_RECYCLE,POOL_TIMEOUT).BACKENDis optional, defaults toDefaultDatabaseBackend. Top-levelROUTERSandROUTINGkeys control routing.Cache
Field
Type
Default / Notes
CACHESConfigMapAlias-keyed cache backend config with
BACKENDandOPTIONS(see Cache Framework)Authentication & Session
Field
Type
Default / Notes
PASSWORD_HASHERStuple[str, ...]("argon2", "bcrypt")SESSION_COOKIE_NAMEstr"sessionid"SESSION_TIMEOUTtimedeltatimedelta(hours=1)SESSION_COOKIE_SECUREboolFalse- setTruein production (requires HTTPS)SESSION_COOKIE_HTTPONLYboolTrue- alwaysTruefor XSS protectionSESSION_COOKIE_SAMESITEstr"Lax"-"Lax","Strict", or"None"SESSION_COOKIE_PATHstr"/"SESSION_COOKIE_DOMAINstr | NoneNone- browser-determined domainUSER_MODELstr"openviper.auth.models.User"AUTH_SESSION_ENABLEDboolTrueSESSION_STOREstr"database"-"database"or a dotted custom pathAUTH_BACKENDStuple[str, ...]JWT + Session backends
DEFAULT_AUTHENTICATION_CLASSEStuple[str, ...]JWTAuthentication,SessionAuthenticationDEFAULT_PERMISSION_CLASSEStuple[str, ...]()- views are public unless permissions are configuredJWT
Field
Type
Default / Notes
JWT_ALGORITHMstr"HS256"JWT_ACCESS_TOKEN_EXPIREtimedeltatimedelta(minutes=30)JWT_REFRESH_TOKEN_EXPIREtimedeltatimedelta(days=7)CSRF
Field
Type
Default / Notes
CSRF_COOKIE_NAMEstr"csrftoken"CSRF_COOKIE_SECUREboolFalseCSRF_COOKIE_HTTPONLYboolFalse- must beFalsefor double-submit cookie patternCSRF_COOKIE_SAMESITEstr"Lax"CSRF_TRUSTED_ORIGINStuple[str, ...]()CORS
Field
Type
Default / Notes
CORS_ALLOWED_ORIGINStuple[str, ...]()CORS_ALLOW_CREDENTIALSboolFalseCORS_ALLOWED_METHODStuple[str, ...]("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")CORS_ALLOWED_HEADERStuple[str, ...]("*",)CORS_EXPOSE_HEADERStuple[str, ...]()CORS_MAX_AGEint600- preflight cache in secondsStatic Files & Media
Field
Type
Default / Notes
STATIC_URLstr"/static/"STATIC_ROOTstr"./static/"STATICFILES_DIRStuple[str, ...]("static/",)MEDIA_URLstr"/media/"MEDIA_ROOTstr"./media/"MEDIA_DIRstr"./media/"STATIC_STORAGEstr"local"-"local"or"s3"MEDIA_STORAGEstr"local"MAX_FILE_SIZEint10485760(10 MB)Templates
Field
Type
Default / Notes
TEMPLATES_DIRstr"templates/"TEMPLATE_AUTO_RELOADboolTrueJINJA_PLUGINSConfigMap{}- set{"enable": 1, "path": "jinja_plugins"}to activate auto-discoveryLogging
Field
Type
Default / Notes
LOG_LEVELstr"INFO"LOG_FORMATstr"text"-"text"or"json"LOGGINGConfigMap{}- fulllogging.config.dictConfigdict; overridesLOG_LEVEL/LOG_FORMATEmail
Field
Type
Default / Notes
EMAILConfigMapBackend config dict; keys:
backend,host,port,use_tls,use_ssl,timeout,username,password,from,default_sender,fail_silently,backgroundRate Limiting
Field
Type
Default / Notes
RATE_LIMIT_BACKENDstr"memory"-"memory"or"redis"RATE_LIMIT_REQUESTSint100RATE_LIMIT_WINDOWint60- window in secondsRATE_LIMIT_BYstr"ip"-"ip","user", or"path"Background Tasks
Field
Type
Default / Notes
TASKSConfigMap{}- Dramatiq broker/worker configuration (see Background Tasks)Model Events
Field
Type
Default / Notes
MODEL_EVENTSConfigMap{}- per-model lifecycle hooks; keys are"module.ClassName"paths, values map event names to lists of dotted callable paths. Supported events:before_validate,validate,before_insert,before_save,after_insert,on_change,on_update,on_delete,after_delete.OAuth2 Events
Field
Type
Default / Notes
OAUTH2_EVENTSdict[str, str]{}- lifecycle hooks for the OAuth2 flow; supported keys:on_success,on_fail,on_error,on_initialAI Integration
Field
Type
Default / Notes
ENABLE_AI_PROVIDERSboolFalseAI_PROVIDERSConfigMap{}- provider-keyed configuration dictsOpenAPI / Swagger
Field
Type
Default / Notes
OPENAPIConfigMapDict consolidating all OpenAPI configuration. Keys:
title("OpenViper API"),version("0.0.1"),description(""),docs_url("/open-api/docs"),redoc_url("/open-api/redoc"),schema_url("/open-api/openapi.json"),enabled(True),admin_url(None- admin routes stay hidden unless explicitly set, e.g."/admin"),exclude([]- route prefixes to omit, or"__ALL__"to disable entirely)Country Field
Field
Type
Default / Notes
COUNTRY_FIELDConfigMap{"EXTRA_COUNTRIES": {}, "ENABLE_CACHE": True, "STRICT": True}- configuration forCountryField- as_dict(mask_sensitive=True) ConfigMap
Return settings as a plain dict. Sensitive fields (e.g.
SECRET_KEY,DATABASES) are masked by default.
- __getitem__(item: str) ConfigValue
Bracket-access proxy:
settings["DEBUG"]delegates togetattr.
- class openviper.conf.settings.LazySettings
The
settingssingleton. Attribute access triggers lazy loading of the settings module on first use.- configure(settings_obj: Settings) None
Programmatically configure the settings. Must be called before any attribute is first read. Useful in tests and embedded applications.
- openviper.conf.settings.validate_settings(s, env) None
Validate a
Settingsinstance for the given environment string ("production","development", etc.). RaisesSettingsValidationErrorif required fields are missing or insecure values are found. Called automatically duringLazySettings.setup()- invalid production configurations are rejected at startup.
- openviper.conf.settings.generate_secret_key(length=64) str
Generate a cryptographically random secret key suitable for
SECRET_KEY.
- class openviper.conf.settings.JsonFormatter
Minimal JSON log formatter with no third-party dependencies. Outputs log records as JSON objects with
time,level,logger, andmessagekeys.
- class openviper.conf.settings.OVDefaultHandler
Sentinel
StreamHandlersubclass installed byconfigure_logging(). Using a distinct subclass letsconfigure_logging()remove its own handler on a subsequent call without touching handlers added by the application or third-party libraries.
Package Exports
openviper.conf re-exports the following from openviper.conf.settings:
Symbol |
Description |
|---|---|
|
Base frozen dataclass for all project settings |
|
Module-level |
|
Validate a |
|
Generate a cryptographically random secret key |
Example Usage
See also
Every example project has its own settings.py - compare patterns:
examples/todoapp/settings.py - minimal single-app settings
examples/ai_moderation_platform/ - multi-app with
AI_PROVIDERS,TASKS,MODEL_EVENTSexamples/ecommerce_clone/ -
TASKS,AI_PROVIDERS,EMAILconfig
Defining Settings
# myproject/settings.py
import dataclasses
from openviper.conf import Settings
@dataclasses.dataclass(frozen=True)
class MySettings(Settings):
PROJECT_NAME: str = "MyBlog"
DEBUG: bool = False
SECRET_KEY: str = "" # set via OPENVIPER_SECRET_KEY env var
INSTALLED_APPS: tuple = (
"myproject.blog",
"myproject.users",
)
# Database configuration (nested format recommended):
DATABASES = {
"default": {
"OPTIONS": {
"URL": "sqlite+aiosqlite:///db.sqlite3",
},
},
}
Then configure the environment:
export OPENVIPER_SETTINGS_MODULE=myproject.settings
export OPENVIPER_SECRET_KEY=your-secret-key-here
Accessing Settings
from openviper.conf import settings
print(settings.DEBUG) # False
print(settings.DATABASES) # {"default": {"OPTIONS": {"URL": "..."}}}
Programmatic Configuration (Tests)
import dataclasses
from openviper.conf import Settings
from openviper.conf.settings import settings
settings.configure(Settings(
DATABASES={
"default": {
"OPTIONS": {"URL": "sqlite+aiosqlite:///:memory:"},
},
},
SECRET_KEY="test-only-secret",
DEBUG=True,
))
Environment Variable Overrides
Any settings field can be overridden at runtime by setting an environment
variable named OPENVIPER_<FIELD_NAME> (uppercase). For example:
export OPENVIPER_DEBUG=true
export OPENVIPER_LOG_LEVEL=DEBUG
Internal API Reference
The following symbols are public but intended for framework internals, advanced customisation, or testing.
- openviper.conf.settings.cast_bool(v: str) bool
Cast a string to
bool. Truthy values:"1","true","yes","on".
- openviper.conf.settings.cast_tuple(v: str) tuple[str, ...]
Cast a comma-separated string to a
tupleof stripped values.
- openviper.conf.settings.cast_env_value(current: EnvValue, raw: str) EnvValue | None
Cast raw env-var string to the same type as current. Returns
Nonefor complex types (dicts, etc.) that cannot be cast from a string.
- openviper.conf.settings.auto_include_project_app(instance: Settings, module_path: str) Settings
Prepend the top-level project package to
INSTALLED_APPSif absent.
- openviper.conf.settings.apply_env_overrides(instance: Settings) Settings
Return a new
Settingswith environment variable overrides applied.
- openviper.conf.settings.configure_logging(instance: Settings) None
Apply logging configuration derived from a
Settingsinstance.
- openviper.conf.settings.load_settings_from_module(module_path: str) Settings | None
Import module_path and return a
Settingsinstance from it. ReturnsNonewhen module_path is empty or when the special"settings"module cannot be found (graceful fallback).
- openviper.conf.settings.validate_production(s: Settings, errors: list[str]) None
Append production-specific validation errors to errors.
- openviper.conf.settings.validate_production_security(s: Settings, errors: list[str]) None
Append production security-header validation errors to errors.
- openviper.conf.settings.validate_production_cookies(s: Settings, errors: list[str]) None
Append production cookie validation errors to errors.
- openviper.conf.settings.validate_production_api(s: Settings, errors: list[str]) None
Append production API exposure validation errors to errors.
- openviper.conf.settings.is_insecure_secret_key(key: str) bool
Return
Trueif key is empty or matches a known insecure value.
- openviper.conf.settings.INSECURE_SECRET_KEYS
frozensetof known insecureSECRET_KEYvalues.
- openviper.conf.settings.MIN_SECRET_KEY_LENGTH
Final[int]- minimumSECRET_KEYlength enforced byvalidate_production()(default:50).
- openviper.conf.settings.MIN_HSTS_SECONDS
Final[int]- minimumSECURE_HSTS_SECONDSenforced byvalidate_production_security()(default:31536000, 1 year).
- openviper.conf.settings.SENSITIVE_FIELDS
frozensetof field names that are masked byas_dict().
- openviper.conf.settings.INSECURE_JWT_ALGORITHMS
frozensetof JWT algorithm names rejected byvalidate_settings().
- openviper.conf.settings.ENV_CASTERS
dictmapping types to caster callables for environment variable parsing.
- openviper.conf.settings.MODULE_CACHE
dict[str, types.ModuleType]caching imported settings modules.
- openviper.conf.settings.SETTINGS_CLASS_CACHE
dict[str, type[Settings]]caching resolved settings classes.
- openviper.conf.settings.FIELD_METADATA_CACHE
dict[type, list[tuple[str, type]]]caching field metadata per class.
- openviper.conf.settings.DOTENV_LOADED
boolflag tracking whether.envhas been loaded to avoid redundant I/O.
- openviper.conf.settings.DOTENV_LOADED
booltracking whether.envhas been loaded.
- openviper.conf.settings.dotenv_path
strpath to the discovered.envfile.
- openviper.conf.settings.framework_version
str- the current OpenViper framework version string.
- class openviper.conf.settings.JsonFormatter(logging.Formatter)
Minimal JSON log formatter with no third-party dependencies.
- class openviper.conf.settings.OVDefaultHandler(logging.StreamHandler[io.TextIOWrapper])
Sentinel
StreamHandlerinstalled byconfigure_logging(). Using a distinct subclass letsconfigure_loggingremove its own handler on a subsequent call without touching user-added handlers.
Type Aliases
- openviper.conf.types.ConfigValue
Union of valid configuration field value types:
str | int | float | bool | None | timedelta | tuple[str, ...] | list[str] | dict[str, ConfigValue]
- openviper.conf.types.ConfigMap
dict[str, ConfigValue]- configuration mapping type.
- openviper.conf.types.EnvValue
Union of types that can be cast from environment variable strings:
bool | int | float | str | tuple[str, ...] | timedelta