Skip to content

Commit

Permalink
Implemented WeekField
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Jan 13, 2023
1 parent 22e766c commit a15ed73
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
- Python 3.11 support :pr:`763`
- Added shorter format to :class:`~fields.DateTimeLocalField`
defaults :pr:`761`
- Added shorter format to :class:`~fields.WeekField`
defaults :pr:`765`

Version 3.0.1
-------------
Expand Down
34 changes: 34 additions & 0 deletions src/wtforms/fields/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"TimeField",
"MonthField",
"DateTimeLocalField",
"WeekField",
)


Expand Down Expand Up @@ -114,6 +115,39 @@ def __init__(self, label=None, validators=None, format="%Y-%m", **kwargs):
super().__init__(label, validators, format, **kwargs)


class WeekField(DateField):
"""
Same as :class:`~wtforms.fields.DateField`, except represents a week,
stores a :class:`datetime.date` of the monday of the given week.
"""

widget = widgets.WeekInput()

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

def process_formdata(self, valuelist):
if not valuelist:
return

time_str = " ".join(valuelist)
for format in self.strptime_format:
try:
if "%w" not in format:
# The '%w' week starting day is needed. This defaults it to monday
# like ISO 8601 indicates.
self.data = datetime.datetime.strptime(
f"{time_str}-1", f"{format}-%w"
).date()
else:
self.data = datetime.datetime.strptime(time_str, format).date()
return
except ValueError:
self.data = None

raise ValueError(self.gettext("Not a valid week value."))


class DateTimeLocalField(DateTimeField):
"""
Same as :class:`~wtforms.fields.DateTimeField`, but represents an
Expand Down
36 changes: 36 additions & 0 deletions tests/fields/test_week.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from datetime import date

from tests.common import DummyPostData

from wtforms.fields import WeekField
from wtforms.form import Form


class F(Form):
a = WeekField()
b = WeekField(format="W%W %Y")
c = WeekField(format="%Y-W%W-%w")


def test_basic():
form = F(DummyPostData(a=["2023-W03"], b=["W03 2023"], c=["2023-W03-0"]))

assert form.a.data == date(2023, 1, 16)
assert "2023-W03" == form.a._value()
assert form.a() == '<input id="a" name="a" type="week" value="2023-W03">'

assert form.b.data == date(2023, 1, 16)
assert form.b() == '<input id="b" name="b" type="week" value="W03 2023">'

# %W makes the week count on the first monday of the year
assert form.c.data == date(2023, 1, 22)
assert form.c() == '<input id="c" name="c" type="week" value="2023-W03-0">'


def test_invalid_data():
form = F(DummyPostData(a=["2008-Wbb"]))

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

0 comments on commit a15ed73

Please sign in to comment.