mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Refactored common code in handlers/base.py and urlresolvers.py
This commit is contained in:
parent
bcd2e6c697
commit
7f76251fb7
@ -70,6 +70,16 @@ class BaseHandler(object):
|
||||
view = transaction.atomic(using=db.alias)(view)
|
||||
return view
|
||||
|
||||
def get_exception_response(self, request, resolver, status_code):
|
||||
try:
|
||||
callback, param_dict = resolver.resolve_error_handler(status_code)
|
||||
response = callback(request, **param_dict)
|
||||
except:
|
||||
signals.got_request_exception.send(sender=self.__class__, request=request)
|
||||
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
|
||||
|
||||
return response
|
||||
|
||||
def get_response(self, request):
|
||||
"Returns an HttpResponse object for the given HttpRequest"
|
||||
|
||||
@ -151,12 +161,7 @@ class BaseHandler(object):
|
||||
if settings.DEBUG:
|
||||
response = debug.technical_404_response(request, e)
|
||||
else:
|
||||
try:
|
||||
callback, param_dict = resolver.resolve404()
|
||||
response = callback(request, **param_dict)
|
||||
except:
|
||||
signals.got_request_exception.send(sender=self.__class__, request=request)
|
||||
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
|
||||
response = self.get_exception_response(request, resolver, 404)
|
||||
|
||||
except PermissionDenied:
|
||||
logger.warning(
|
||||
@ -165,14 +170,7 @@ class BaseHandler(object):
|
||||
'status_code': 403,
|
||||
'request': request
|
||||
})
|
||||
try:
|
||||
callback, param_dict = resolver.resolve403()
|
||||
response = callback(request, **param_dict)
|
||||
except:
|
||||
signals.got_request_exception.send(
|
||||
sender=self.__class__, request=request)
|
||||
response = self.handle_uncaught_exception(request,
|
||||
resolver, sys.exc_info())
|
||||
response = self.get_exception_response(request, resolver, 403)
|
||||
|
||||
except SuspiciousOperation as e:
|
||||
# The request logger receives events for any problematic request
|
||||
@ -186,14 +184,7 @@ class BaseHandler(object):
|
||||
'request': request
|
||||
})
|
||||
|
||||
try:
|
||||
callback, param_dict = resolver.resolve400()
|
||||
response = callback(request, **param_dict)
|
||||
except:
|
||||
signals.got_request_exception.send(
|
||||
sender=self.__class__, request=request)
|
||||
response = self.handle_uncaught_exception(request,
|
||||
resolver, sys.exc_info())
|
||||
response = self.get_exception_response(request, resolver, 400)
|
||||
|
||||
except SystemExit:
|
||||
# Allow sys.exit() to actually exit. See tickets #1023 and #4701
|
||||
@ -251,7 +242,7 @@ class BaseHandler(object):
|
||||
if resolver.urlconf_module is None:
|
||||
six.reraise(*exc_info)
|
||||
# Return an HttpResponse that displays a friendly error message.
|
||||
callback, param_dict = resolver.resolve500()
|
||||
callback, param_dict = resolver.resolve_error_handler(500)
|
||||
return callback(request, **param_dict)
|
||||
|
||||
def apply_response_fixes(self, request, response):
|
||||
|
@ -393,7 +393,7 @@ class RegexURLResolver(LocaleRegexProvider):
|
||||
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
|
||||
return patterns
|
||||
|
||||
def _resolve_special(self, view_type):
|
||||
def resolve_error_handler(self, view_type):
|
||||
callback = getattr(self.urlconf_module, 'handler%s' % view_type, None)
|
||||
if not callback:
|
||||
# No handler specified in file; use default
|
||||
@ -402,18 +402,6 @@ class RegexURLResolver(LocaleRegexProvider):
|
||||
callback = getattr(urls, 'handler%s' % view_type)
|
||||
return get_callable(callback), {}
|
||||
|
||||
def resolve400(self):
|
||||
return self._resolve_special('400')
|
||||
|
||||
def resolve403(self):
|
||||
return self._resolve_special('403')
|
||||
|
||||
def resolve404(self):
|
||||
return self._resolve_special('404')
|
||||
|
||||
def resolve500(self):
|
||||
return self._resolve_special('500')
|
||||
|
||||
def reverse(self, lookup_view, *args, **kwargs):
|
||||
return self._reverse_with_prefix(lookup_view, '', *args, **kwargs)
|
||||
|
||||
|
@ -614,15 +614,15 @@ class ErrorHandlerResolutionTests(TestCase):
|
||||
|
||||
def test_named_handlers(self):
|
||||
handler = (empty_view, {})
|
||||
self.assertEqual(self.resolver.resolve400(), handler)
|
||||
self.assertEqual(self.resolver.resolve404(), handler)
|
||||
self.assertEqual(self.resolver.resolve500(), handler)
|
||||
self.assertEqual(self.resolver.resolve_error_handler(400), handler)
|
||||
self.assertEqual(self.resolver.resolve_error_handler(404), handler)
|
||||
self.assertEqual(self.resolver.resolve_error_handler(500), handler)
|
||||
|
||||
def test_callable_handers(self):
|
||||
handler = (empty_view, {})
|
||||
self.assertEqual(self.callable_resolver.resolve400(), handler)
|
||||
self.assertEqual(self.callable_resolver.resolve404(), handler)
|
||||
self.assertEqual(self.callable_resolver.resolve500(), handler)
|
||||
self.assertEqual(self.callable_resolver.resolve_error_handler(400), handler)
|
||||
self.assertEqual(self.callable_resolver.resolve_error_handler(404), handler)
|
||||
self.assertEqual(self.callable_resolver.resolve_error_handler(500), handler)
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls_without_full_import')
|
||||
|
Loading…
Reference in New Issue
Block a user