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 b1bf086ae9)
This commit is contained in:
Oliver 2023-05-22 22:59:10 +10:00 committed by GitHub
parent f76059b2b4
commit 96b7845d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -764,6 +764,11 @@ CURRENCIES = get_setting(
typecast=list, 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 # Maximum number of decimal places for currency rendering
CURRENCY_DECIMAL_PLACES = 6 CURRENCY_DECIMAL_PLACES = 6

View File

@ -8,6 +8,16 @@ def set_default_currency(apps, schema_editor):
""" migrate the currency setting from config.yml to db """ """ migrate the currency setting from config.yml to db """
# get value from settings-file # get value from settings-file
base_currency = get_setting('INVENTREE_BASE_CURRENCY', 'base_currency', 'USD') 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 # write to database
InvenTreeSetting.set_setting('INVENTREE_DEFAULT_CURRENCY', base_currency, None, create=True) InvenTreeSetting.set_setting('INVENTREE_DEFAULT_CURRENCY', base_currency, None, create=True)