From d7d3d8aa26d392f128d745dd7c50ac542c5173b0 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Wed, 7 Jun 2023 12:05:37 +0200 Subject: [PATCH] Currency migrations - stop migrations when defaults change (#4975) * make currency choices independend * replace hard coded default currency * use function for psql? * use callable default * revert some fields * also migrat all currency codes --- InvenTree/common/settings.py | 5 +++++ InvenTree/company/migrations/0025_auto_20201110_1001.py | 5 +++-- InvenTree/order/migrations/0038_auto_20201112_1737.py | 5 +++-- InvenTree/order/migrations/0039_auto_20201112_2203.py | 3 ++- InvenTree/order/migrations/0045_auto_20210504_1946.py | 5 +++-- InvenTree/part/migrations/0055_auto_20201110_1001.py | 5 +++-- InvenTree/part/migrations/0067_partinternalpricebreak.py | 5 +++-- InvenTree/part/migrations/0089_auto_20221112_0128.py | 2 +- InvenTree/stock/migrations/0053_auto_20201110_0513.py | 5 +++-- InvenTree/stock/migrations/0055_auto_20201117_1453.py | 3 ++- 10 files changed, 28 insertions(+), 15 deletions(-) diff --git a/InvenTree/common/settings.py b/InvenTree/common/settings.py index 96db99fa63..be049cf433 100644 --- a/InvenTree/common/settings.py +++ b/InvenTree/common/settings.py @@ -23,6 +23,11 @@ def currency_code_default(): return code +def all_currency_codes(): + """Returns a list of all currency codes.""" + return [(a, CURRENCIES[a].name) for a in CURRENCIES] + + def currency_code_mappings(): """Returns the current currency choices.""" return [(a, CURRENCIES[a].name) for a in settings.CURRENCIES] diff --git a/InvenTree/company/migrations/0025_auto_20201110_1001.py b/InvenTree/company/migrations/0025_auto_20201110_1001.py index 0fd62c0a23..f2fa767d81 100644 --- a/InvenTree/company/migrations/0025_auto_20201110_1001.py +++ b/InvenTree/company/migrations/0025_auto_20201110_1001.py @@ -2,6 +2,7 @@ from django.db import migrations, connection import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,11 +15,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='supplierpricebreak', name='price', - field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price'), + field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price'), ), migrations.AddField( model_name='supplierpricebreak', name='price_currency', - field=djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('NZD', 'New Zealand Dollar'), ('GBP', 'Pound Sterling'), ('USD', 'US Dollar'), ('JPY', 'Yen')], default='USD', editable=False, max_length=3), + field=djmoney.models.fields.CurrencyField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default(), editable=False, max_length=3), ), ] diff --git a/InvenTree/order/migrations/0038_auto_20201112_1737.py b/InvenTree/order/migrations/0038_auto_20201112_1737.py index 0d563c80d4..cdad4fe72f 100644 --- a/InvenTree/order/migrations/0038_auto_20201112_1737.py +++ b/InvenTree/order/migrations/0038_auto_20201112_1737.py @@ -2,6 +2,7 @@ from django.db import migrations import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,11 +15,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='purchaseorderlineitem', name='purchase_price', - field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Unit purchase price', max_digits=19, null=True, verbose_name='Purchase Price'), + field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit purchase price', max_digits=19, null=True, verbose_name='Purchase Price'), ), migrations.AddField( model_name='purchaseorderlineitem', name='purchase_price_currency', - field=djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('NZD', 'New Zealand Dollar'), ('GBP', 'Pound Sterling'), ('USD', 'US Dollar'), ('JPY', 'Yen')], default='USD', editable=False, max_length=3), + field=djmoney.models.fields.CurrencyField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default(), editable=False, max_length=3), ), ] diff --git a/InvenTree/order/migrations/0039_auto_20201112_2203.py b/InvenTree/order/migrations/0039_auto_20201112_2203.py index bff585688d..52b51b6ca9 100644 --- a/InvenTree/order/migrations/0039_auto_20201112_2203.py +++ b/InvenTree/order/migrations/0039_auto_20201112_2203.py @@ -2,6 +2,7 @@ from django.db import migrations import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='purchaseorderlineitem', name='purchase_price', - field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency='USD', help_text='Unit purchase price', max_digits=19, null=True, verbose_name='Purchase Price'), + field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit purchase price', max_digits=19, null=True, verbose_name='Purchase Price'), ), ] diff --git a/InvenTree/order/migrations/0045_auto_20210504_1946.py b/InvenTree/order/migrations/0045_auto_20210504_1946.py index a8d9469dc7..c1e5f23df2 100644 --- a/InvenTree/order/migrations/0045_auto_20210504_1946.py +++ b/InvenTree/order/migrations/0045_auto_20210504_1946.py @@ -1,6 +1,7 @@ # Generated by Django 3.2 on 2021-05-04 19:46 from django.db import migrations +import common.settings import djmoney.models.fields @@ -14,11 +15,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='salesorderlineitem', name='sale_price', - field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency='USD', help_text='Unit sale price', max_digits=19, null=True, verbose_name='Sale Price'), + field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit sale price', max_digits=19, null=True, verbose_name='Sale Price'), ), migrations.AddField( model_name='salesorderlineitem', name='sale_price_currency', - field=djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('GBP', 'British Pound'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('JPY', 'Japanese Yen'), ('NZD', 'New Zealand Dollar'), ('USD', 'US Dollar')], default='USD', editable=False, max_length=3), + field=djmoney.models.fields.CurrencyField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default(), editable=False, max_length=3), ), ] diff --git a/InvenTree/part/migrations/0055_auto_20201110_1001.py b/InvenTree/part/migrations/0055_auto_20201110_1001.py index e9649c985e..5c31eb3250 100644 --- a/InvenTree/part/migrations/0055_auto_20201110_1001.py +++ b/InvenTree/part/migrations/0055_auto_20201110_1001.py @@ -2,6 +2,7 @@ from django.db import migrations import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,11 +15,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='partsellpricebreak', name='price', - field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price'), + field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price'), ), migrations.AddField( model_name='partsellpricebreak', name='price_currency', - field=djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('NZD', 'New Zealand Dollar'), ('GBP', 'Pound Sterling'), ('USD', 'US Dollar'), ('JPY', 'Yen')], default='USD', editable=False, max_length=3), + field=djmoney.models.fields.CurrencyField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default(), editable=False, max_length=3), ), ] diff --git a/InvenTree/part/migrations/0067_partinternalpricebreak.py b/InvenTree/part/migrations/0067_partinternalpricebreak.py index f1b16fe87c..f0e8561730 100644 --- a/InvenTree/part/migrations/0067_partinternalpricebreak.py +++ b/InvenTree/part/migrations/0067_partinternalpricebreak.py @@ -2,6 +2,7 @@ import InvenTree.fields import django.core.validators +import common.settings from django.db import migrations, models import django.db.models.deletion import djmoney.models.fields @@ -19,8 +20,8 @@ class Migration(migrations.Migration): fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('quantity', InvenTree.fields.RoundingDecimalField(decimal_places=5, default=1, help_text='Price break quantity', max_digits=15, validators=[django.core.validators.MinValueValidator(1)], verbose_name='Quantity')), - ('price_currency', djmoney.models.fields.CurrencyField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('EUR', 'Euro'), ('NZD', 'New Zealand Dollar'), ('GBP', 'Pound Sterling'), ('USD', 'US Dollar'), ('JPY', 'Yen')], default='USD', editable=False, max_length=3)), - ('price', djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price')), + ('price_currency', djmoney.models.fields.CurrencyField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default(), editable=False, max_length=3)), + ('price', djmoney.models.fields.MoneyField(decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Unit price at specified quantity', max_digits=19, null=True, verbose_name='Price')), ('part', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='internalpricebreaks', to='part.part', verbose_name='Part')), ], options={ diff --git a/InvenTree/part/migrations/0089_auto_20221112_0128.py b/InvenTree/part/migrations/0089_auto_20221112_0128.py index ed84289568..7d25a1f558 100644 --- a/InvenTree/part/migrations/0089_auto_20221112_0128.py +++ b/InvenTree/part/migrations/0089_auto_20221112_0128.py @@ -35,7 +35,7 @@ class Migration(migrations.Migration): name='PartPricing', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('currency', models.CharField(choices=[('AUD', 'Australian Dollar'), ('CAD', 'Canadian Dollar'), ('CNY', 'Chinese Yuan'), ('EUR', 'Euro'), ('GBP', 'British Pound'), ('JPY', 'Japanese Yen'), ('NZD', 'New Zealand Dollar'), ('USD', 'US Dollar')], default=common.settings.currency_code_default, help_text='Currency used to cache pricing calculations', max_length=10, verbose_name='Currency')), + ('currency', models.CharField(choices=common.settings.currency_code_mappings(), default=common.settings.currency_code_default, help_text='Currency used to cache pricing calculations', max_length=10, verbose_name='Currency')), ('updated', models.DateTimeField(auto_now=True, help_text='Timestamp of last pricing update', verbose_name='Updated')), ('scheduled_for_update', models.BooleanField(default=False)), ('bom_cost_min_currency', djmoney.models.fields.CurrencyField(choices=[], default='', editable=False, max_length=3)), diff --git a/InvenTree/stock/migrations/0053_auto_20201110_0513.py b/InvenTree/stock/migrations/0053_auto_20201110_0513.py index db002c9135..4462eb44cb 100644 --- a/InvenTree/stock/migrations/0053_auto_20201110_0513.py +++ b/InvenTree/stock/migrations/0053_auto_20201110_0513.py @@ -2,6 +2,7 @@ from django.db import migrations import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,11 +15,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='stockitem', name='purchase_price', - field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency='USD', help_text='Single unit purchase price at time of purchase', max_digits=19, null=True, verbose_name='Purchase Price'), + field=djmoney.models.fields.MoneyField(decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Single unit purchase price at time of purchase', max_digits=19, null=True, verbose_name='Purchase Price'), ), migrations.AddField( model_name='stockitem', name='purchase_price_currency', - field=djmoney.models.fields.CurrencyField(choices=[('XUA', 'ADB Unit of Account'), ('AFN', 'Afghani'), ('DZD', 'Algerian Dinar'), ('ARS', 'Argentine Peso'), ('AMD', 'Armenian Dram'), ('AWG', 'Aruban Guilder'), ('AUD', 'Australian Dollar'), ('AZN', 'Azerbaijanian Manat'), ('BSD', 'Bahamian Dollar'), ('BHD', 'Bahraini Dinar'), ('THB', 'Baht'), ('PAB', 'Balboa'), ('BBD', 'Barbados Dollar'), ('BYN', 'Belarussian Ruble'), ('BYR', 'Belarussian Ruble'), ('BZD', 'Belize Dollar'), ('BMD', 'Bermudian Dollar (customarily known as Bermuda Dollar)'), ('BTN', 'Bhutanese ngultrum'), ('VEF', 'Bolivar Fuerte'), ('BOB', 'Boliviano'), ('XBA', 'Bond Markets Units European Composite Unit (EURCO)'), ('BRL', 'Brazilian Real'), ('BND', 'Brunei Dollar'), ('BGN', 'Bulgarian Lev'), ('BIF', 'Burundi Franc'), ('XOF', 'CFA Franc BCEAO'), ('XAF', 'CFA franc BEAC'), ('XPF', 'CFP Franc'), ('CAD', 'Canadian Dollar'), ('CVE', 'Cape Verde Escudo'), ('KYD', 'Cayman Islands Dollar'), ('CLP', 'Chilean peso'), ('XTS', 'Codes specifically reserved for testing purposes'), ('COP', 'Colombian peso'), ('KMF', 'Comoro Franc'), ('CDF', 'Congolese franc'), ('BAM', 'Convertible Marks'), ('NIO', 'Cordoba Oro'), ('CRC', 'Costa Rican Colon'), ('HRK', 'Croatian Kuna'), ('CUP', 'Cuban Peso'), ('CUC', 'Cuban convertible peso'), ('CZK', 'Czech Koruna'), ('GMD', 'Dalasi'), ('DKK', 'Danish Krone'), ('MKD', 'Denar'), ('DJF', 'Djibouti Franc'), ('STD', 'Dobra'), ('DOP', 'Dominican Peso'), ('VND', 'Dong'), ('XCD', 'East Caribbean Dollar'), ('EGP', 'Egyptian Pound'), ('SVC', 'El Salvador Colon'), ('ETB', 'Ethiopian Birr'), ('EUR', 'Euro'), ('XBB', 'European Monetary Unit (E.M.U.-6)'), ('XBD', 'European Unit of Account 17(E.U.A.-17)'), ('XBC', 'European Unit of Account 9(E.U.A.-9)'), ('FKP', 'Falkland Islands Pound'), ('FJD', 'Fiji Dollar'), ('HUF', 'Forint'), ('GHS', 'Ghana Cedi'), ('GIP', 'Gibraltar Pound'), ('XAU', 'Gold'), ('XFO', 'Gold-Franc'), ('PYG', 'Guarani'), ('GNF', 'Guinea Franc'), ('GYD', 'Guyana Dollar'), ('HTG', 'Haitian gourde'), ('HKD', 'Hong Kong Dollar'), ('UAH', 'Hryvnia'), ('ISK', 'Iceland Krona'), ('INR', 'Indian Rupee'), ('IRR', 'Iranian Rial'), ('IQD', 'Iraqi Dinar'), ('IMP', 'Isle of Man Pound'), ('JMD', 'Jamaican Dollar'), ('JOD', 'Jordanian Dinar'), ('KES', 'Kenyan Shilling'), ('PGK', 'Kina'), ('LAK', 'Kip'), ('KWD', 'Kuwaiti Dinar'), ('AOA', 'Kwanza'), ('MMK', 'Kyat'), ('GEL', 'Lari'), ('LVL', 'Latvian Lats'), ('LBP', 'Lebanese Pound'), ('ALL', 'Lek'), ('HNL', 'Lempira'), ('SLL', 'Leone'), ('LSL', 'Lesotho loti'), ('LRD', 'Liberian Dollar'), ('LYD', 'Libyan Dinar'), ('SZL', 'Lilangeni'), ('LTL', 'Lithuanian Litas'), ('MGA', 'Malagasy Ariary'), ('MWK', 'Malawian Kwacha'), ('MYR', 'Malaysian Ringgit'), ('TMM', 'Manat'), ('MUR', 'Mauritius Rupee'), ('MZN', 'Metical'), ('MXV', 'Mexican Unidad de Inversion (UDI)'), ('MXN', 'Mexican peso'), ('MDL', 'Moldovan Leu'), ('MAD', 'Moroccan Dirham'), ('BOV', 'Mvdol'), ('NGN', 'Naira'), ('ERN', 'Nakfa'), ('NAD', 'Namibian Dollar'), ('NPR', 'Nepalese Rupee'), ('ANG', 'Netherlands Antillian Guilder'), ('ILS', 'New Israeli Sheqel'), ('RON', 'New Leu'), ('TWD', 'New Taiwan Dollar'), ('NZD', 'New Zealand Dollar'), ('KPW', 'North Korean Won'), ('NOK', 'Norwegian Krone'), ('PEN', 'Nuevo Sol'), ('MRO', 'Ouguiya'), ('TOP', 'Paanga'), ('PKR', 'Pakistan Rupee'), ('XPD', 'Palladium'), ('MOP', 'Pataca'), ('PHP', 'Philippine Peso'), ('XPT', 'Platinum'), ('GBP', 'Pound Sterling'), ('BWP', 'Pula'), ('QAR', 'Qatari Rial'), ('GTQ', 'Quetzal'), ('ZAR', 'Rand'), ('OMR', 'Rial Omani'), ('KHR', 'Riel'), ('MVR', 'Rufiyaa'), ('IDR', 'Rupiah'), ('RUB', 'Russian Ruble'), ('RWF', 'Rwanda Franc'), ('XDR', 'SDR'), ('SHP', 'Saint Helena Pound'), ('SAR', 'Saudi Riyal'), ('RSD', 'Serbian Dinar'), ('SCR', 'Seychelles Rupee'), ('XAG', 'Silver'), ('SGD', 'Singapore Dollar'), ('SBD', 'Solomon Islands Dollar'), ('KGS', 'Som'), ('SOS', 'Somali Shilling'), ('TJS', 'Somoni'), ('SSP', 'South Sudanese Pound'), ('LKR', 'Sri Lanka Rupee'), ('XSU', 'Sucre'), ('SDG', 'Sudanese Pound'), ('SRD', 'Surinam Dollar'), ('SEK', 'Swedish Krona'), ('CHF', 'Swiss Franc'), ('SYP', 'Syrian Pound'), ('BDT', 'Taka'), ('WST', 'Tala'), ('TZS', 'Tanzanian Shilling'), ('KZT', 'Tenge'), ('XXX', 'The codes assigned for transactions where no currency is involved'), ('TTD', 'Trinidad and Tobago Dollar'), ('MNT', 'Tugrik'), ('TND', 'Tunisian Dinar'), ('TRY', 'Turkish Lira'), ('TMT', 'Turkmenistan New Manat'), ('TVD', 'Tuvalu dollar'), ('AED', 'UAE Dirham'), ('XFU', 'UIC-Franc'), ('USD', 'US Dollar'), ('USN', 'US Dollar (Next day)'), ('UGX', 'Uganda Shilling'), ('CLF', 'Unidad de Fomento'), ('COU', 'Unidad de Valor Real'), ('UYI', 'Uruguay Peso en Unidades Indexadas (URUIURUI)'), ('UYU', 'Uruguayan peso'), ('UZS', 'Uzbekistan Sum'), ('VUV', 'Vatu'), ('CHE', 'WIR Euro'), ('CHW', 'WIR Franc'), ('KRW', 'Won'), ('YER', 'Yemeni Rial'), ('JPY', 'Yen'), ('CNY', 'Yuan Renminbi'), ('ZMK', 'Zambian Kwacha'), ('ZMW', 'Zambian Kwacha'), ('ZWD', 'Zimbabwe Dollar A/06'), ('ZWN', 'Zimbabwe dollar A/08'), ('ZWL', 'Zimbabwe dollar A/09'), ('PLN', 'Zloty')], default='USD', editable=False, max_length=3), + field=djmoney.models.fields.CurrencyField(choices=common.settings.all_currency_codes(), default=common.settings.currency_code_default(), editable=False, max_length=3), ), ] diff --git a/InvenTree/stock/migrations/0055_auto_20201117_1453.py b/InvenTree/stock/migrations/0055_auto_20201117_1453.py index 265347ae5c..12ad90cc0a 100644 --- a/InvenTree/stock/migrations/0055_auto_20201117_1453.py +++ b/InvenTree/stock/migrations/0055_auto_20201117_1453.py @@ -2,6 +2,7 @@ from django.db import migrations import djmoney.models.fields +import common.settings class Migration(migrations.Migration): @@ -14,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='stockitem', name='purchase_price', - field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency='USD', help_text='Single unit purchase price at time of purchase', max_digits=19, null=True, verbose_name='Purchase Price'), + field=djmoney.models.fields.MoneyField(blank=True, decimal_places=4, default_currency=common.settings.currency_code_default(), help_text='Single unit purchase price at time of purchase', max_digits=19, null=True, verbose_name='Purchase Price'), ), ]