divio/djangocms-picture — pull request #2 by nickstenning (2014-02-21 16:42:40+00:00)
Title: Ensure __unicode__ returns unicode for empty Pictures

The Picture model makes use of `cms.utils.compat.dj.python_2_unicode_compatible` in order to ensure
that it can be used in both Python 2 and 3. This means (somewhat counterintuitively, in my opinion) that the `__str__` method of the class must return unicode objects in Python 2. If it doesn't, then we can end up in a situation where the object's `__unicode__` method returns a byte string.

This in turn is a problem because instances of the Picture plugin are passed to Django's `django.utils.html.escapejs` function when created in the CMS user interface. This function throws TypeError if passed an object whose `__unicode__` method returns non-unicode, as documented at:

https://code.djangoproject.com/ticket/20856

In short, all possible return values of __str__ should be unicode instances. This commit adds a test for the specific case that was causing me problems, namely the empty Picture.