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 7b8298d commit 0a0ff7a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
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 3.0.0
-------------
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
11 changes: 11 additions & 0 deletions src/wtforms/fields/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"SelectMultipleField",
"StringField",
"TimeField",
"MonthField",
)


Expand Down Expand Up @@ -889,6 +890,16 @@ 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().__init__(label, validators, format, **kwargs)


class FormField(Field):
"""
Encapsulate a form as a field in another form.
Expand Down
8 changes: 8 additions & 0 deletions src/wtforms/fields/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ 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
24 changes: 24 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from wtforms.fields import IntegerField
from wtforms.fields import IntegerRangeField
from wtforms.fields import Label
from wtforms.fields import MonthField
from wtforms.fields import MultipleFileField
from wtforms.fields import PasswordField
from wtforms.fields import RadioField
Expand Down Expand Up @@ -756,6 +757,29 @@ def test_failure(self):
assert form.a.process_errors[0] == "Not a valid date value"


class TestMonthField:
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 TestTimeField:
class F(Form):
a = TimeField()
Expand Down

0 comments on commit 0a0ff7a

Please sign in to comment.