10BC0 fix template label nested translation (#6687) by jbazik · Pull Request #6896 · django-cms/django-cms · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@jbazik
Copy link
Contributor
@jbazik jbazik commented Sep 2, 2020

Description

Fixes a serious bug in (at least) 3.7.2 to dev that causes an exception in the admin when editing any page, when USE_I18N is set to False. Fixes #6687.

In utils/conf.get_templates, the inherit template label is stored as a gettext_lazy object:

        templates.append((constants.TEMPLATE_INHERITANCE_MAGIC, _('Inherit the template of the nearest ancestor')))

In models/pagemodel.py, all template label choices are stored as gettext_lazy objects:

    template_choices = [(x, y) for x, _(y) in get_cms_setting('TEMPLATES')]

This creates a nested lazy object, which results in an exception in django.template.base.render_value_in_context when it is coerced to a string:

            value = str(value)

This only happens when USE_I18N is False. In that case, the function stored in the lazy object is django.utils.translation.gettext_noop, which just returns it's argument. The str() constructor doesn't know what to do with the stored proxy object.

The docs suggest that CMS_TEMPLATES should contain translated labels, so the correct fix is to not apply translation when building the choices list in the Page model.

A test is included. Note that it is not enough to override_settings(USE_I18N=False) when testing, since the translation mechanism in django is set up during initialization and is not affected by the override. My test includes a mock to test against gettext_noop.

Related resources

Checklist

  • [ x] I have opened this pull request against develop
  • [ x] I have updated the CHANGELOG.rst
  • [x ] I have added or modified the tests when changing logic

@coveralls
Copy link
coveralls commented Sep 2, 2020

Coverage Status

Coverage remained the same at 78.215% when pulling c980a00 on jbazik:fix-template-labels-noi18n into 4d75b92 on divio:develop.

@FinalAngel
Copy link
Member

any feedback on this @Aiky30 or @victor-yunenko

@jbazik jbazik force-pushed the fix-template-labels-noi18n branch from 0d249bf to cb423f9 Compare September 8, 2020 04:02
Copy link
Contributor
@Aiky30 Aiky30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these changes fix when I18n is disabled but then break the scenario for the majority when it is enabled because the UI item will no longer be translated for users using a language other than English.

I think that building the template list should be different dependant on the I18N Setting. This then protects both use cases.

@jbazik
Copy link
Contributor Author
jbazik commented Oct 1, 2020

When should the labels be translated? The docs indicate, by example (http://docs.django-cms.org/en/latest/reference/configuration.html#cms-templates), that they should be translated when configured. In that case, my fix is correct for both cases. If instead sites in all languages should configure the labels untranslated, then you are right.

There is an alternative fix: storing the TEMPLATE_INHERITANCE_MAGIC label untranslated. That works only because nested gettext doesn't break with I18N turned on. But it's the least disruptive. If you'd prefer that, would you like me to change the docs, too, or leave them as-is?

John

@Aiky30
Copy link
Contributor
Aiky30 commented Oct 2, 2020

There is an alternative fix: storing the TEMPLATE_INHERITANCE_MAGIC label untranslated. That works only because nested gettext doesn't break with I18N turned on. But it's the least disruptive. If you'd prefer that, would you like me to change the docs, too, or leave them as-is?

It was the template inheritance text that I thought would have then been broken but you're right that it's stored with translations active which we should keep: https://github.com/divio/django-cms/blob/c4b48fab0f84c237b436442039137ac1921e2093/cms/utils/conf.py#L168

I was thinking of a simple if else condition but you're right that it is covered already so no action needed, the test looks sane. I'm happy.

My apologies for the confusion.

Andrew

@Aiky30
Copy link
Contributor
Aiky30 commented Oct 2, 2020

@jbazik Can you bring the branch back up to date so that we can see the build pass. Once passed I will re-review and accept.

If you could add a changelog entry whilst you are making changes that would help too please.

@jbazik jbazik force-pushed the fix-template-labels-noi18n branch 2 times, most recently from 9776797 to c980a00 Compare October 3, 2020 03:27
@jbazik
Copy link
Contributor Author
jbazik commented Oct 3, 2020

I added a changelog entry and rebased.

Copy link
Contributor
@Aiky30 Aiky30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this change.

@Aiky30
Copy link
Contributor
Aiky30 commented Oct 3, 2020

@FinalAngel I'm happy with the change.

@dhiashalabi
Copy link
dhiashalabi commented Dec 17, 2020

I am happy with these changes

@FinalAngel
Copy link
Member

👍 will merge once the tests pass

@marksweb
Copy link
Member
marksweb commented Feb 3, 2021

@jbazik Looks like this might benefit from another merge of develop to pick up the differences in the coverage and coveralls requirements.

@jbazik jbazik force-pushed the fix-template-labels-noi18n branch from 48d6964 to a10f6ca Compare February 17, 2021 04:40
@jbazik
Copy link
Contributor Author
jbazik commented Feb 17, 2021

@marksweb done - thanks

@marksweb
Copy link
Member

@FinalAngel This is now passing after being updated

Copy link
Contributor
@Aiky30 Aiky30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the nature of this change i.e. anyone who didn't follow the docs on translations can we please add a changelog entry, this will most likely make it into django-cms 3.9.x and could be considered a breaking change. My apologies, we have recently agreed that no changes should be coming in without a changelog entry, especially if there are potentially breaking changes. I'm sure you would appreciate this if the change affected your project.

I will personally take responsibility for getting the change merged ASAP with a changelog entry in place.

Thank you.

@Aiky30 Aiky30 self-assigned this Feb 17, 2021
@Aiky30 Aiky30 added this to the 3.9.x milestone Feb 17, 2021
@jbazik jbazik force-pushed the fix-template-labels-noi18n branch from a10f6ca to 2970cc2 Compare February 17, 2021 16 8000 :02
@jbazik
Copy link
Contributor Author
jbazik commented Feb 17, 2021

@Aiky30 added changelog entry, thanks.

Copy link
Contributor
@Aiky30 Aiky30 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with the changes, just needs the tests to pass now.

@jbazik jbazik force-pushed the fix-template-labels-noi18n branch from ca7c387 to e1a5946 Compare March 25, 2021 23:02
@jbazik
Copy link
Contributor Author
jbazik commented Mar 25, 2021

Rebased against develop again.

@marksweb
Copy link
Member

@victor-yunenko This PR requires a review from you to get wrapped up.

@vinitkumar vinitkumar merged commit 4012e39 into django-cms:develop Apr 15, 2021
@goutnet goutnet modified the milestones: 3.9.x, 3.8.1, 3.8.x May 11, 2021
4wrk added a commit to 4wrk/django-cms that referenced this pull request Dec 26, 2021
Fixes a serious bug in 3.8.0 to that causes an exception in the admin when editing any page, when USE_I18N is set to False. Fixes django-cms#6687
Details: django-cms#6896 (comment)
@sparrowme
Copy link
Contributor

This happens for me in v5.0.0 has it recurred?
If I only toggle the referenced setting to True the error disappears and reverting back to False reproduces error.

@fsbraun
Copy link
Member
fsbraun commented May 19, 2025

@sparrowme Interestingly, it has. :-(

@fsbraun
Copy link
Member
fsbraun commented May 19, 2025

@sparrowme Where exactly did you encounter the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Advanced page settings not working
0