Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: container replace gettext_lazy with gettext #164

Merged
merged 3 commits into from
Jun 7, 2024

Conversation

florianRepenn
Copy link
Contributor

@florianRepenn florianRepenn commented Jun 6, 2024

Description

Replacing gettext_lazy with gettext in djangocms_bootstrap4/contrib/bootstrap4_grid/models.py

@fsbraun fsbraun changed the title container: replace gettext_lazy with gettext fix: container replace gettext_lazy with gettext Jun 6, 2024
@fsbraun
Copy link
Sponsor Member

fsbraun commented Jun 6, 2024

@florianRepenn Thanks for reporting and submitting the fix!

Have you tested this? I believe the problem lies elsewhere. Maybe you can check?

Here container types are imported from the settings

choice = get_choices_match(GRID_CONTAINER_CHOICES, self.container_type)

This might need to be run through force_str to make sure the lazy translations from the settings are resolved.

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jun 6, 2024

I might be wrong, of course. Can you share the traceback?

@florianRepenn
Copy link
Contributor Author

@fsbraun Thanks for replying so fast.
I tested my fix by changing the file in my running application container and I got rid of the exception.

I just tested to wrap the line you provided in the force_str function but that didn't has any effect. (Also not sure if I understood correct)

Here is the traceback:

Template error:
In template /usr/local/lib/python3.9/dist-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 26
   __str__ returned non-string (type __proxy__)
   16 :                                 {{ field.label_tag }}
   17 :                                 {% if field.is_readonly %}
   18 :                                     <div class="readonly">{{ field.contents }}</div>
   19 :                                 {% else %}
   20 :                                     {{ field.field }}
   21 :                                 {% endif %}
   22 :                             {% endif %}
   23 :                         </div>
   24 :                     {% if field.field.help_text %}
   25 :                         <div class="help"{% if field.field.id_for_label %} id="{{ field.field.id_for_label }}_helptext"{% endif %}>
   26 :                             <div> {{ field.field.help_text|safe }} </div>
   27 :                         </div>
   28 :                     {% endif %}
   29 :                 </div>
   30 :             {% endfor %}
   31 :             {% if not line.fields|length == 1 %}</div>{% endif %}
   32 :         </div>
   33 :     {% endfor %}
   34 : </fieldset>
   35 : 

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.9/dist-packages/django/core/handlers/base.py", line 220, in _get_response
    response = response.render()
  File "/usr/local/lib/python3.9/dist-packages/django/template/response.py", line 111, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python3.9/dist-packages/django/template/response.py", line 89, in rendered_content
    return template.render(context, self._request)
  File "/usr/local/lib/python3.9/dist-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 175, in render
    return self._render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 157, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 63, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/loader_tags.py", line 208, in render
    return template.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 177, in render
    return self._render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 167, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/defaulttags.py", line 238, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/defaulttags.py", line 321, in render
    return nodelist.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1005, in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 966, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 1064, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python3.9/dist-packages/django/template/base.py", line 742, in resolve
    new_obj = func(obj, *arg_vals)
  File "/usr/local/lib/python3.9/dist-packages/django/template/defaultfilters.py", line 47, in _dec
    first = str(first)

Exception Type: TypeError at /de/admin/cms/page/add-plugin/
Exception Value: __str__ returned non-string (type __proxy__)

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jun 6, 2024

Ok, I seem to have been wrong here.

It is not good practice to use gettext for model declarations, since anyone with a non-English setup will create migrations.

The next thing I noticed is that the container's help text was doubly lazy: mark_safe_lazy on the outside and gettext_lazy on the inside. I created a patch for your patch to fix this. Would you like to give it a try?

@florianRepenn
Copy link
Contributor Author

Yes of cause. I can test it in my setup. Where can I find it?

@fsbraun
Copy link
Sponsor Member

fsbraun commented Jun 7, 2024

It should be a pr in your fork.

Remove doubly-lazy help text
@florianRepenn
Copy link
Contributor Author

This works for me. I approved the PR and merged it.

@fsbraun fsbraun merged commit 49983f4 into django-cms:master Jun 7, 2024
7 of 19 checks passed
@fsbraun
Copy link
Sponsor Member

fsbraun commented Jun 7, 2024

@florianRepenn Welcome as a new django CMS contributor 🚀!!
I'd be delighted if you joined our Discord group: https://discord-contributors-channel.django-cms.org/

@florianRepenn
Copy link
Contributor Author

Thanks a lot @fsbraun!

@wesleyboar
Copy link
Contributor

Thanks for this. This fixes my app having been unable to create or edit Containers. I've installed off of the master branch merge commit for now.

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.

3 participants