aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--config/middleware.py27
-rw-r--r--config/settings/gcloud_settings.py5
2 files changed, 32 insertions, 0 deletions
diff --git a/config/middleware.py b/config/middleware.py
new file mode 100644
index 0000000..c3b7c46
--- /dev/null
+++ b/config/middleware.py
@@ -0,0 +1,27 @@
+"""
+whipped from https://www.youtube.com/watch?v=DbAzWll4UIA&list=PLw02n0FEB3E3VSHjyYMcFadtQORvl1Ssj&index=27
+"""
+import re
+
+from django.conf import settings
+from django.shortcuts import redirect
+
+EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip("/"))]
+if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
+ EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]
+
+
+class LoginRequiredMiddleware:
+ def __init__(self, get_response):
+ self.get_response = get_response
+
+ def __call__(self, request, *args, **kwargs):
+ response = self.get_response(request)
+ return response
+
+ def process_view(self, request, view_func, view_args, view_kwargs):
+ assert hasattr(request, 'user')
+ path = request.path_info.lstrip("/")
+ if not request.user.is_authenticated:
+ if not any(url.match(path) for url in EXEMPT_URLS):
+ return redirect(settings.LOGIN_URL)
diff --git a/config/settings/gcloud_settings.py b/config/settings/gcloud_settings.py
index 3a8912f..a057db8 100644
--- a/config/settings/gcloud_settings.py
+++ b/config/settings/gcloud_settings.py
@@ -169,12 +169,17 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
+ 'config.middleware.LoginRequiredMiddleware'
]
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
+LOGIN_EXEMPT_URLS = (
+ r"account/login",
+ r"account/logout"
+)
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases