Skip to content

Commit

Permalink
Implemented MonthField
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Apr 24, 2020
1 parent bd03955 commit af978db
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
.. currentmodule:: wtforms

Version 2.x.x
-------------

Unreleased

- Implemented :class:`~wtforms.fields.core.MonthField`. :pr:`530` :pr:`593`


Version 2.3.1
-------------
Expand Down
2 changes: 2 additions & 0 deletions docs/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ namespace and can be overridden or modified just like any other widget.

.. autoclass:: DateField(default field arguments, format='%Y-%m-%d')

.. autoclass:: MonthField(default field arguments, format='%Y-%m')

.. autoclass:: TimeField(default field arguments, format='%H:%M')

.. autoclass:: DateTimeLocalField(default field arguments, format='%Y-%m-%d %H:%M:%S')
Expand Down
25 changes: 25 additions & 0 deletions tests/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,29 @@ def test_failure(self):
self.assertEqual(form.a.process_errors[0], 'Not a valid date value')


class TestDateField(TestCase):
class F(Form):
a = MonthField()
b = MonthField(format='%m/%Y')

def test_basic(self):
d = date(2008, 5, 1)
form = self.F(DummyPostData(a=['2008-05'], b=['05/2008']))

assert d == form.a.data
assert '2008-05' == form.a._value()

assert d == form.b.data

def test_failure(self):
form = self.F(DummyPostData(a=['2008-bb']))

assert not form.validate()
assert 1 == len(form.a.process_errors)
assert 1 == len(form.a.errors)
assert 'Not a valid date value' == form.a.process_errors[0]


class TimeFieldTest(TestCase):
class F(Form):
a = TimeField()
Expand Down Expand Up @@ -962,6 +985,7 @@ class F(Form):
email = html5.EmailField()
datetime = html5.DateTimeField()
date = html5.DateField()
month = html5.MonthField()
dt_local = html5.DateTimeLocalField()
integer = html5.IntegerField()
decimal = html5.DecimalField()
Expand Down Expand Up @@ -989,6 +1013,7 @@ def test_simple(self):
b('email', 'foo@bar.com', 'type="email"'),
b('datetime', '2013-09-05 00:23:42', 'type="datetime"', datetime(2013, 9, 5, 0, 23, 42)),
b('date', '2013-09-05', 'type="date"', date(2013, 9, 5)),
b('month', '2014-05', 'type="month"', date(2014, 5, 1)),
b('dt_local', '2013-09-05 00:23:42', 'type="datetime-local"', datetime(2013, 9, 5, 0, 23, 42)),
b('integer', '42', '<input id="integer" name="integer" type="number" value="42">', 42),
b('decimal', '43.5', '<input id="decimal" name="decimal" step="any" type="number" value="43.5">', Decimal('43.5')),
Expand Down
11 changes: 10 additions & 1 deletion wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
__all__ = (
'BooleanField', 'DecimalField', 'DateField', 'DateTimeField', 'FieldList',
'FloatField', 'FormField', 'IntegerField', 'RadioField', 'SelectField',
'SelectMultipleField', 'StringField', 'TimeField',
'SelectMultipleField', 'StringField', 'TimeField', 'MonthField',
)


Expand Down Expand Up @@ -827,6 +827,15 @@ def process_formdata(self, valuelist):
raise ValueError(self.gettext('Not a valid time value'))


class MonthField(DateField):
"""
Same as DateField, except represents a month, stores a `datetime.date` with `day = 1`.
"""

def __init__(self, label=None, validators=None, format="%Y-%m", **kwargs):
super(MonthField, self).__init__(label, validators, format, **kwargs)


class FormField(Field):
"""
Encapsulate a form as a field in another form.
Expand Down
7 changes: 7 additions & 0 deletions wtforms/fields/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class TimeField(core.TimeField):
widget = widgets.TimeInput()


class MonthField(core.MonthField):
"""
Represents an ``<input type="month">``.
"""
widget = widgets.MonthInput()


class DateTimeLocalField(core.DateTimeField):
"""
Represents an ``<input type="datetime-local">``.
Expand Down

0 comments on commit af978db

Please # to comment.