Ejemplo n.º 1
0
    def clean(self):
        cleaned_data = super(AgeGatewayForm, self).clean()

        country = cleaned_data.get('country')
        date_of_birth = cleaned_data.get('date_of_birth')
        if country and date_of_birth:
            if get_age(date_of_birth) < country.minimum_age:
                msg = "You must be at least %s years of age to use this site." \
                    % country.minimum_age
                raise forms.ValidationError(_(msg))

        return cleaned_data
Ejemplo n.º 2
0
    def process_response(self, request, response):

        # Ignore ajax
        if request.is_ajax():
            return response

        # Protected URLs
        global PROTECTED_URLS_PATTERN
        if not PROTECTED_URLS_PATTERN:
            PROTECTED_URLS_PATTERN = r'|'.join((
                reverse('age-gateway'),
                reverse('join'),
                reverse('login'),
                reverse('logout'),
                reverse('password_reset'),
                reverse('terms-and-conditions'),
                '/auth/password_reset/',
                '/static/',
                '/admin/',
            ))
        if re.match(PROTECTED_URLS_PATTERN, request.META['PATH_INFO']) is not None:
            return response

        # Listing feeds also exempted
        # todo: make the test more refined.
        if request.META['PATH_INFO'].endswith('/feed/'):
            return response

        # Now only do we possibly hit the database
        private_site = get_preference('GeneralPreferences', 'private_site')
        show_age_gateway = get_preference('GeneralPreferences', 'show_age_gateway')

        # Check trivial case
        if not (private_site or show_age_gateway):
            return response

        # Private site not enabled and gateway passed
        if not private_site and request.COOKIES.get('age_gateway_passed'):
            return response

        # Exempted URLs
        exempted_urls = get_preference('GeneralPreferences', 'exempted_urls')
        if exempted_urls \
            and (
                re.match(
                    r'|'.join(exempted_urls.split()),
                    request.META['PATH_INFO']
               ) is not None
            ):
            return response

        # Exempted IP addresses
        exempted_ips = get_preference('GeneralPreferences', 'exempted_ips')
        if exempted_ips \
            and (
                re.match(
                    r'|'.join(exempted_ips.split()),
                    request.META['REMOTE_ADDR']
               ) is not None
            ):
            return response

        # Exempted user agents
        exempted_user_agents = get_preference('GeneralPreferences', 'exempted_user_agents')
        if exempted_user_agents \
            and (
                re.match(
                    r'|'.join(exempted_user_agents.split()),
                    request.META.get('HTTP_USER_AGENT', '')
               ) is not None
            ):
            return response

        user = getattr(request, 'user', None)
        if (user is not None) and user.is_anonymous():
            if private_site:
                return redirect_to_login(request.path_info,
                                         login_url=reverse('login'))
            else:
                # check if a partner site has supplied this
                # site with the user's age
                ag_values, expires = self.get_partner_age_gateway_values(request)
                if ag_values and expires:
                    # verify age and automatically pass age gateway
                    dob = datetime.strptime(ag_values[3:], '%d-%m-%Y').date()
                    if Country.objects.filter(country_code__iexact=ag_values[:2],
                                              minimum_age__lte=get_age(dob)).exists():
                        response.set_cookie('age_gateway_passed', value=1,
                                            expires=expires)
                        response.set_cookie('age_gateway_values', value=ag_values,
                                            expires=expires)
                        return response
                return redirect_to_login(request.path_info,
                                         login_url=reverse('age-gateway'))

        return response