2017-05-24 11:33:11 -04:00
|
|
|
from __future__ import unicode_literals
|
2017-11-03 13:24:31 -04:00
|
|
|
import sys
|
2017-05-24 11:33:11 -04:00
|
|
|
|
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
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.shortcuts import render
|
2017-04-05 14:40:25 -04:00
|
|
|
from django.urls import reverse
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
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):
|
2016-03-01 11:23:03 -05: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
|
|
|
|
# performs its own authentication.
|
2017-03-21 13:23:56 -04:00
|
|
|
api_path = reverse('api-root')
|
2016-12-07 15:14:22 -05:00
|
|
|
if not request.path_info.startswith(api_path) 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):
|
|
|
|
|
|
|
|
# Raise exceptions if in debug mode
|
|
|
|
if settings.DEBUG:
|
|
|
|
raise exception
|
|
|
|
|
|
|
|
# Determine the type of exception
|
|
|
|
if isinstance(exception, ProgrammingError):
|
|
|
|
template_name = 'exceptions/programming_error.html'
|
|
|
|
elif isinstance(exception, ImportError):
|
|
|
|
template_name = 'exceptions/import_error.html'
|
2017-11-06 17:24:09 -05:00
|
|
|
elif (
|
|
|
|
sys.version_info[0] >= 3 and isinstance(exception, PermissionError)
|
|
|
|
) or (
|
|
|
|
isinstance(exception, OSError) and exception.errno == 13
|
|
|
|
):
|
2017-11-03 13:24:31 -04:00
|
|
|
template_name = 'exceptions/permission_error.html'
|
|
|
|
else:
|
|
|
|
template_name = '500.html'
|
|
|
|
|
|
|
|
# Return an error message
|
|
|
|
type_, error, traceback = sys.exc_info()
|
|
|
|
return render(request, template_name, {
|
|
|
|
'exception': str(type_),
|
|
|
|
'error': error,
|
|
|
|
}, status=500)
|