def _clean_date(self, date): if not date: return "" if len(date) > 20: raise ValidationError("The date entered was too long") try: parsed_date = parse_approximate_date(date) except ValueError: raise ValidationError( "That date of birth could not be understood. Try using DD/MM/YYYY instead" ) return parsed_date
def clean_birth_date(self): birth_date = self.cleaned_data["birth_date"] if not birth_date: return "" try: parsed_date = parse_approximate_date(birth_date) except ValueError: if settings.DD_MM_DATE_FORMAT_PREFERRED: message = _( "That date of birth could not be understood. Try using DD/MM/YYYY instead" ) else: message = _( "That date of birth could not be understood. Try using MM/DD/YYYY instead" ) raise ValidationError(message) return parsed_date
def dod_as_approximate_date(self): from people.helpers import parse_approximate_date return parse_approximate_date(self.death_date)
def test_nonsense_string(self): with self.assertRaises(ValueError): parse_approximate_date("this is not a date")
def test_expanded_natural_date_string(self): parsed = parse_approximate_date("31st of December 1999") self.assertEqual(type(parsed), ApproximateDate) self.assertEqual(repr(parsed), "1999-12-31")
def test_empty_string(self): with self.assertRaises(ValueError): parse_approximate_date("")
def test_dd_mm_yyyy_with_dashes(self): parsed = parse_approximate_date("1-4-1977") self.assertEqual(type(parsed), ApproximateDate) self.assertEqual(repr(parsed), "1977-04-01")
def test_nonsense(self): with self.assertRaises(ValueError): parse_approximate_date("12345678")
def test_iso_8601(self): parsed = parse_approximate_date("1977-04-01") self.assertEqual(type(parsed), ApproximateDate) self.assertEqual(repr(parsed), "1977-04-01")
def test_only_year(self): parsed = parse_approximate_date("1977") self.assertEqual(type(parsed), ApproximateDate) self.assertEqual(repr(parsed), "1977-00-00")