core: properly handle new redirections set in decorators.

- Allows easier full pyramid migration
This commit is contained in:
Marcin Kuzminski 2017-03-02 16:10:47 +01:00
parent bab90b0598
commit cfa10105b7
3 changed files with 22 additions and 11 deletions

View file

@ -32,7 +32,7 @@ from pyramid.config import Configurator
from pyramid.settings import asbool, aslist
from pyramid.wsgi import wsgiapp
from pyramid.httpexceptions import (
HTTPError, HTTPInternalServerError, HTTPFound)
HTTPException, HTTPError, HTTPInternalServerError, HTTPFound)
from pyramid.events import ApplicationCreated
from pyramid.renderers import render_to_response
from routes.middleware import RoutesMiddleware
@ -226,7 +226,7 @@ def error_handler(exception, request):
base_response = HTTPInternalServerError()
# prefer original exception for the response since it may have headers set
if isinstance(exception, HTTPError):
if isinstance(exception, HTTPException):
base_response = exception
def is_http_error(response):

View file

@ -1268,6 +1268,7 @@ class NotAnonymous(object):
return get_cython_compat_decorator(self.__wrapper, func)
def __wrapper(self, func, *fargs, **fkwargs):
import rhodecode.lib.helpers as h
cls = fargs[0]
self.user = cls._rhodecode_user
@ -1277,8 +1278,6 @@ class NotAnonymous(object):
if anonymous:
came_from = request.path_qs
import rhodecode.lib.helpers as h
h.flash(_('You need to be a registered user to '
'perform this action'),
category='warning')
@ -1315,6 +1314,7 @@ class HasAcceptedRepoType(object):
return get_cython_compat_decorator(self.__wrapper, func)
def __wrapper(self, func, *fargs, **fkwargs):
import rhodecode.lib.helpers as h
cls = fargs[0]
rhodecode_repo = cls.rhodecode_repo
@ -1325,7 +1325,6 @@ class HasAcceptedRepoType(object):
if rhodecode_repo.alias in self.repo_type_list:
return func(*fargs, **fkwargs)
else:
import rhodecode.lib.helpers as h
h.flash(h.literal(
_('Action not supported for %s.' % rhodecode_repo.alias)),
category='warning')
@ -1349,7 +1348,7 @@ class PermsDecorator(object):
from pyramid.threadlocal import get_current_request
pyramid_request = get_current_request()
if not pyramid_request:
# return global request of pylons incase pyramid one isn't available
# return global request of pylons in case pyramid isn't available
return request
return pyramid_request
@ -1360,6 +1359,7 @@ class PermsDecorator(object):
return _request.path_qs
def __wrapper(self, func, *fargs, **fkwargs):
import rhodecode.lib.helpers as h
cls = fargs[0]
_user = cls._rhodecode_user
@ -1375,10 +1375,9 @@ class PermsDecorator(object):
anonymous = _user.username == User.DEFAULT_USER
if anonymous:
import rhodecode.lib.helpers as h
came_from = self._get_came_from()
h.flash(_('You need to be signed in to view this page'),
category='warning')
category='warning')
raise HTTPFound(
h.route_path('login', _query={'came_from': came_from}))
@ -1970,3 +1969,5 @@ def get_cython_compat_decorator(wrapper, func):
return wrapper(func, *args, **kwds)
local_wrapper.__wrapped__ = func
return local_wrapper

View file

@ -21,7 +21,8 @@
import logging
from pyramid import httpexceptions
from pyramid.httpexceptions import HTTPError, HTTPInternalServerError
from pyramid.httpexceptions import (
HTTPRedirection, HTTPError, HTTPInternalServerError)
from pyramid.threadlocal import get_current_request
from rhodecode.lib.exceptions import VCSServerUnavailable
@ -53,7 +54,7 @@ class PylonsErrorHandlingMiddleware(object):
def is_http_error(self, response):
# webob type error responses
return (400 <= response.status_int <= 599)
return 400 <= response.status_int <= 599
def reraise(self):
return self._reraise
@ -73,9 +74,16 @@ class PylonsErrorHandlingMiddleware(object):
if self.is_http_error(response):
response = webob_to_pyramid_http_response(response)
return self.error_view(response, request)
except HTTPError as e: # pyramid type exceptions
except HTTPRedirection as e:
# pyramid type redirection, with status codes in the 300s
log.debug('Handling pyramid HTTPRedirection: %s', e)
return e
except HTTPError as e:
# pyramid type exceptions, with status codes in the 400s and 500s
log.debug('Handling pyramid HTTPError: %s', e)
return self.error_view(e, request)
except Exception as e:
log.debug('Handling general error: %s', e)
if self.reraise():
raise
@ -89,6 +97,8 @@ class PylonsErrorHandlingMiddleware(object):
def webob_to_pyramid_http_response(webob_response):
log.debug('response is webob http error[%s], handling now...',
webob_response.status_int)
ResponseClass = httpexceptions.status_map[webob_response.status_int]
pyramid_response = ResponseClass(webob_response.status)
pyramid_response.status = webob_response.status