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

Minor typing and spelling fixes #457

Merged
merged 3 commits into from
Dec 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 13 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,12 @@ 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:
"""Get default value."""
if callable(default := self.default):
return default()
return default


class RecaptchaField(RecaptchaFieldBase):
"""RecaptchaField with an improved validator."""
Expand Down Expand Up @@ -270,7 +278,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 +373,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 +389,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 +412,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
6 changes: 3 additions & 3 deletions src/baseframe/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask import current_app, render_template
from furl import furl
from markupsafe import Markup, escape
from wtforms import Field as WTField
from wtforms import Field as WTField, SelectFieldBase
from wtforms.widgets import RadioInput, Select, html_params

from ..extensions import _
Expand All @@ -31,7 +31,7 @@
class SelectWidget(Select):
"""Add support of choices with ``optgroup`` to the ``Select`` widget."""

def __call__(self, field: WTField, **kwargs: t.Any) -> Markup:
def __call__(self, field: SelectFieldBase, **kwargs: t.Any) -> Markup:
kwargs.setdefault('id', field.id)
if self.multiple:
kwargs['multiple'] = True
Expand Down Expand Up @@ -61,7 +61,7 @@ def __call__(self, field: WTField, **kwargs: t.Any) -> Markup:
class Select2Widget(Select):
"""Add a select2 class to the rendered select widget."""

def __call__(self, field: WTField, **kwargs: t.Any) -> Markup:
def __call__(self, field: SelectFieldBase, **kwargs: t.Any) -> Markup:
kwargs.setdefault('id', field.id)
kwargs.pop('type', field.type)
if field.multiple:
Expand Down