2016-03-01 11:23:03 -05:00
|
|
|
from django.conf import settings
|
2017-11-03 13:24:31 -04:00
|
|
|
from django.db import ProgrammingError
|
2017-12-13 11:49:36 -05:00
|
|
|
from django.http import Http404, HttpResponseRedirect
|
2017-04-05 14:40:25 -04:00
|
|
|
from django.urls import reverse
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-07-23 23:12:41 -04:00
|
|
|
from .views import server_error
|
|
|
|
|
2016-12-07 15:14:22 -05:00
|
|
|
BASE_PATH = getattr(settings, 'BASE_PATH', False)
|
2016-03-01 11:23:03 -05:00
|
|
|
LOGIN_REQUIRED = getattr(settings, 'LOGIN_REQUIRED', False)
|
|
|
|
|
|
|
|
|
2016-12-26 10:48:15 -05:00
|
|
|
class LoginRequiredMiddleware(object):
|
2016-03-01 11:23:03 -05:00
|
|
|
"""
|
|
|
|
If LOGIN_REQUIRED is True, redirect all non-authenticated users to the login page.
|
|
|
|
"""
|
2016-12-26 10:48:15 -05:00
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
2018-03-30 10:39:22 -04:00
|
|
|
if LOGIN_REQUIRED and not request.user.is_authenticated:
|
2016-12-07 15:14:22 -05:00
|
|
|
# Redirect unauthenticated requests to the login page. API requests are exempt from redirection as the API
|
2019-04-30 16:09:10 +02:00
|
|
|
# performs its own authentication. Also metrics can be read without login.
|
2017-03-21 13:23:56 -04:00
|
|
|
api_path = reverse('api-root')
|
2019-04-30 16:54:23 +02:00
|
|
|
if not request.path_info.startswith(api_path, '/metrics') and request.path_info != settings.LOGIN_URL:
|
2016-10-13 16:12:27 -04:00
|
|
|
return HttpResponseRedirect('{}?next={}'.format(settings.LOGIN_URL, request.path_info))
|
2016-12-26 10:48:15 -05:00
|
|
|
return self.get_response(request)
|
2017-03-21 13:23:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
class APIVersionMiddleware(object):
|
|
|
|
"""
|
|
|
|
If the request is for an API endpoint, include the API version as a response header.
|
|
|
|
"""
|
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
api_path = reverse('api-root')
|
|
|
|
response = self.get_response(request)
|
|
|
|
if request.path_info.startswith(api_path):
|
|
|
|
response['API-Version'] = settings.REST_FRAMEWORK_VERSION
|
|
|
|
return response
|
2017-11-03 13:24:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ExceptionHandlingMiddleware(object):
|
|
|
|
"""
|
|
|
|
Intercept certain exceptions which are likely indicative of installation issues and provide helpful instructions
|
|
|
|
to the user.
|
|
|
|
"""
|
|
|
|
def __init__(self, get_response):
|
|
|
|
self.get_response = get_response
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
return self.get_response(request)
|
|
|
|
|
|
|
|
def process_exception(self, request, exception):
|
|
|
|
|
2017-11-06 17:48:13 -05:00
|
|
|
# Don't catch exceptions when in debug mode
|
2017-11-03 13:24:31 -04:00
|
|
|
if settings.DEBUG:
|
2017-11-06 17:48:13 -05:00
|
|
|
return
|
2017-11-03 13:24:31 -04:00
|
|
|
|
2017-12-13 11:49:36 -05:00
|
|
|
# Ignore Http404s (defer to Django's built-in 404 handling)
|
|
|
|
if isinstance(exception, Http404):
|
|
|
|
return
|
|
|
|
|
2018-07-23 23:00:09 -04:00
|
|
|
# Determine the type of exception. If it's a common issue, return a custom error page with instructions.
|
|
|
|
custom_template = None
|
2017-11-03 13:24:31 -04:00
|
|
|
if isinstance(exception, ProgrammingError):
|
2018-07-23 23:00:09 -04:00
|
|
|
custom_template = 'exceptions/programming_error.html'
|
2017-11-03 13:24:31 -04:00
|
|
|
elif isinstance(exception, ImportError):
|
2018-07-23 23:00:09 -04:00
|
|
|
custom_template = 'exceptions/import_error.html'
|
2018-08-14 11:47:54 -04:00
|
|
|
elif isinstance(exception, PermissionError):
|
2018-07-23 23:00:09 -04:00
|
|
|
custom_template = 'exceptions/permission_error.html'
|
|
|
|
|
2018-07-23 23:12:41 -04:00
|
|
|
# Return a custom error message, or fall back to Django's default 500 error handling
|
2018-07-23 23:00:09 -04:00
|
|
|
if custom_template:
|
2018-07-23 23:12:41 -04:00
|
|
|
return server_error(request, template_name=custom_template)
|