CMSPluginBase should use force_text and force_str for __str__ and __repr__
See original GitHub issueWhen 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:
- Created 8 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Or maybe
get_plugin_name
onCMSPlugin
shouldn’t accessname
attribute onCMSPluginBase
directly: https://github.com/divio/django-cms/blob/develop/cms/models/pluginmodel.py#L119So instead of
it should be something like
Assuming that it’s ok to have
name
translatable…Appears to have been resolved and merged in the PR referenced above.