diff --git a/municipal_finance/admin.py b/municipal_finance/admin.py index 2ff693a99..a7e573877 100644 --- a/municipal_finance/admin.py +++ b/municipal_finance/admin.py @@ -324,8 +324,8 @@ class DemarcationChangesAdmin(admin.ModelAdmin): @admin.register(ItemCodeSchema) class ItemCodeSchemaAdmin(admin.ModelAdmin): - list_display = ("user", "datetime") - readonly_fields = ("user", "import_report") + list_display = ("user", "datetime", "version") + readonly_fields = ("user",) task_function = "municipal_finance.update.update_item_code_schema" task_name = "Item Code Schema update" diff --git a/municipal_finance/migrations/0046_auto_20231026_1653.py b/municipal_finance/migrations/0046_auto_20231102_1802.py similarity index 74% rename from municipal_finance/migrations/0046_auto_20231026_1653.py rename to municipal_finance/migrations/0046_auto_20231102_1802.py index e4aee3dd5..f440a27fd 100644 --- a/municipal_finance/migrations/0046_auto_20231026_1653.py +++ b/municipal_finance/migrations/0046_auto_20231102_1802.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.28 on 2023-10-26 14:53 +# Generated by Django 2.2.28 on 2023-11-02 16:02 from django.conf import settings from django.db import migrations, models @@ -20,7 +20,7 @@ class Migration(migrations.Migration): ('id', models.AutoField(primary_key=True, serialize=False)), ('datetime', models.DateTimeField(auto_now_add=True)), ('task_id', models.TextField(editable=False, null=True)), - ('import_report', models.TextField(null=True)), + ('version', models.CharField(max_length=10)), ('file', models.FileField(max_length=255, upload_to=municipal_finance.models.updates.UpdateFilePath())), ('user', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to=settings.AUTH_USER_MODEL)), ], @@ -29,4 +29,9 @@ class Migration(migrations.Migration): 'db_table': 'item_code_schema', }, ), + migrations.AddField( + model_name='financialpositionitemsv2', + name='version', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='municipal_finance.ItemCodeSchema'), + ), ] diff --git a/municipal_finance/models/financial_position.py b/municipal_finance/models/financial_position.py index 91deeb07f..ce83c06b0 100644 --- a/municipal_finance/models/financial_position.py +++ b/municipal_finance/models/financial_position.py @@ -2,6 +2,7 @@ from .small_auto_field import SmallAutoField from .amount_type import AmountTypeV2 +from .updates import ItemCodeSchema class BsheetItems(models.Model): @@ -64,6 +65,9 @@ class Meta: class FinancialPositionItemsV2(BsheetItems): id = SmallAutoField(primary_key=True) code = models.TextField(unique=True) + version = models.ForeignKey( + ItemCodeSchema, on_delete=models.CASCADE, blank=True, null=True + ) class Meta: db_table = "financial_position_items_v2" @@ -72,6 +76,7 @@ class Meta: def __str__(self): return self.code + class FinancialPositionFactsV2(BsheetFacts): item = models.ForeignKey( FinancialPositionItemsV2, diff --git a/municipal_finance/models/schema.py b/municipal_finance/models/schema.py index 359c700c7..fbb18dec0 100644 --- a/municipal_finance/models/schema.py +++ b/municipal_finance/models/schema.py @@ -2,7 +2,7 @@ class ItemCodeSchema(models.Model): - version = models.TextField() + version = models.CharField(max_length=10) class Meta: abstract = True diff --git a/municipal_finance/models/updates.py b/municipal_finance/models/updates.py index 7efb3effd..cffeefb7b 100644 --- a/municipal_finance/models/updates.py +++ b/municipal_finance/models/updates.py @@ -123,7 +123,7 @@ class ItemCodeSchema(models.Model): user = models.ForeignKey(User, models.DO_NOTHING) datetime = models.DateTimeField(auto_now_add=True) task_id = models.TextField(null=True, editable=False) - import_report = models.TextField(null=True) + version = models.CharField(max_length=10) file = models.FileField( upload_to=UpdateFilePath(), max_length=255, diff --git a/municipal_finance/update/item_code_schema.py b/municipal_finance/update/item_code_schema.py index f5d633545..89005482a 100644 --- a/municipal_finance/update/item_code_schema.py +++ b/municipal_finance/update/item_code_schema.py @@ -3,24 +3,24 @@ schema_codes = { - "finpos": "A6", - "capital": "SA34A", - "cashflow": "A7", - "incexp": "A4", + "A6": "finpos", + "SA34A": "capital", + "A7": "cashflow", + "A4": "incexp", } - def update_item_code_schema(update_obj, batch_size, **kwargs): print("__________") print("Update all item codes") + print(update_obj.id) file = default_storage.open(update_obj.file.name, "rb") workbook = xlrd.open_workbook(file_contents=file.read()) sheet = workbook.sheet_by_index(0) for i in range(sheet.nrows): - for j in range(sheet.ncols): - print(sheet.cell_value(i, j)) + if sheet.row_values(i)[0].strip() != "": + print(sheet.row_values(i)) # For each model of item code update add codes with the corresponding schema version # It should also be possible to update item codes of a matching schema version