Skip to content

Commit

Permalink
Add or update item codes with version
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelglenister committed Nov 2, 2023
1 parent 08ccd68 commit ef8e67c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
4 changes: 2 additions & 2 deletions municipal_finance/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)),
],
Expand All @@ -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'),
),
]
5 changes: 5 additions & 0 deletions municipal_finance/models/financial_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .small_auto_field import SmallAutoField
from .amount_type import AmountTypeV2
from .updates import ItemCodeSchema


class BsheetItems(models.Model):
Expand Down Expand Up @@ -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"
Expand All @@ -72,6 +76,7 @@ class Meta:
def __str__(self):
return self.code


class FinancialPositionFactsV2(BsheetFacts):
item = models.ForeignKey(
FinancialPositionItemsV2,
Expand Down
2 changes: 1 addition & 1 deletion municipal_finance/models/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class ItemCodeSchema(models.Model):
version = models.TextField()
version = models.CharField(max_length=10)

class Meta:
abstract = True
2 changes: 1 addition & 1 deletion municipal_finance/models/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions municipal_finance/update/item_code_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ef8e67c

Please sign in to comment.