Created
December 21, 2011 09:36
-
-
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
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
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