Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented WeekField #765

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Unreleased
- Added shorter format to :class:`~fields.DateTimeLocalField`
defaults :pr:`761`
- Stop support for python 3.7 :pr:`794`
- 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]