gaqzi / django-emoji

A simple django app to use emojis on your website ⛺
https://pypi.python.org/pypi/django-emoji
MIT License
109 stars 19 forks source link

Emoji.replace_unicode() is broken on python2 #20

Open jrabbit opened 7 years ago

jrabbit commented 7 years ago

Opening an issue while I investigate this further. Seems to work as expected in python3(.6) in python 3:

In [2]: Emoji.replace_unicode(u'Emoji test \U0001f408\U0001f52b\U0001f480')
Out[2]: 'Emoji test <img src="/static/emoji/img/cat2.png" alt="🐈" title="cat2" class="emoji"><img src="/static/emoji/img/gun.png" alt="🔫" title="gun" class="emoji"><img src="/static/emoji/img/skull.png" alt="💀" title="skull" class="emoji">'

in 2:

In [31]: Emoji.replace_unicode(u'\U0001f408\U0001f52b\U0001f480')
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-31-1ffa4d7f2641> in <module>()
----> 1 Emoji.replace_unicode(u'\U0001f408\U0001f52b\U0001f480')

/usr/local/lib/python2.7/site-packages/emoji/models.pyc in replace_unicode(cls, replacement_string)
    180             if name:
    181                 if settings.EMOJI_ALT_AS_UNICODE:
--> 182                     character = e._image_string(name, alt=character)
    183                 else:
    184                     character = e._image_string(name)

/usr/local/lib/python2.7/site-packages/emoji/models.pyc in _image_string(self, filename, alt)
    108             self._static_url(filename),
    109             alt or title,
--> 110             title,
    111         )
    112 

UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f408' in position 0: ordinal not in range(128)
jrabbit commented 7 years ago

Ok further complexity... it works on a fresh project so I think there's a type mismatch with the recommended settings versus the default