Skip to content

Commit

Permalink
💩 [#4826] Work around translation issue
Browse files Browse the repository at this point in the history
Added some introspection in the migration to properly evaluate the translated
default values for the newly added configuration model fields.
  • Loading branch information
sergei-maertens committed Nov 15, 2024
1 parent d91a771 commit e705a98
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()],
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -153,4 +188,5 @@ class Migration(migrations.Migration):
verbose_name="submission confirmation title",
),
),
migrations.RunPython(update_translated_defaults, migrations.RunPython.noop),
]
6 changes: 4 additions & 2 deletions src/openforms/utils/translations.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
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.
When using literal :func:`gettext` or :func:`gettext_lazy` default values in
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

0 comments on commit e705a98

Please sign in to comment.