Monday, November 26, 2012

Dealing with unicode user name in OSQA

OSQA is a pretty engine for creating Q&A site for in you community.

I use it for a couple of weeks and i'm very happy with it.
But some day ago i start receiving letter from my user in that they say that it is a bug on site, and user cannot fill additional field in user profile.
After investigation i found a bug.
The root of problem was unicode username.

Link for user settings contain slug field and look like:
url(r'^%s(?P<id>\d+)/(?P<slug>.*)/%s$' % (_('users/'), _('edit/')), app.users.edit_user, name='edit_user'),
and in actual form url it look like
<form name="" action="/users/1/admin/edit/" method="post">
but when you user have unicode user name it look like:
<form name="" action="/users/21//edit/" method="post">
and when user with unicode username try to submit form system do redirect 302 instead of saving data redirect them to correct url for editing (but url in form still same wrong).

i look to the template for edit preference page(forum\skins\default\templates\users\edit.html:36):

<form name="" action="{% url edit_user user.id user.username|slugify %}" method="post">

so template filter slugify does not work with unicode :(

So i created new one :)

in file forum\templatetags\extra_filters.py i add new tag

from django.utils.encoding import smart_unicode
from django.template.defaultfilters import slugify
@register.filter
def slug(value):
    return slugify(smart_unicode(value))
and use them instead |slugify 
i add 
{% load extra_filters %}
in top of template and change filter in form action link:
<form name="" action="{% url edit_user user.id user.username|slug %}" method="post">

And now my users are happy :)




No comments:

Post a Comment