refactor(finance/models): replace `submitted` and `confirmed` by a `status` field (#3)
This is in preparation for a [new statement admin view](#179).testing
parent
c6bf9fe915
commit
69a4560ea4
@ -0,0 +1,39 @@
|
|||||||
|
# Generated by Django 4.2.20 on 2025-10-11 15:43
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
def set_status_from_old_fields(apps, schema_editor):
|
||||||
|
"""
|
||||||
|
Set the status field based on the existing submitted and confirmed fields.
|
||||||
|
- If confirmed is True, status = CONFIRMED (2)
|
||||||
|
- If submitted is True but confirmed is False, status = SUBMITTED (1)
|
||||||
|
- Otherwise, status = UNSUBMITTED (0)
|
||||||
|
"""
|
||||||
|
Statement = apps.get_model('finance', 'Statement')
|
||||||
|
UNSUBMITTED, SUBMITTED, CONFIRMED = 0, 1, 2
|
||||||
|
|
||||||
|
for statement in Statement.objects.all():
|
||||||
|
if statement.confirmed:
|
||||||
|
statement.status = CONFIRMED
|
||||||
|
elif statement.submitted:
|
||||||
|
statement.status = SUBMITTED
|
||||||
|
else:
|
||||||
|
statement.status = UNSUBMITTED
|
||||||
|
statement.save(update_fields=['status'])
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('finance', '0009_statement_ljp_to'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='statement',
|
||||||
|
name='status',
|
||||||
|
field=models.IntegerField(choices=[(0, 'In preparation'), (1, 'Submitted'), (2, 'Confirmed')], default=0, verbose_name='Status'),
|
||||||
|
),
|
||||||
|
migrations.RunPython(set_status_from_old_fields, reverse_code=migrations.RunPython.noop),
|
||||||
|
]
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
# Generated by Django 4.2.20 on 2025-10-11 16:01
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('finance', '0010_statement_status'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='statement',
|
||||||
|
name='confirmed',
|
||||||
|
),
|
||||||
|
migrations.RemoveField(
|
||||||
|
model_name='statement',
|
||||||
|
name='submitted',
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -1,3 +1,4 @@
|
|||||||
from .admin import *
|
from .admin import *
|
||||||
from .models import *
|
from .models import *
|
||||||
from .rules import *
|
from .rules import *
|
||||||
|
from .migrations import *
|
||||||
|
|||||||
@ -0,0 +1,70 @@
|
|||||||
|
import django.test
|
||||||
|
from django.apps import apps
|
||||||
|
from django.db import connection
|
||||||
|
from django.db.migrations.executor import MigrationExecutor
|
||||||
|
|
||||||
|
|
||||||
|
class StatusMigrationTestCase(django.test.TransactionTestCase):
|
||||||
|
"""Test the migration from submitted/confirmed fields to status field."""
|
||||||
|
|
||||||
|
app = 'finance'
|
||||||
|
migrate_from = [('finance', '0009_statement_ljp_to')]
|
||||||
|
migrate_to = [('finance', '0010_statement_status')]
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# Get the state before migration
|
||||||
|
executor = MigrationExecutor(connection)
|
||||||
|
executor.migrate(self.migrate_from)
|
||||||
|
|
||||||
|
# Get the old models (before migration)
|
||||||
|
old_apps = executor.loader.project_state(self.migrate_from).apps
|
||||||
|
self.Statement = old_apps.get_model(self.app, 'Statement')
|
||||||
|
|
||||||
|
# Create statements with different combinations of submitted/confirmed
|
||||||
|
# created_by is nullable, so we don't need to create a Member
|
||||||
|
self.unsubmitted = self.Statement.objects.create(
|
||||||
|
short_description='Unsubmitted Statement',
|
||||||
|
submitted=False,
|
||||||
|
confirmed=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.submitted = self.Statement.objects.create(
|
||||||
|
short_description='Submitted Statement',
|
||||||
|
submitted=True,
|
||||||
|
confirmed=False
|
||||||
|
)
|
||||||
|
|
||||||
|
self.confirmed = self.Statement.objects.create(
|
||||||
|
short_description='Confirmed Statement',
|
||||||
|
submitted=True,
|
||||||
|
confirmed=True
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_status_field_migration(self):
|
||||||
|
"""Test that status field is correctly set from old submitted/confirmed fields."""
|
||||||
|
# Run the migration
|
||||||
|
executor = MigrationExecutor(connection)
|
||||||
|
executor.loader.build_graph()
|
||||||
|
executor.migrate(self.migrate_to)
|
||||||
|
|
||||||
|
# Get the new models (after migration)
|
||||||
|
new_apps = executor.loader.project_state(self.migrate_to).apps
|
||||||
|
Statement = new_apps.get_model(self.app, 'Statement')
|
||||||
|
|
||||||
|
# Constants from the Statement model
|
||||||
|
UNSUBMITTED = 0
|
||||||
|
SUBMITTED = 1
|
||||||
|
CONFIRMED = 2
|
||||||
|
|
||||||
|
# Verify the migration worked correctly
|
||||||
|
unsubmitted = Statement.objects.get(pk=self.unsubmitted.pk)
|
||||||
|
self.assertEqual(unsubmitted.status, UNSUBMITTED,
|
||||||
|
'Statement with submitted=False, confirmed=False should have status=UNSUBMITTED')
|
||||||
|
|
||||||
|
submitted = Statement.objects.get(pk=self.submitted.pk)
|
||||||
|
self.assertEqual(submitted.status, SUBMITTED,
|
||||||
|
'Statement with submitted=True, confirmed=False should have status=SUBMITTED')
|
||||||
|
|
||||||
|
confirmed = Statement.objects.get(pk=self.confirmed.pk)
|
||||||
|
self.assertEqual(confirmed.status, CONFIRMED,
|
||||||
|
'Statement with submitted=True, confirmed=True should have status=CONFIRMED')
|
||||||
Loading…
Reference in New Issue