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

1# Generated by Django 4.2.12 on 2024-06-02 13:32 

2 

3from django.conf import settings 

4from django.db import migrations 

5 

6from moneyed import CURRENCIES 

7 

8import InvenTree.config 

9 

10 

11def set_currencies(apps, schema_editor): 

12 """Set the default currency codes. 

13  

14 Ref: https://github.com/inventree/InvenTree/pull/7390 

15  

16 Previously, the allowed currency codes were set in the external configuration 

17 (e.g via the configuration file or environment variables). 

18 

19 Now, they are set in the database (via the InvenTreeSetting model). 

20 

21 So, this data migration exists to transfer any configured currency codes, 

22 from the external configuration, into the database settings model. 

23 """ 

24 

25 InvenTreeSetting = apps.get_model('common', 'InvenTreeSetting') 

26 

27 key = 'CURRENCY_CODES' 

28 

29 codes = InvenTree.config.get_setting('INVENTREE_CURRENCIES', 'currencies', None) 

30 

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 

34 

35 if type(codes) == str: 

36 codes = codes.split(',') 

37 

38 valid_codes = set() 

39 

40 for code in codes: 

41 code = code.strip().upper() 

42 

43 if code in CURRENCIES: 

44 valid_codes.add(code) 

45 

46 if len(valid_codes) == 0: 

47 print(f"No currency codes found in configuration file - skipping migration") 

48 return 

49 

50 value = ','.join(valid_codes) 

51 

52 if not settings.TESTING: # pragma: no cover 

53 print(f"Found existing currency codes:", value) 

54 

55 setting = InvenTreeSetting.objects.filter(key=key).first() 

56 

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() 

67 

68 

69 

70class Migration(migrations.Migration): 

71 

72 dependencies = [ 

73 ('common', '0022_projectcode_responsible'), 

74 ] 

75 

76 operations = [ 

77 migrations.RunPython(set_currencies, reverse_code=migrations.RunPython.noop) 

78 ]