From 96b7845d840d0d5acb556c68a625d01be43af23a Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 22 May 2023 22:59:10 +1000 Subject: [PATCH] Fix for currency migration (#4873) - Handle case where no currencies are provided - Handle case where base currency is not in provided options (cherry picked from commit b1bf086ae9024d82911fb402e4193f02cf74c8fb) --- InvenTree/InvenTree/settings.py | 5 +++++ .../common/migrations/0010_migrate_currency_setting.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 0632f78245..ef4d6f558a 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -764,6 +764,11 @@ CURRENCIES = get_setting( typecast=list, ) +# Ensure that at least one currency value is available +if len(CURRENCIES) == 0: # pragma: no cover + logger.warning("No currencies selected: Defaulting to USD") + CURRENCIES = ['USD'] + # Maximum number of decimal places for currency rendering CURRENCY_DECIMAL_PLACES = 6 diff --git a/InvenTree/common/migrations/0010_migrate_currency_setting.py b/InvenTree/common/migrations/0010_migrate_currency_setting.py index 0604e93008..09b0100db4 100644 --- a/InvenTree/common/migrations/0010_migrate_currency_setting.py +++ b/InvenTree/common/migrations/0010_migrate_currency_setting.py @@ -8,6 +8,16 @@ def set_default_currency(apps, schema_editor): """ migrate the currency setting from config.yml to db """ # get value from settings-file base_currency = get_setting('INVENTREE_BASE_CURRENCY', 'base_currency', 'USD') + + from common.settings import currency_codes + + # check if value is valid + if base_currency not in currency_codes(): + if len (currency_codes()) > 0: + base_currency = currency_codes()[0] + else: + base_currency = 'USD' + # write to database InvenTreeSetting.set_setting('INVENTREE_DEFAULT_CURRENCY', base_currency, None, create=True)