Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.