Skip to content

Instantly share code, notes, and snippets.

@vladimiroff
Created December 21, 2011 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimiroff/1505394 to your computer and use it in GitHub Desktop.
Save vladimiroff/1505394 to your computer and use it in GitHub Desktop.
Django Middleware: Each user has to confirm that he is at least 18 years old
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from django.conf import settings
class AdultsOnlyMiddleware(object):
"""
Each user has to confirm that he is at least 18 years old
Note: No, I did not make this for some porn site.
"""
url = reverse('confirm_your_age')
adult_key = 'has_18'
def process_request(self, request):
if not request.session.get(self.adult_key, False):
if request.POST.get(self.adult_key, False):
request.session[self.adult_key] = True
return redirect(request.GET.get('next'))
if request.path_info != self.url and settings.MEDIA_URL not in request.path_info:
return redirect( "%s?next=%s" % (self.url, request.path_info))
elif request.path_info == self.url:
return redirect('/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment