Skip to content

Commit

Permalink
Remove form_nonce (stage 2 of 2) (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
jace authored Apr 4, 2024
1 parent 27aff27 commit 5443032
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 40 deletions.
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ skips = ['*/*_test.py', '*/test_*.py']
# 3. Rule E501 (line too long) is left to Black; some strings are worse for wrapping

# Enable pycodestyle (`E`) and Pyflakes (`F`) codes by default.
select = ["E", "F"]
ignore = ["E402", "E501"]
lint.select = ["E", "F"]
lint.ignore = ["E402", "E501"]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = [
lint.fixable = [
"A",
"B",
"C",
Expand Down Expand Up @@ -229,7 +229,7 @@ fixable = [
"UP",
"YTT",
]
unfixable = []
lint.unfixable = []

# Exclude a variety of commonly ignored directories.
exclude = [
Expand Down Expand Up @@ -259,16 +259,16 @@ exclude = [
line-length = 88

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
lint.dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

# Target Python 3.9
target-version = "py39"

[tool.ruff.mccabe]
[tool.ruff.lint.mccabe]
# Unlike Flake8, default to a complexity level of 10.
max-complexity = 10

[tool.ruff.isort]
[tool.ruff.lint.isort]
# These config options should match isort config above under [tool.isort]
combine-as-imports = true
extra-standard-library = ['typing_extensions']
Expand All @@ -284,5 +284,5 @@ section-order = [
'local-folder',
]

[tool.ruff.isort.sections]
[tool.ruff.lint.isort.sections]
repo = ['baseframe']
25 changes: 1 addition & 24 deletions src/baseframe/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from ..extensions import _, __, get_timezone
from ..utils import request_timestamp
from .parsleyjs import HiddenField, StringField, TextAreaField, URLField
from .parsleyjs import StringField, TextAreaField, URLField
from .typing import ReturnIterChoices, ValidatorList
from .validators import Recaptcha, StopValidation, ValidationError
from .widgets import (
Expand All @@ -55,7 +55,6 @@
'FieldList',
'FileField',
'Label',
'NonceField',
'RecaptchaField',
'SelectMultipleField',
'SubmitField',
Expand Down Expand Up @@ -110,28 +109,6 @@ class GeonameidProtocol(te.Protocol):
geonameid: str


class NonceField(HiddenField):
"""Customized HiddenField for nonce values that ignores the form target object."""

def process(
self,
formdata: MultiDict,
data: t.Optional[t.Dict[str, t.Any]] = None,
extra_filters: t.Optional[t.Iterable[t.Callable[[t.Any], t.Any]]] = None,
) -> None:
"""Discard data coming from an object."""
super().process(formdata, extra_filters=extra_filters)

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
4 changes: 0 additions & 4 deletions src/baseframe/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from wtforms import Field as WTField
from wtforms.utils import unset_value

from ..extensions import __
from ..signals import form_validation_error, form_validation_success
from . import (
fields as bfields,
Expand Down Expand Up @@ -102,9 +101,6 @@ class Form(BaseForm):
__expects__: t.Iterable[str] = ()
__returns__: t.Iterable[str] = ()

form_nonce = bfields.NonceField("Nonce", default=lambda: '')
form_nonce_error = __("This form has already been submitted")

def __init_subclass__(cls, **kwargs: t.Any) -> None:
"""Validate :attr:`__expects__` and :attr:`__returns__` in sub-classes."""
super().__init_subclass__(**kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{{ field.label.text }}
</legend>
{%- for subfield in field.form %}
{%- if not subfield.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{%- if not subfield.type in ['CSRFTokenField', 'HiddenField'] -%}
{{ renderfield(field=subfield, css_class=css_class, widget_css_class=widget_css_class, sidetext=sidetext, tabindex=tabindex, autofocus=false, nolabel=nolabel, style=style, rows=rows) }}
{%- endif %}
{%- endfor %}
Expand Down Expand Up @@ -135,7 +135,7 @@
{%- endif %}
{%- set autofocus = true %}
{% for field in form -%}
{%- if field.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{%- if field.type in ['CSRFTokenField', 'HiddenField'] -%}
{# Don't show hidden #}
{%- else -%}
{{ renderfield(field, autofocus=autofocus, style=style) }}
Expand Down
4 changes: 2 additions & 2 deletions src/baseframe/templates/baseframe/mui/forms.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{{ field.label.text }}
</legend>
{%- for subfield in field.form %}
{%- if not subfield.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{%- if not subfield.type in ['CSRFTokenField', 'HiddenField'] -%}
{{ renderfield(field=subfield, css_class=css_class, widget_css_class=widget_css_class, sidetext=sidetext, tabindex=tabindex, autofocus=false, nolabel=nolabel, style=style, rows=rows) }}
{%- endif %}
{%- endfor %}
Expand Down Expand Up @@ -189,7 +189,7 @@
{%- set autofocus = true %}
{% set autofocus = namespace(val=true) %}
{% for field in form -%}
{%- if field.type in ['CSRFTokenField', 'HiddenField', 'NonceField'] -%}
{%- if field.type in ['CSRFTokenField', 'HiddenField'] -%}
{# Don't show hidden #}
{%- else -%}
{{ renderfield(field, autofocus=autofocus.val, style=style) }}
Expand Down

0 comments on commit 5443032

Please sign in to comment.