middleware.py 544 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
from django.conf import settings
from django.utils.http import is_safe_url
from django.shortcuts import redirect


class AllowOnlySiteUrl:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
Jean-Baptiste Pasquier's avatar
Jean-Baptiste Pasquier committed
11
        response = self.get_response(request)
12
        if(is_safe_url(request.get_raw_uri(), allowed_hosts=settings.SITE_URL) or response.status_code != 200):
13 14
            return response
        else:
15
            return redirect('{}{}'.format(settings.SITE_URL, request.path), permanent=True)