def wrapper(form): """Decorator wrapped method. """ birth_date = form.cleaned_data.get(field_name) if form.program and not validate.isAgeSufficientForProgram( birth_date, form.program): raise forms.ValidationError( 'Your age does not allow you to participate in the program.') return birth_date
def post(self): """Handles POST requests. """ form = AgeCheckForm(self.data.POST) if not form.is_valid(): return self.get() program = self.data.program birth_date = form.cleaned_data['birth_date'] age_sufficient = validate.isAgeSufficientForProgram(birth_date, program) if age_sufficient: self.response.set_cookie('age_check', birth_date) else: self.response.set_cookie('age_check', '0') # redirect to the same page and have the cookies sent across self.redirect.program().to('gci_age_check')
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.isAgeSufficientForProgram(birth_date, program) response.set_cookie('age_check', birth_date if age_sufficient else '0') return response
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.isAgeSufficientForProgram( birth_date, program) response.set_cookie('age_check', birth_date if age_sufficient else '0') return response
def testIsAgeSufficientForProgram(self): """Tests isAgeSufficientForProgram function.""" test_program_start = datetime.date(2012, 11, 26) test_min_age = 13 test_max_age = 20 properties = { 'student_min_age_as_of': test_program_start, 'student_min_age': test_min_age, 'student_max_age': test_max_age, } test_program = seeder_logic.seed(program_model.Program, properties=properties) # Someone hasn't yet been born. test_birth_date = test_program_start.replace( year=test_program_start.year + 1) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's really, really young. test_birth_date = test_program_start.replace( year=test_program_start.year - 1) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's just one day too young. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) test_birth_date = test_birth_date + datetime.timedelta(1) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's just old enough today. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) self.assertTrue( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's old enough by a full day. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) test_birth_date = test_birth_date - datetime.timedelta(1) self.assertTrue( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's right in the sweet spot. test_birth_date = test_program_start.replace( year=test_program_start.year - (test_min_age + test_max_age - 1) / 2) test_birth_date = test_birth_date + datetime.timedelta(173) self.assertTrue( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's young enough by six months. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date + datetime.timedelta(180) self.assertTrue( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's just young enough. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date + datetime.timedelta(1) self.assertTrue( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's having a birthday! Sadly they are too old. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's too old by a full day. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date - datetime.timedelta(1) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's too old by six months. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date - datetime.timedelta(180) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program)) # Someone's way too old. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age * 12) self.assertFalse( validate.isAgeSufficientForProgram(test_birth_date, test_program))
def testIsAgeSufficientForProgram(self): """Tests isAgeSufficientForProgram function.""" test_program_start = datetime.date(2012, 11, 26) test_min_age = 13 test_max_age = 20 properties = { 'student_min_age_as_of': test_program_start, 'student_min_age': test_min_age, 'student_max_age': test_max_age, } test_program = seeder_logic.seed( program_model.Program, properties=properties) # Someone hasn't yet been born. test_birth_date = test_program_start.replace( year=test_program_start.year + 1) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's really, really young. test_birth_date = test_program_start.replace( year=test_program_start.year - 1) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's just one day too young. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) test_birth_date = test_birth_date + datetime.timedelta(1) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's just old enough today. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) self.assertTrue(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's old enough by a full day. test_birth_date = test_program_start.replace( year=test_program_start.year - test_min_age) test_birth_date = test_birth_date - datetime.timedelta(1) self.assertTrue(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's right in the sweet spot. test_birth_date = test_program_start.replace( year=test_program_start.year - (test_min_age + test_max_age - 1) / 2) test_birth_date = test_birth_date + datetime.timedelta(173) self.assertTrue(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's young enough by six months. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date + datetime.timedelta(180) self.assertTrue(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's just young enough. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date + datetime.timedelta(1) self.assertTrue(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's having a birthday! Sadly they are too old. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's too old by a full day. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date - datetime.timedelta(1) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's too old by six months. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age - 1) test_birth_date = test_birth_date - datetime.timedelta(180) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program)) # Someone's way too old. test_birth_date = test_program_start.replace( year=test_program_start.year - test_max_age * 12) self.assertFalse(validate.isAgeSufficientForProgram( test_birth_date, test_program))