resource.py and authentication

2 views
Skip to first unread message

benm

unread,
Jan 9, 2009, 7:06:40 AM1/9/09
to Django REST interface
Hi,

I'm a bit confused by the following line 134 in resource.py :

response = HttpResponse(_('Authorization Required'),
mimetype=self.mimetype)

Is the underscore before _('Authorization Required') intentional?

I get "global name '_' is not defined" using a Resource with HTTP
basic authentication enabled, unless I remove it when it works OK.

Thanks for all your hard work on this project, it's very useful.

Ben

Malcolm Tredinnick

unread,
Jan 9, 2009, 7:19:47 AM1/9/09
to django-res...@googlegroups.com
On Fri, 2009-01-09 at 04:06 -0800, benm wrote:
> Hi,
>
> I'm a bit confused by the following line 134 in resource.py :
>
> response = HttpResponse(_('Authorization Required'),
> mimetype=self.mimetype)
>
> Is the underscore before _('Authorization Required') intentional?
>
> I get "global name '_' is not defined" using a Resource with HTTP
> basic authentication enabled, unless I remove it when it works OK.

That looks like a file that hasn't been ported properly to the latest
code. Once upon a time, Django automatically provided the gettext()
function via the alias _(). However, that changed after we added unicode
support, for a few different reasons, so code has to explicitly import
the right translation function. That particular function provides
internationalisation support: the error string will be translated into
the currently active language.

The correct solution is to add

from django.utils.translation import ugettext as _

to the top of the file. Looks like somebody reported this ([1]),
although their patch uses gettext() instead of ugettext() -- the latter
is a better habit to get into these days.

[1] http://code.google.com/p/django-rest-interface/issues/detail?id=31

Regards,
Malcolm


David Larlet

unread,
Jan 9, 2009, 7:18:12 AM1/9/09
to django-res...@googlegroups.com
Ben,

It seems that an import is missing:
from django.utils.translation import gettext as _ (or maybe _lazy)

That's an issue and should be reported as is.

David

Le 9 janv. 09 à 13:06, benm a écrit :

Alaa Salman

unread,
Jan 9, 2009, 10:19:17 AM1/9/09
to django-res...@googlegroups.com
On Fri, Jan 9, 2009 at 2:19 PM, Malcolm Tredinnick
<mal...@pointy-stick.com> wrote:
>
> The correct solution is to add
>
> from django.utils.translation import ugettext as _
>
> to the top of the file. Looks like somebody reported this ([1]),
> although their patch uses gettext() instead of ugettext() -- the latter
> is a better habit to get into these days.
>
> [1] http://code.google.com/p/django-rest-interface/issues/detail?id=31
>

The last time i tested this code was just after the 1.0 release and my
code is still running off that. I had some trouble reproducing some of
the bugs...i just make sure that the test cases run every once in a
while.
I'll try to take care of this bug this weekend.
Thanks everyone for the heads up.


--
Regards,
Alaa Salman
http://www.codedemigod.com
FSF Member #6304
"Never measure the height of a mountain until you have reached the
top. Then you will see how low it was." ---Dag Hammarskjold

Reply all
Reply to author
Forward
0 new messages