Skip to content

Instantly share code, notes, and snippets.

@bbelderbos
Created July 20, 2020 17:17
Show Gist options
  • Save bbelderbos/0bcb04e0b7a89a0cb108d331ea75f8e1 to your computer and use it in GitHub Desktop.
Save bbelderbos/0bcb04e0b7a89a0cb108d331ea75f8e1 to your computer and use it in GitHub Desktop.
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