From 44e2a61e32520044ebfd289d3b5b91551d3eafba Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 15 Nov 2024 15:39:08 +0100 Subject: [PATCH] :poop: [#4826] Work around translation issue Added some introspection in the migration to properly evaluate the translated default values for the newly added configuration model fields. --- ...bmission_confirmation_template_and_more.py | 58 +++++++++++++++---- src/openforms/utils/translations.py | 6 +- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/openforms/config/migrations/0065_globalconfiguration_cosign_submission_confirmation_template_and_more.py b/src/openforms/config/migrations/0065_globalconfiguration_cosign_submission_confirmation_template_and_more.py index 3e8aeb5e5f..25af2fdd35 100644 --- a/src/openforms/config/migrations/0065_globalconfiguration_cosign_submission_confirmation_template_and_more.py +++ b/src/openforms/config/migrations/0065_globalconfiguration_cosign_submission_confirmation_template_and_more.py @@ -1,11 +1,46 @@ # Generated by Django 4.2.16 on 2024-11-15 10:26 -from django.db import migrations, models import functools + +from django.conf import settings +from django.db import migrations, models +from django.db.migrations.state import StateApps +from django.utils import translation +from django.utils.translation import gettext + +import tinymce.models + import openforms.config.models.config import openforms.template.validators import openforms.utils.translations -import tinymce.models + + +def update_translated_defaults(apps: StateApps, _): + """ + Set the properly translated default config field values. + + Workaround for https://github.com/open-formulieren/open-forms/issues/4826 + """ + GlobalConfiguration = apps.get_model("config", "GlobalConfiguration") + config = GlobalConfiguration.objects.first() + if config is None: + return + + for field in ( + "cosign_submission_confirmation_template", + "cosign_submission_confirmation_title", + "submission_confirmation_title", + ): + for lang, _ in settings.LANGUAGES: + with translation.override(lang): + default_callback = config._meta.get_field(field).default + assert isinstance(default_callback, functools.partial) + if default_callback.func is openforms.utils.translations.get_default: + default_callback = functools.partial( + gettext, *default_callback.args + ) + setattr(config, f"{field}_{lang}", default_callback()) + config.save() class Migration(migrations.Migration): @@ -22,7 +57,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.config.models.config._render, *("config/default_cosign_submission_confirmation.html",), - **{} + **{}, ), help_text="The content of the submission confirmation page for submissions requiring cosigning. The variables 'public_reference' and 'cosigner_email' are available. We strongly advise you to include the 'public_reference' in case users need to contact the customer service.", validators=[openforms.template.validators.DjangoTemplateValidator()], @@ -36,7 +71,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.config.models.config._render, *("config/default_cosign_submission_confirmation.html",), - **{} + **{}, ), help_text="The content of the submission confirmation page for submissions requiring cosigning. The variables 'public_reference' and 'cosigner_email' are available. We strongly advise you to include the 'public_reference' in case users need to contact the customer service.", null=True, @@ -51,7 +86,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.config.models.config._render, *("config/default_cosign_submission_confirmation.html",), - **{} + **{}, ), help_text="The content of the submission confirmation page for submissions requiring cosigning. The variables 'public_reference' and 'cosigner_email' are available. We strongly advise you to include the 'public_reference' in case users need to contact the customer service.", null=True, @@ -66,7 +101,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Request not complete yet",), - **{} + **{}, ), help_text="The content of the confirmation page title for submissions requiring cosigning.", max_length=200, @@ -81,7 +116,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Request not complete yet",), - **{} + **{}, ), help_text="The content of the confirmation page title for submissions requiring cosigning.", max_length=200, @@ -97,7 +132,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Request not complete yet",), - **{} + **{}, ), help_text="The content of the confirmation page title for submissions requiring cosigning.", max_length=200, @@ -113,7 +148,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Confirmation: {{ public_reference }}",), - **{} + **{}, ), help_text="The content of the confirmation page title. You can (and should) use the 'public_reference' variable so the users have a reference in case they need to contact the customer service.", max_length=200, @@ -128,7 +163,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Confirmation: {{ public_reference }}",), - **{} + **{}, ), help_text="The content of the confirmation page title. You can (and should) use the 'public_reference' variable so the users have a reference in case they need to contact the customer service.", max_length=200, @@ -144,7 +179,7 @@ class Migration(migrations.Migration): default=functools.partial( openforms.utils.translations.get_default, *("Confirmation: {{ public_reference }}",), - **{} + **{}, ), help_text="The content of the confirmation page title. You can (and should) use the 'public_reference' variable so the users have a reference in case they need to contact the customer service.", max_length=200, @@ -153,4 +188,5 @@ class Migration(migrations.Migration): verbose_name="submission confirmation title", ), ), + migrations.RunPython(update_translated_defaults, migrations.RunPython.noop), ] diff --git a/src/openforms/utils/translations.py b/src/openforms/utils/translations.py index aed06782b0..b682d2ae21 100644 --- a/src/openforms/utils/translations.py +++ b/src/openforms/utils/translations.py @@ -1,12 +1,14 @@ from functools import partial from typing import Callable +from django.utils.functional import Promise + def get_default(value) -> str: return str(value) -def runtime_gettext(literal) -> Callable[[], str]: +def runtime_gettext(literal: Promise) -> Callable[[], str]: """ Generate a callable for migration defaults resolving to a translated literal. @@ -14,7 +16,7 @@ def runtime_gettext(literal) -> Callable[[], str]: migrations, the defaults are evaluated and frozen in the migration files. By using a callable, we can defer this, see - https://docs.djangoproject.com/en/2.2/topics/migrations/#serializing-values + https://docs.djangoproject.com/en/4.2/topics/migrations/#serializing-values """ func = partial(get_default, literal) return func