blob: 66b657f723c9681bdbb27eee356fa78740abc7c5 [file] [log] [blame]
# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for the GCI age check."""
from django import forms
from codein.views.helper import urls
from melange.request import exception
from melange.request import links
from soc.logic import validate
from soc.views.helper import url_patterns
from soc.modules.gci.views import base as gci_base
from soc.modules.gci.views import forms as gci_forms
from soc.modules.gci.views.helper import url_patterns as gci_url_patterns
BIRTH_DATE_COOKIE_MAX_AGE_SECS = 3600
class AgeCheckForm(gci_forms.GCIModelForm):
"""Django form for the Age verification page."""
class Meta:
model = None
css_prefix = 'gci_age_check'
fields = ['birth_date']
birth_date = forms.DateField(
label='Date of birth (YYYY-MM-DD)', required=True)
class AgeCheck(gci_base.GCIRequestHandler):
"""View for the GCI age check."""
def djangoURLPatterns(self):
"""The URL pattern for the view."""
return [gci_url_patterns.url(r'age_check/%s$' % url_patterns.PROGRAM,
self, name='gci_age_check')]
def checkAccess(self, data, check, mutator):
"""Ensures that student sign up is active and the user is logged out."""
check.studentSignupActive()
check.isLoggedOut()
def templatePath(self):
"""Returns the path to the template."""
return 'modules/gci/age_check/base.html'
def context(self, data, check, mutator):
"""Handler for default HTTP GET request."""
context = {
'page_name': 'Age Verification for %s' % data.program.name,
'program': data.program,
'failed_check': False
}
age_check_result = data.request.COOKIES.get('age_check', None)
if age_check_result == '0':
context['failed_check'] = True
elif age_check_result:
# age check passed, redirect to register as a student page
raise exception.Redirect(
links.ABSOLUTE_LINKER.program(
data.program, urls.UrlNames.PROFILE_REGISTER_AS_STUDENT,
secure=True))
if data.POST:
context['form'] = AgeCheckForm(data=data.POST)
else:
context['form'] = AgeCheckForm()
return context
def post(self, data, check, mutator):
"""Handles POST requests."""
form = AgeCheckForm(data=data.POST)
if not form.is_valid():
# TODO(nathaniel): problematic self-call.
return self.get(data, check, mutator)
program = data.program
birth_date = form.cleaned_data['birth_date']
# redirect to the same page and have the cookies sent across
# TODO(nathaniel): make this .program() call unnecessary.
data.redirect.program()
response = data.redirect.to('gci_age_check')
age_sufficient = validate.isAgeSufficientForStudent(birth_date, program)
response.set_cookie(
'age_check', birth_date if age_sufficient else '0',
max_age=BIRTH_DATE_COOKIE_MAX_AGE_SECS)
return response