diff --git a/src/open_inwoner/openklant/migrations/0026_contactform_subject_config_default.py b/src/open_inwoner/openklant/migrations/0026_contactform_subject_config_default.py new file mode 100644 index 0000000000..2128c31c1e --- /dev/null +++ b/src/open_inwoner/openklant/migrations/0026_contactform_subject_config_default.py @@ -0,0 +1,42 @@ +# Generated by Django 4.2.18 on 2025-03-04 13:08 + +from django.db import migrations +from django.db.models import Q + + +def set_default_for_klant_config(apps, _): + ContactFormSubject = apps.get_model("openklant", "ContactFormSubject") + ESuiteKlantConfig = apps.get_model("openklant", "ESuiteKlantConfig") + OpenKlant2Config = apps.get_model("openklant", "OpenKlant2Config") + + for contact_form_subject in ContactFormSubject.objects.filter( + Q(esuite_config__isnull=True) | Q(openklant_config__isnull=True) + ): + contact_form_subject.esuite_config = ESuiteKlantConfig.objects.first() + contact_form_subject.openklant_config = OpenKlant2Config.objects.first() + contact_form_subject.save() + + +def delete_default_for_klant_config(apps, _): + ContactFormSubject = apps.get_model("openklant", "ContactFormSubject") + + for contact_form_subject in ContactFormSubject.objects.filter( + Q(esuite_config__isnull=False) | Q(openklant_config__isnull=False) + ): + contact_form_subject.esuite_config = None + contact_form_subject.openklant_config = None + contact_form_subject.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ("openklant", "0025_esuiteklantconfig_send_klantcontact_confirmation_email"), + ] + + operations = [ + migrations.RunPython( + code=set_default_for_klant_config, + reverse_code=delete_default_for_klant_config, + ) + ] diff --git a/src/open_inwoner/openklant/tests/test_migrations.py b/src/open_inwoner/openklant/tests/test_migrations.py new file mode 100644 index 0000000000..ffd29b967d --- /dev/null +++ b/src/open_inwoner/openklant/tests/test_migrations.py @@ -0,0 +1,31 @@ +from open_inwoner.utils.tests.test_migrations import TestSuccessfulMigrations + + +class ContactFormSubjectConfigMigrationTest(TestSuccessfulMigrations): + migrate_from = "0025_esuiteklantconfig_send_klantcontact_confirmation_email" + migrate_to = "0026_contactform_subject_config_default" + app = "openklant" + + def setUpBeforeMigration(self, app): + ContactFormSubject = app.get_model("openklant", "ContactFormSubject") + ESuiteKlantConfig = app.get_model("openklant", "ESuiteKlantConfig") + OpenKlant2Config = app.get_model("openklant", "OpenKlant2Config") + + self.esuite_config_1 = ESuiteKlantConfig.objects.create() + self.esuite_config_2 = ESuiteKlantConfig.objects.create() + self.openklant_config_1 = OpenKlant2Config.objects.create() + self.openklant_config_2 = OpenKlant2Config.objects.create() + + for contact_form_subject in ContactFormSubject.objects.all(): + contact_form_subject.esuite_config = None + contact_form_subject.openklant_config = None + contact_form_subject.save() + + def test_set_default_config(self): + ContactFormSubject = self.apps.get_model("openklant", "ContactFormSubject") + + for contact_form_subject in ContactFormSubject.objects.all(): + self.assertEqual(contact_form_subject.esuite_config, self.esuite_config_1) + self.assertEqual( + contact_form_subject.openklant_config, self.openklant_config_1 + )