question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CMSPluginBase should use force_text and force_str for __str__ and __repr__

See original GitHub issue

When working on aldryn_bootstrap3 plugin, I stumbled upon a following error:

In [18]: c = CMSPlugin.objects.get(id=6061)
In [24]: c.get_plugin_class()
Out[24]: aldryn_bootstrap3.cms_plugins.Bootstrap3ColumnCMSPlugin

In [21]: c.get_plugin_name()
Out[21]: <django.utils.functional.__proxy__ at 0x7fd8d4e2a610>

In [22]: c.get_plugin_instance()
Out[22]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    697                 type_pprinters=self.type_printers,
    698                 deferred_pprinters=self.deferred_printers)
--> 699             printer.pretty(obj)
    700             printer.flush()
    701             return stream.getvalue()

/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
    366                 if cls in self.type_pprinters:
    367                     # printer registered in self.type_pprinters
--> 368                     return self.type_pprinters[cls](obj, self, cycle)
    369                 else:
    370                     # deferred printer

/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in inner(obj, p, cycle)
    550                 p.text(',')
    551                 p.breakable()
--> 552             p.pretty(x)
    553         if len(obj) == 1 and type(obj) is tuple:
    554             # Special case for 1-item tuples.

/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
    381                             if callable(meth):
    382                                 return meth(obj, self, cycle)
--> 383             return _default_pprint(obj, self, cycle)
    384         finally:
    385             self.end_group()

/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
    501     if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
    502         # A user-provided repr. Find newlines and replace them with p.break_()
--> 503         _repr_pprint(obj, p, cycle)
    504         return
    505     p.begin_group(1, '<')

/home/zan/.virtualenvs/datafy-cms/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _repr_pprint(obj, p, cycle)
    692     """A pprint that just redirects to the normal repr function."""
    693     # Find newlines and replace them with p.break_()
--> 694     output = repr(obj)
    695     for idx,output_line in enumerate(output.splitlines()):
    696         if idx:

TypeError: __repr__ returned non-string (type __proxy__)

This exception also gets raised when trying to publish some changes. Looking at the code, the problem seems to be in CMSPluginBase : https://github.com/divio/django-cms/blob/develop/cms%2Fplugin_base.py#L347-L351

Because __str__ and __repr__ don’t use force_text and force_str, the lazy translation causes an error. Therefore, CMSPluginBase should probably use force_text and force_str for __str__ and __repr__

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zanderlecommented, Feb 26, 2016

Or maybe get_plugin_name on CMSPlugin shouldn’t access name attribute on CMSPluginBase directly: https://github.com/divio/django-cms/blob/develop/cms/models/pluginmodel.py#L119

So instead of

def get_plugin_name(self):
    from cms.plugin_pool import plugin_pool

    return plugin_pool.get_plugin(self.plugin_type).name

it should be something like

def get_plugin_name(self):
    from cms.plugin_pool import plugin_pool

    return smart_text(plugin_pool.get_plugin(self.plugin_type))

Assuming that it’s ok to have name translatable…

0reactions
Aiky30commented, Dec 8, 2020

Appears to have been resolved and merged in the PR referenced above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CMSPluginBase should use force_text and force_str for __ ...
When working on aldryn_bootstrap3 plugin, I stumbled upon a following error: In [18]: c = CMSPlugin.objects.get(id=6061) In [24]: ...
Read more >
str() vs repr() in Python - GeeksforGeeks
repr () prints “official” representation of a date-time object (means using the “official” string representation we can reconstruct the object).
Read more >
python - What is the difference between __str__ and __repr__?
So one should first write a __repr__ that allows you to reinstantiate an equivalent object from the string it returns e.g. using eval...
Read more >
Python __str__() and __repr__() functions | DigitalOcean
This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression...
Read more >
str and repr | Pydon't - Mathspp
The str class is used when you want to convert something to the string type, and is also used when you need a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found