Skip to content

Commit bbe7df0

Browse files
committed
Add the parse function
1 parent 0725027 commit bbe7df0

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='django-variable-resolution-date',
6-
version='0.1.6',
6+
version='0.1.7',
77
description='A django field that can represent either a year, or a year and a month, or a full calendar date',
88
long_description='',
99
author='Nicholas Wolff',

tests/test_field.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def test_it_can_create_vrd_with_full_date(self):
1818
def test_it_cannot_create_vrd_with_invalid_full_date(self):
1919
with raises(ValidationError) as ex:
2020
ClubMember(name='dave', member_since='1997-02-29').full_clean()
21-
assert ex.value.messages == ['Enter a year, year-month, or year-month-day.']
21+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
2222

2323
def test_it_cannot_create_vrd_with_extra_characters(self):
2424
with raises(ValidationError) as ex:
2525
ClubMember(name='earl', member_since='20001').full_clean()
26-
assert ex.value.messages == ['Enter a year, year-month, or year-month-day.']
26+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
2727

2828
def test_it_cannot_create_vrd_with_month_day(self):
2929
with raises(ValidationError) as ex:
3030
ClubMember(name='fred', member_since='02-03').full_clean()
31-
assert ex.value.messages == ['Enter a year, year-month, or year-month-day.']
31+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']

tests/test_parsing.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from django.core.exceptions import ValidationError
2+
from django.test import TestCase
3+
from pytest import raises
4+
5+
from variable_resolution_date import parse
6+
7+
8+
class VariableResolutionDateFieldTest(TestCase):
9+
def test_it_can_parse_vrd_with_year(self):
10+
y, m, d = parse('1996')
11+
assert y == 1996
12+
assert m is None
13+
assert d is None
14+
15+
def test_it_can_parse_vrd_with_year_and_month(self):
16+
y, m, d = parse('1996-02')
17+
assert y == 1996
18+
assert m == 2
19+
assert d is None
20+
21+
def test_it_can_parse_vrd_with_full_date(self):
22+
y, m, d = parse('1996-02-29')
23+
assert y == 1996
24+
assert m == 2
25+
assert d is 29
26+
27+
def test_it_cannot_parse_vrd_with_invalid_full_date(self):
28+
with raises(ValidationError) as ex:
29+
parse('1997-02-29')
30+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
31+
32+
def test_it_cannot_parse_vrd_with_extra_characters(self):
33+
with raises(ValidationError) as ex:
34+
parse('20001')
35+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
36+
37+
def test_it_cannot_parse_vrd_with_month_day(self):
38+
with raises(ValidationError) as ex:
39+
parse('02-03')
40+
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']

variable_resolution_date/__init__.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,28 @@
2323
VARIABLE_RESOLUTION_DATE_LENGTH = 10
2424

2525

26-
def validate_variable_resolution_date(value):
26+
def parse(value):
27+
""" returns a triple: year, month, day (month and day may be None) """
2728
match = VARIABLE_RESOLUTION_DATE_RE.match(force_text(value))
2829
if match:
2930
year = int(match.group(1))
30-
month = int(match.group(2) or 1)
31-
day = int(match.group(3) or 1)
31+
month = int(match.group(2)) if match.group(2) else None
32+
day = int(match.group(3)) if match.group(3) else None
3233
try:
33-
datetime.date(year, month, day)
34-
return
35-
except Exception:
34+
# Make sure the different parts taken together represent a valid date.
35+
datetime.date(year, month or 1, day or 1)
36+
return year, month, day
37+
except ValueError:
3638
pass
37-
raise ValidationError('Enter a year, year-month, or year-month-day.')
39+
raise ValidationError('Must be a valid year, year-month, or year-month-day.')
40+
41+
42+
def validate(value):
43+
parse(value)
3844

3945

4046
class VariableResolutionDateField(models.CharField):
41-
default_validators = [validate_variable_resolution_date]
47+
default_validators = [validate]
4248
description = 'A year, year-month, or year-month-day date'
4349

4450
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)