Skip to content

Commit

Permalink
Add the parse function
Browse files Browse the repository at this point in the history
  • Loading branch information
nwolff committed Jan 3, 2018
1 parent 0725027 commit bbe7df0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='django-variable-resolution-date',
version='0.1.6',
version='0.1.7',
description='A django field that can represent either a year, or a year and a month, or a full calendar date',
long_description='',
author='Nicholas Wolff',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def test_it_can_create_vrd_with_full_date(self):
def test_it_cannot_create_vrd_with_invalid_full_date(self):
with raises(ValidationError) as ex:
ClubMember(name='dave', member_since='1997-02-29').full_clean()
assert ex.value.messages == ['Enter a year, year-month, or year-month-day.']
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']

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

def test_it_cannot_create_vrd_with_month_day(self):
with raises(ValidationError) as ex:
ClubMember(name='fred', member_since='02-03').full_clean()
assert ex.value.messages == ['Enter a year, year-month, or year-month-day.']
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
40 changes: 40 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from pytest import raises

from variable_resolution_date import parse


class VariableResolutionDateFieldTest(TestCase):
def test_it_can_parse_vrd_with_year(self):
y, m, d = parse('1996')
assert y == 1996
assert m is None
assert d is None

def test_it_can_parse_vrd_with_year_and_month(self):
y, m, d = parse('1996-02')
assert y == 1996
assert m == 2
assert d is None

def test_it_can_parse_vrd_with_full_date(self):
y, m, d = parse('1996-02-29')
assert y == 1996
assert m == 2
assert d is 29

def test_it_cannot_parse_vrd_with_invalid_full_date(self):
with raises(ValidationError) as ex:
parse('1997-02-29')
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']

def test_it_cannot_parse_vrd_with_extra_characters(self):
with raises(ValidationError) as ex:
parse('20001')
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']

def test_it_cannot_parse_vrd_with_month_day(self):
with raises(ValidationError) as ex:
parse('02-03')
assert ex.value.messages == ['Must be a valid year, year-month, or year-month-day.']
22 changes: 14 additions & 8 deletions variable_resolution_date/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@
VARIABLE_RESOLUTION_DATE_LENGTH = 10


def validate_variable_resolution_date(value):
def parse(value):
""" returns a triple: year, month, day (month and day may be None) """
match = VARIABLE_RESOLUTION_DATE_RE.match(force_text(value))
if match:
year = int(match.group(1))
month = int(match.group(2) or 1)
day = int(match.group(3) or 1)
month = int(match.group(2)) if match.group(2) else None
day = int(match.group(3)) if match.group(3) else None
try:
datetime.date(year, month, day)
return
except Exception:
# Make sure the different parts taken together represent a valid date.
datetime.date(year, month or 1, day or 1)
return year, month, day
except ValueError:
pass
raise ValidationError('Enter a year, year-month, or year-month-day.')
raise ValidationError('Must be a valid year, year-month, or year-month-day.')


def validate(value):
parse(value)


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

def __init__(self, *args, **kwargs):
Expand Down

0 comments on commit bbe7df0

Please sign in to comment.