Thanks Pankaj,  In this case I could not use an HttpResponse because the
helper method I am developing doesn't return a respose it returns a dict or
raises an exception if the user is not authenticated.
For example:

def get_server_time(request):
      time_dictionary = helper_method()
      #Rest of the view here

def helper_method():
     if user.is_authenticated():
        return {'server_time': server_time()}
     else:
        raise HttpCustomException(redirect='this_login_for_this_method')

The goal is to return the dictionary or redirect the user to a specific
login screen (there would be several login screens).
The idea is to encapsulate all this functionality in the helper method so
the views dont have to implement the authentication part or write a
try/catch/redirect in all the views.

> get_response() method of django.core.handlers.base.BaseHandler handles
> Http404 and sets status code here -
>
https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/base.py#L138-L155

This is what I wanted to know, but it seem i couldnt be able to reproduce
because it checks specifically for an exception of the kind Http404. I
think I will have to attach to a custom middleware and catch/redirect the
exceptions/views from there

btw this is for a Oauth client library.

Thanks

On Mon, Feb 18, 2013 at 11:52 AM, Pankaj Singh <ps.j...@gmail.com> wrote:
> Hey Serge,
>
> Here is a brief description of how status code is set in request response
> cycle of Django.
>
> Http404 inherits from Exception defined here -
>
https://github.com/django/django/blob/1.4.3/django/http/__init__.py#L132-L133
>
> get_response() method of django.core.handlers.base.BaseHandler handles
> Http404 and sets status code here -
>
https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/base.py#L138-L155
>
> django.core.handlers.base.BaseHandler has some code for setting
status_code
> to 403 and 500 as well.
>
>> I want to make the same but with for a 401 code and a custom redirection
>> according to the resource to be accessed.
>
>
> There is really simple way to do this.
>
>
> class HttpResponseUnauthorizedRequest(HttpResponse):
>
>     status_code = 401
>
>
> You can use this class similar to HttpResponse
>
>
> def protected_view(request):
>     ## handle authorization
>     ## if not authorized then return above response
>     return HttpResponseUnauthorizedRequest("Authorazation is required")
>
>
> There are similar classes for different status code as well -
>
https://github.com/django/django/blob/stable/1.4.x/django/http/__init__.py#L751-L770
>
> Similar implementation by django-tastypie -
>
https://github.com/toastdriven/django-tastypie/blob/master/tastypie/http.py
>
> So you have set status code, now what
>
> process_response() method of django.middleware.common.CommonMiddleware
> checks if status code is 404 and sends an email to admins for broken urls
-
>
https://github.com/django/django/blob/stable/1.4.x/django/middleware/common.py#L94-L109
>
> You can see status_code based processing of response in other middlewares
as
> well, e.g. cache, http and csrf.
>
> Finally request handlers set status code is response header -
>
> 1. django.core.handlers.wsgi.WSGIHandler sets status code in response
header
> -
>
https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/wsgi.py#L245-L253
>
> 2. If you are using modpython then status_code in header is set by
> django.core.handlers.modpython.ModPythonHandler -
>
https://github.com/django/django/blob/stable/1.4.x/django/core/handlers/wsgi.py#L245-L253
>
> This response is served by Webserver.
>
> Sincerely,
> Pankaj Singh
> http://about.me/psjinx
>
>
> On Mon, Feb 18, 2013 at 6:15 PM, Serge G. Spaolonzi <se...@cobalys.com>
> wrote:
>>
>> Hi,
>>
>> How does the Http404 exception works internally?
>> I want to make the same but with for a 401 code and a custom
>> redirection according to the resource to be accessed.
>> I have thought about using a middleware to archive this but I am not
>> sure if it is standard way used by 'django.http.Http404'.
>>
>> Thanks
>>
>> --
>> Serge G. Spaolonzi
>> Cobalys Systems
>> http://www.cobalys.com
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



-- 
Serge G. Spaolonzi
Cobalys Systems
http://www.cobalys.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to