Unicode issue in or_me filter tag

Issue #5 resolved
Alexei Gousev created an issue

postman/templatetags/postman_tags.py, line 35: {{{ #!python

return _('<me>') if str(value) == str(arg) else value }}}

If value contains Unicode characters (such as my case of u'Dami\xe1n Hiller'), this causes an exception and the whole thing crashes:

{{{ #!error

Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\xe1' in position 4: ordinal not in range(128) }}}

Comments (8)

  1. Patrick Samson repo owner

    I can't reproduce the problem, I'm able to see something like "tá3" in the sender column. Give some details and context, check the encoding setting everywhere (DB, templates, .py)

  2. Alexei Gousev reporter

    I'm still don't quite understand how the problem isn't in the or_me() function... I know my database is right since it shows up correctly elsewhere, and I'm using a template almost identical to your default one (just some minor HTML changes). Regardless of what happens in the templates though, at the bottom of my traceback it's still obviously trying to call str(value) when value = u'Jos\xe9 Testuser', which results in the exception.

    When you successfully display "tá3" in the sender column, is the or_me() function being called at all? If so, why isn't it crashing?

  3. Patrick Samson repo owner
    • changed status to open

    Yes, in my example, the or_me() filter is called. I guess the problem comes from how you use the filter in your template. Please paste the extract of the template. In the default template provided with the app, you're supposed to give the filter a str() of a User object, which results to a UTF-8 string, not a unicode string.

  4. Alexei Gousev reporter

    Ah, problem solved :)

    I changed to "message.obfuscated_sender|or_me:user" "message.sender.get_full_name|or_me:user", which introduced in the Unicode stuff. I just got rid of calling the or_me filter altogether in the template to fix it.

    Thanks!

  5. Patrick Samson repo owner

    I just made some improvement in the comparison management of the filter. Your writing as "message.sender.get_full_name|or_me:user" will now no more raise an error, but it still doesn't make sense: not even speaking of encodings, you are comparing something made of User.first_name + User.last_name with a representation of a User which is based on User.username -> very little chance to be equals unless you enforce a strict policy of dependence between these fields.

    You should know that get_full_name() has the risk to be empty. But anyway, if you want, now you can write: "message.sender.get_full_name|or_me:user.get_full_name".

    Please update to the latest commit and confirm it works.

  6. Log in to comment