Skip to content
This repository has been archived by the owner on Aug 2, 2019. It is now read-only.

Commit

Permalink
Add date field type (ref #51)
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed May 30, 2013
1 parent 2f0c663 commit c65fe35
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
9 changes: 8 additions & 1 deletion daybed/schemas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
Boolean,
Regex,
Email,
Date
)


__all__ = ['registry', 'TypeField',
'DefinitionValidator', 'SchemaValidator',
'IntField', 'StringField', 'RangeField',
'RegexField', 'EmailField', 'URLField',
'DecimalField']
'DecimalField', 'DateField']


class AlreadyRegisteredError(Exception):
Expand Down Expand Up @@ -233,3 +234,9 @@ def validation(cls, **kwargs):
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
kwargs['validator'] = Regex(urlpattern, msg="Invalid URL")
return super(URLField, cls).validation(**kwargs)


@registry.add('date')
class DateField(TypeField):
"""A date field (ISO_8601, yyyy-mm-dd)."""
node = Date
30 changes: 30 additions & 0 deletions daybed/tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,36 @@ def update_data(self, entry):
entry['status'] = 'done'


class TimestampedModelTest(FunctionalTest, BaseWebTest):

model_name = 'timestamped'

@property
def valid_definition(self):
return {
"title": "timestamped",
"description": "Playing with date fields",
"fields": [
{
"name": "creation",
"type": "date",
"description": "created on"
}
]
}

@property
def valid_data(self):
return {'creation': '2012-04-15'}

@property
def invalid_data(self):
return {'creation': '15-04-2012'}

def update_data(self, entry):
entry['creation'] = '2013-05-30'


class MushroomsModelTest(FunctionalTest, BaseWebTest):

model_name = 'mushroom_spots'
Expand Down
17 changes: 17 additions & 0 deletions daybed/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import pyramid.testing
import colander

Expand Down Expand Up @@ -90,6 +92,21 @@ def test_url(self):
validator.deserialize('http://daybed.lolnet.org'))
self.assertRaises(colander.Invalid, validator.deserialize, 'http://lolnet/org')

def test_date(self):
schema = schemas.DateField.definition()
definition = schema.deserialize(
{'description': 'First commit',
'name': 'creation',
'type': 'date'})

validator = schemas.DateField.validation(**definition)
self.assertEquals(datetime.date(2012, 4, 15),
validator.deserialize('2012-04-15'))
self.assertRaises(colander.Invalid, validator.deserialize, '2012/04/15')
self.assertRaises(colander.Invalid, validator.deserialize, '2012-13-01')
self.assertRaises(colander.Invalid, validator.deserialize, '2012-04-31')
self.assertRaises(colander.Invalid, validator.deserialize, '2012-04-30T13:37Z')

def test_point(self):
schema = schemas.PointField.definition()
definition = schema.deserialize(
Expand Down

0 comments on commit c65fe35

Please sign in to comment.