From f5dcffd032bccdf4a908cce22e56d2aa4401c786 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 19 Jun 2026 23:05:23 +0000 Subject: [PATCH] Remove global setting - simplify code --- CHANGELOG.md | 2 +- docs/docs/report/helpers.md | 9 +++----- docs/docs/settings/global.md | 1 - .../InvenTree/common/setting/system.py | 8 ------- src/backend/InvenTree/common/tests.py | 18 --------------- .../InvenTree/report/templatetags/report.py | 22 ++++++++++--------- .../pages/Index/Settings/SystemSettings.tsx | 1 - 7 files changed, 16 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dccb9c81bb..930b77ad24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- [#12208](https://github.com/inventree/InvenTree/pull/12208) adds custom locale support for rendering currencies, dates and numbers within reports. This allows users to specify a custom locale for report rendering, which can be used to control the formatting of dates, numbers and currency values in the generated reports. The custom locale can be set via the `REPORT_LOCALE` system setting, and if not set, the system will fall back to using the default system locale for report rendering. +- [#12208](https://github.com/inventree/InvenTree/pull/12208) adds custom locale support for rendering currencies, dates and numbers within reports. This allows users to specify a custom locale for report rendering, which can be used to control the formatting of dates, numbers and currency values in the generated reports. - [#12204](https://github.com/inventree/InvenTree/pull/12204) adds new filtering options to PartCategoryTree and StockLocationTree API endpoints, allowing tree data to be fetched dynamically - [#12165](https://github.com/inventree/InvenTree/pull/12165) adds support for parameters against the PartCategory model - [#12103](https://github.com/inventree/InvenTree/pull/12103) adds column-based filtering to table views in the user interface. This extends the existing table filtering functionality by allowing users to apply filters directly to individual columns. diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 2d414f4bc1..dfc98bc442 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -410,7 +410,7 @@ For rendering date and datetime information, the following helper functions are Both functions resolve their output using the following priority order: 1. **`fmt=` argument** — a [strftime format string](https://docs.python.org/3/library/datetime.html#format-codes). When provided, this takes full priority and the `locale` argument is ignored. -2. **`locale=` argument** (or the `REPORT_LOCALE` global setting) — when no `fmt` is given, Babel formats the value in a locale-aware *medium* style (e.g. `Jan 12, 2025` for `en-us`). +2. **`locale=` argument** (or the default system locale setting) — when no `fmt` is given, Babel formats the value in a locale-aware *medium* style (e.g. `Jan 12, 2025` for `en-us`). 3. **ISO 8601 fallback** — when neither `fmt` nor a locale is available, the value is returned in ISO format (e.g. `2025-01-12`). ### format_date @@ -507,10 +507,7 @@ The locale controls how the currency symbol is rendered. Different locales produ The locale used by `render_currency` is resolved in the following priority order: 1. **Explicit `locale=` argument** in the template tag — highest priority, always wins -2. **`REPORT_LOCALE` global setting** — applied to all reports when set, with no per-tag argument needed -3. **Server `LANGUAGE_CODE`** — the fallback when neither of the above is configured - -To apply a consistent locale across all reports without modifying every template, set the `REPORT_LOCALE` global setting (via *Settings → Reporting → Report Currency Locale*). Individual templates can still override it per-tag using the `locale=` argument. +2. **Server `LANGUAGE_CODE`** — the fallback when neither of the above is configured #### Example @@ -521,7 +518,7 @@ To apply a consistent locale across all reports without modifying every template Line Item Unit Pricing: diff --git a/docs/docs/settings/global.md b/docs/docs/settings/global.md index c855f4b15a..2e22defb8e 100644 --- a/docs/docs/settings/global.md +++ b/docs/docs/settings/global.md @@ -142,7 +142,6 @@ Configuration of report generation: | Name | Description | Default | Units | | ---- | ----------- | ------- | ----- | {{ globalsetting("REPORT_ENABLE") }} -{{ globalsetting("REPORT_LOCALE") }} {{ globalsetting("REPORT_DEFAULT_PAGE_SIZE") }} {{ globalsetting("REPORT_DEBUG_MODE") }} {{ globalsetting("REPORT_FETCH_URLS") }} diff --git a/src/backend/InvenTree/common/setting/system.py b/src/backend/InvenTree/common/setting/system.py index dffe340d35..d6fe3e3a6b 100644 --- a/src/backend/InvenTree/common/setting/system.py +++ b/src/backend/InvenTree/common/setting/system.py @@ -671,14 +671,6 @@ SYSTEM_SETTINGS: dict[str, InvenTreeSettingsKeyType] = { 'default': False, 'validator': bool, }, - 'REPORT_LOCALE': { - 'name': _('Default Report Locale'), - 'description': _( - 'Default locale to use when rendering dates, numbers, and currency values in reports (e.g. en-US, de-DE). Leave blank to use the system locale setting.' - ), - 'default': '', - 'validator': common.validators.validate_locale, - }, 'REPORT_DEBUG_MODE': { 'name': _('Debug Mode'), 'description': _('Generate reports in debug mode (HTML output)'), diff --git a/src/backend/InvenTree/common/tests.py b/src/backend/InvenTree/common/tests.py index 17b373b6cf..2bd0c41db8 100644 --- a/src/backend/InvenTree/common/tests.py +++ b/src/backend/InvenTree/common/tests.py @@ -620,24 +620,6 @@ class GlobalSettingsApiTest(InvenTreeAPITestCase): self.assertEqual(codes, 'AUD,USD,GBP') - def test_report_currency_locale(self): - """Test validation of the REPORT_LOCALE setting (locale string for report formatting).""" - url = reverse('api-global-setting-detail', kwargs={'key': 'REPORT_LOCALE'}) - - # Valid locale values are accepted - for valid in ['en-us', 'en-gb', 'en-au', 'de-de', 'fr-fr']: - response = self.patch(url, data={'value': valid}, expected_code=200) - self.assertEqual(response.data['value'], valid) - - # Blank value is accepted (means "use system locale") - response = self.patch(url, data={'value': ''}, expected_code=200) - self.assertEqual(response.data['value'], '') - - # Invalid locale values are rejected - for invalid in ['xx-zz', 'not-a-locale']: - response = self.patch(url, data={'value': invalid}, expected_code=400) - self.assertIn('Invalid locale value', str(response.data)) - def test_company_name(self): """Test a settings object lifecycle e2e.""" setting = InvenTreeSetting.get_setting_object('INVENTREE_COMPANY_NAME') diff --git a/src/backend/InvenTree/report/templatetags/report.py b/src/backend/InvenTree/report/templatetags/report.py index 33abd3b022..0c54202685 100644 --- a/src/backend/InvenTree/report/templatetags/report.py +++ b/src/backend/InvenTree/report/templatetags/report.py @@ -776,23 +776,25 @@ def modulo(x: Any, y: Any, cast: Optional[type] = None) -> Any: return cast_to_type(result, cast) -def _get_report_locale(override: Optional[str] = None) -> Optional[str]: - """Return the locale to use for report formatting. +def _get_report_locale(override: Optional[str] = None) -> str: + """Return the locale to use for report formatting.""" + locale = override or settings.LANGUAGE_CODE - Priority: explicit override > REPORT_LOCALE global setting > None (caller falls back to LANGUAGE_CODE). - """ - if override: - return override + # Run a check to ensure that the locale is valid (i.e. can be used by babel) + try: + get_locale(locale) + except Exception: + raise ValidationError( + f'Invalid locale specified for report formatting: {locale}' + ) - return get_global_setting('REPORT_LOCALE', cache=True) or settings.LANGUAGE_CODE + return locale @register.simple_tag def render_currency(money, **kwargs): """Render a currency / Money object.""" - if 'locale' not in kwargs: - if locale := _get_report_locale(): - kwargs['locale'] = locale + kwargs['locale'] = _get_report_locale(kwargs.get('locale')) return InvenTree.helpers_model.render_currency(money, **kwargs) diff --git a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx index b9f3a0c35e..c272fddaed 100644 --- a/src/frontend/src/pages/Index/Settings/SystemSettings.tsx +++ b/src/frontend/src/pages/Index/Settings/SystemSettings.tsx @@ -197,7 +197,6 @@ export default function SystemSettings() {