Skip to content

Commit

Permalink
Merge 6f6999e into 2270008
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Dec 18, 2023
2 parents 2270008 + 6f6999e commit 755b789
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/baseframe/forms/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def render_form(
with_chrome: bool = True,
action: t.Optional[str] = None,
autosave: bool = False,
draft_revision: t.Optional[int] = None,
draft_revision: t.Optional[t.Any] = None,
template: str = '',
) -> Response:
"""Render a form."""
Expand Down
17 changes: 12 additions & 5 deletions src/baseframe/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
from werkzeug.datastructures import MultiDict
from wtforms import Form as WTForm
from wtforms.fields import (
DateTimeField as DateTimeFieldBase,
Field,
FieldList,
FileField,
Label,
SelectField as SelectFieldBase,
SelectMultipleField,
SubmitField,
TextAreaField as TextAreaFieldBase,
)
from wtforms.utils import unset_value
from wtforms.widgets import Select as OriginalSelectWidget
Expand Down Expand Up @@ -122,6 +124,11 @@ def process(
def populate_obj(self, *_args: t.Any, **_kwargs: t.Any) -> None:
"""Override populate_obj to not attempt setting nonce on the object."""

def get_default(self) -> str:
if callable(default := self.default):
return default()
return default


class RecaptchaField(RecaptchaFieldBase):
"""RecaptchaField with an improved validator."""
Expand Down Expand Up @@ -270,7 +277,7 @@ def process_formdata(self, valuelist: t.List[str]) -> None:
self.data = bleach.linkify(self.data or '', callbacks=[])


class DateTimeField(wtforms.fields.DateTimeField):
class DateTimeField(DateTimeFieldBase):
"""
A text field which stores a `datetime.datetime` matching a format.
Expand Down Expand Up @@ -365,14 +372,14 @@ def process_formdata(self, valuelist: t.List[str]) -> None:
if valuelist:
# We received a timestamp from the browser. Parse and save it
data: t.Optional[datetime] = None
# Valuelist will contain `date` and `time` as two separate values
# `valuelist` will contain `date` and `time` as two separate values
# if the widget is rendered as two parts. If so, parse each at a time
# and use it as a default to replace values from the next value. If the
# front-end renders a single widget, the entire content will be parsed once.
for value in valuelist:
if value.strip():
try:
# dateutil cannot handle ISO and European-style dates at the
# `dateutil` cannot handle ISO and European-style dates at the
# same time, so `dayfirst` MUST be False. Setting it to True
# will interpret YYYY-DD-MM instead of YYYY-MM-DD. Bug report:
# https://github.com/dateutil/dateutil/issues/402
Expand All @@ -381,7 +388,7 @@ def process_formdata(self, valuelist: t.List[str]) -> None:
)
except (ValueError, OverflowError, TypeError):
# TypeError is not a documented error for `parser.parse`, but
# the DateTimeField implementation in wtforms_dateutil says
# the DateTimeField implementation in `wtforms_dateutil` says
# it can happen due to a known bug
raise ValidationError(self.message) from None
if data is not None:
Expand All @@ -404,7 +411,7 @@ def process_formdata(self, valuelist: t.List[str]) -> None:
self.data = None


class TextListField(wtforms.fields.TextAreaField):
class TextListField(TextAreaFieldBase):
"""A list field that renders as a textarea with one line per list item."""

def _value(self) -> str:
Expand Down

0 comments on commit 755b789

Please sign in to comment.