Ticket #7553: 7553-sites-decorators-excpt.diff

File 7553-sites-decorators-excpt.diff, 3.7 KB (added by Michael Newman, 16 years ago)

A proof-of-concept patch using an exception to catch data that may fall through.

  • django/views/decorators/cache.py

     
    4242    """
    4343    def _wrapped_view_func(request, *args, **kwargs):
    4444        response = view_func(request, *args, **kwargs)
    45         # Although rare, it is possible for a view to return None (e.g. the
    46         # django.contrib.admin.sites.AdminSite.login view in one corner-case)
    47         if response:
    48             add_never_cache_headers(response)
     45        add_never_cache_headers(response)
    4946        return response
    5047    return wraps(view_func)(_wrapped_view_func)
  • django/contrib/admin/sites.py

     
    2424class NotRegistered(Exception):
    2525    pass
    2626
     27class PostDataFallThrough(Exception):
     28    pass
     29
    2730def _encode_post_data(post_data):
    2831    from django.conf import settings
    2932    pickled = pickle.dumps(post_data)
     
    7174            model_or_iterable = [model_or_iterable]
    7275        for model in model_or_iterable:
    7376            if model in self._registry:
    74                 raise AlreadyRegistered('The model %s is already registered' % model.__name__)
     77                raise AlreadyRegistered, _('The model %s is already registered') % model.__name__
    7578            self._registry[model] = admin_class(model, self)
    7679
    7780    def unregister(self, model_or_iterable):
     
    8487            model_or_iterable = [model_or_iterable]
    8588        for model in model_or_iterable:
    8689            if model not in self._registry:
    87                 raise NotRegistered('The model %s is not registered' % model.__name__)
     90                raise NotRegistered, _('The model %s is not registered') % model.__name__
    8891            del self._registry[model]
    8992
    9093    def has_permission(self, request):
     
    111114        # The 'logout' view doesn't require that the person is logged in.
    112115        if url == 'logout':
    113116            return self.logout(request)
    114 
     117       
     118        # Check for permission or show login form
    115119        if not self.has_permission(request):
    116             response = self.login(request)
    117             if response:
    118                 # make sure that there is a response before returning
    119                 # this addresses any post data that might persist from
    120                 # expired sessions and continue through (#5999)
     120            try:
     121                response = self.login(request)
    121122                return response
     123            except PostDataFallThrough:
     124                # exception for a form that carries post data
     125                pass
    122126
    123127        if url == '':
    124128            return self.index(request)
     
    250254                user.last_login = datetime.datetime.now()
    251255                user.save()
    252256                if request.POST.has_key('post_data'):
     257                    request.session.delete_test_cookie()
    253258                    post_data = _decode_post_data(request.POST['post_data'])
    254259                    if post_data and not post_data.has_key(LOGIN_FORM_KEY):
    255260                        # overwrite request.POST with the saved post_data, and continue
    256261                        request.POST = post_data
    257262                        request.user = user
    258                         return None
     263                        raise PostDataFallThrough
    259264                    else:
    260                         request.session.delete_test_cookie()
    261265                        return http.HttpResponseRedirect(request.path)
    262266            else:
    263267                return self.display_login_form(request, ERROR_MESSAGE)
Back to Top