Coverage for src/backend/InvenTree/common/migrations/0023_auto_20240602_1332.py: 26%
36 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1# Generated by Django 4.2.12 on 2024-06-02 13:32
3from django.conf import settings
4from django.db import migrations
6from moneyed import CURRENCIES
8import InvenTree.config
11def set_currencies(apps, schema_editor):
12 """Set the default currency codes.
14 Ref: https://github.com/inventree/InvenTree/pull/7390
16 Previously, the allowed currency codes were set in the external configuration
17 (e.g via the configuration file or environment variables).
19 Now, they are set in the database (via the InvenTreeSetting model).
21 So, this data migration exists to transfer any configured currency codes,
22 from the external configuration, into the database settings model.
23 """
25 InvenTreeSetting = apps.get_model('common', 'InvenTreeSetting')
27 key = 'CURRENCY_CODES'
29 codes = InvenTree.config.get_setting('INVENTREE_CURRENCIES', 'currencies', None)
31 if codes is None: 31 ↛ 35line 31 didn't jump to line 35 because the condition on line 31 was always true
32 # No currency codes are defined in the configuration file
33 return
35 if type(codes) == str:
36 codes = codes.split(',')
38 valid_codes = set()
40 for code in codes:
41 code = code.strip().upper()
43 if code in CURRENCIES:
44 valid_codes.add(code)
46 if len(valid_codes) == 0:
47 print(f"No currency codes found in configuration file - skipping migration")
48 return
50 value = ','.join(valid_codes)
52 if not settings.TESTING: # pragma: no cover
53 print(f"Found existing currency codes:", value)
55 setting = InvenTreeSetting.objects.filter(key=key).first()
57 if setting:
58 if not settings.TESTING: # pragma: no cover
59 print(f"- Updating existing setting for currency codes")
60 setting.value = value
61 setting.save()
62 else:
63 if not settings.TESTING: # pragma: no cover
64 print(f"- Creating new setting for currency codes")
65 setting = InvenTreeSetting(key=key, value=value)
66 setting.save()
70class Migration(migrations.Migration):
72 dependencies = [
73 ('common', '0022_projectcode_responsible'),
74 ]
76 operations = [
77 migrations.RunPython(set_currencies, reverse_code=migrations.RunPython.noop)
78 ]