Skip to content

refactor(settings): standardize Constance env overrides to CONSTANCE_ prefix DEV-1976#7230

Open
platreth wants to merge 2 commits into
mainfrom
hugo/dev-1976-constance-env-prefix
Open

refactor(settings): standardize Constance env overrides to CONSTANCE_ prefix DEV-1976#7230
platreth wants to merge 2 commits into
mainfrom
hugo/dev-1976-constance-env-prefix

Conversation

@platreth

@platreth platreth commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

🔗 Related PRs — must merge together

This is the app-side of a 3-repo Constance env-var standardization:

The kpi app now reads CONSTANCE_SUPPORT_EMAIL, so shipping one without the others silently drops the support email to its default. Merge/release together.

📣 Summary

Standardize all Constance override env vars on a single CONSTANCE_ prefix.

📖 Description

The env vars that override Constance defaults used three conventions (bare, KOBO_, CONSTANCE_). This unifies them on CONSTANCE_, matching the existing CONSTANCE_ASR_MT_GOOGLE_* precedent.

Renamed:

  • KOBO_SUPPORT_EMAILCONSTANCE_SUPPORT_EMAIL
  • KOBO_SUPPORT_URLCONSTANCE_SUPPORT_URL
  • KOBO_ACADEMY_URLCONSTANCE_ACADEMY_URL
  • KOBO_COMMUNITY_URLCONSTANCE_COMMUNITY_URL
  • USAGE_LIMIT_ENFORCEMENTCONSTANCE_USAGE_LIMIT_ENFORCEMENT (env fallback in base.py + pytest env in pyproject.toml)

⚠️ Breaking: no backward-compat fallback. Deployments setting the old names must rename them. The two deploy repos are covered by the paired PRs above.

✅ Test plan

  • test_api_environment.py — both settings axes
  • usage-limit-enforcement tests (KPI submissions, OpenRosa, subsequences)

… prefix

Rename Constance override env vars to a single CONSTANCE_ prefix:
- KOBO_SUPPORT_EMAIL/URL, KOBO_ACADEMY_URL, KOBO_COMMUNITY_URL
- USAGE_LIMIT_ENFORCEMENT (env fallback + pytest env)

BREAKING: deployments setting the old names must rename them.
No backward-compat fallback (matches the existing ASR_MT_GOOGLE_* precedent).
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR standardizes all Constance env-var overrides to a CONSTANCE_ prefix, replacing the previous mix of bare names, KOBO_, and CONSTANCE_ conventions. It introduces a shared _constance_env helper that resolves the new-prefixed key, falls back transparently to the deprecated name with a DeprecationWarning, and otherwise returns the hard-coded default.

  • kobo/settings/base.py: Adds _constance_env(getter, new_key, deprecated_key, default) and applies it to SUPPORT_EMAIL, SUPPORT_URL, ACADEMY_URL, COMMUNITY_URL, and USAGE_LIMIT_ENFORCEMENT; removes the now-superseded migration comment for the ASR_MT_GOOGLE_* vars.
  • pyproject.toml: Renames the pytest env override from USAGE_LIMIT_ENFORCEMENT=True to CONSTANCE_USAGE_LIMIT_ENFORCEMENT=True to match the new name, ensuring usage-limit tests still behave correctly.

Confidence Score: 5/5

Safe to merge — the refactor correctly resolves env vars with a deprecation-warning fallback, and the pytest env rename keeps tests consistent.

The two-line logic in _constance_env is straightforward, warnings is already imported, and the existing ASR_MT_GOOGLE_* vars confirm the pattern works. The only note is that the PR description overstates the breaking nature of the change — the code actually provides a graceful fallback — but this does not affect runtime correctness.

No files require special attention.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Settings module loads] --> B{Is deprecated_key in os.environ?}
    B -- Yes --> C[Emit DeprecationWarning]
    B -- No --> D[No warning]
    C --> E[Resolve deprecated_key value]
    D --> F[Resolve deprecated_key value or default]
    E --> G{Is new CONSTANCE_ key in os.environ?}
    F --> G
    G -- Yes --> H[Use CONSTANCE_ key value]
    G -- No --> I[Use deprecated key value or default]
    H --> J[CONSTANCE_CONFIG default set]
    I --> J
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[Settings module loads] --> B{Is deprecated_key in os.environ?}
    B -- Yes --> C[Emit DeprecationWarning]
    B -- No --> D[No warning]
    C --> E[Resolve deprecated_key value]
    D --> F[Resolve deprecated_key value or default]
    E --> G{Is new CONSTANCE_ key in os.environ?}
    F --> G
    G -- Yes --> H[Use CONSTANCE_ key value]
    G -- No --> I[Use deprecated key value or default]
    H --> J[CONSTANCE_CONFIG default set]
    I --> J
Loading

Reviews (2): Last reviewed commit: "refactor(settings): keep backward-compat..." | Re-trigger Greptile

Comment thread kobo/settings/base.py
'in the user interface',
),
'SUPPORT_EMAIL': (
env.str('KOBO_SUPPORT_EMAIL', env.str('DEFAULT_FROM_EMAIL', 'help@kobotoolbox.org')),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not break the compatibility here.
Just read from the new variable but keep reading from the deprecated one too.

For any deprecated variable. Please log a Deprecation warning instance.
See

kpi/kobo/settings/base.py

Lines 2007 to 2014 in 39416ad

if not (MONGO_DB_URL := env.str('MONGO_DB_URL', False)):
# ToDo Remove all this block by the end of 2022.
# Update kobo-install accordingly
logging.warning(
'`MONGO_DB_URL` is not found. '
'`KPI_MONGO_HOST`, `KPI_MONGO_PORT`, `KPI_MONGO_NAME`, '
'`KPI_MONGO_USER`, `KPI_MONGO_PASS` '
'are deprecated and will not be supported anymore soon.'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's way better! Done

@platreth platreth self-assigned this Jul 9, 2026
@platreth platreth removed the request for review from jnm July 9, 2026 14:01
… names

Per review: read the CONSTANCE_-prefixed var but fall back to the
deprecated name (KOBO_*/bare), emitting a DeprecationWarning when the old
name is still set. Adds the _constance_env helper (matches the existing
KPI_BROKER_URL -> CELERY_BROKER_URL deprecation idiom).
@platreth platreth requested a review from noliveleger July 9, 2026 15:26

@noliveleger noliveleger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also update the PR description.
It does not match what's happening anymore 🙏

Comment thread kobo/settings/base.py
# `django.conf.settings.THE_SETTING`


def _constance_env(getter, new_key, deprecated_key, default):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to create an utility file where this could be moved out the settings files.
You can also move other helpers present in that file (e.g.: dj_stripe_request_callback_method)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants