Created
July 20, 2020 17:17
-
-
Save bbelderbos/0bcb04e0b7a89a0cb108d331ea75f8e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TermsEnforcerMiddleware: | |
"""Django Middleware (add to MIDDLEWARE) to enforce users to sign the terms""" | |
def __init__(self, get_response): | |
self.get_response = get_response | |
def __call__(self, request): | |
response = self.get_response(request) | |
# only relevant for logged in users | |
if not request.user.is_authenticated: | |
return response | |
path = request.path | |
# pages to not redirect on (no recursion please!) | |
path = path.strip('/') | |
if path in 'logout terms privacy'.split(): | |
return response | |
# if no consent get one before giving access to the site | |
# assumes One-to-One User-Profile model | |
if not request.user.profile.accepts_terms: | |
return redirect('terms') | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment