Skip to content

Commit

Permalink
[#3086] Create data migration to add defaults for ContactFormSubject …
Browse files Browse the repository at this point in the history
…klant configs
  • Loading branch information
pi-sigma committed Mar 4, 2025
1 parent 0baddce commit a5c84a3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.2.18 on 2025-03-04 13:08

from django.db import migrations


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.all():
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.all():
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,
)
]
34 changes: 34 additions & 0 deletions src/open_inwoner/openklant/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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

def test_set_default_config(self):
ContactFormSubject = self.apps.get_model("openklant", "ContactFormSubject")
ESuiteKlantConfig = self.apps.get_model("openklant", "ESuiteKlantConfig")
OpenKlant2Config = self.apps.get_model("openklant", "OpenKlant2Config")

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
)

0 comments on commit a5c84a3

Please sign in to comment.