From 5443032399db34c337c01c7736e6850d44bdccdb Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Thu, 4 Apr 2024 15:44:26 +0530 Subject: [PATCH] Remove form_nonce (stage 2 of 2) (#476) --- pyproject.toml | 16 ++++++------ src/baseframe/forms/fields.py | 25 +------------------ src/baseframe/forms/form.py | 4 --- .../baseframe/bootstrap3/forms.html.jinja2 | 4 +-- .../templates/baseframe/mui/forms.html.jinja2 | 4 +-- 5 files changed, 13 insertions(+), 40 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4b965f66..1dd69b45 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -229,7 +229,7 @@ fixable = [ "UP", "YTT", ] -unfixable = [] +lint.unfixable = [] # Exclude a variety of commonly ignored directories. exclude = [ @@ -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'] @@ -284,5 +284,5 @@ section-order = [ 'local-folder', ] -[tool.ruff.isort.sections] +[tool.ruff.lint.isort.sections] repo = ['baseframe'] diff --git a/src/baseframe/forms/fields.py b/src/baseframe/forms/fields.py index 77048713..413deef1 100644 --- a/src/baseframe/forms/fields.py +++ b/src/baseframe/forms/fields.py @@ -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 ( @@ -55,7 +55,6 @@ 'FieldList', 'FileField', 'Label', - 'NonceField', 'RecaptchaField', 'SelectMultipleField', 'SubmitField', @@ -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.""" diff --git a/src/baseframe/forms/form.py b/src/baseframe/forms/form.py index e92fe306..20b1e5e1 100644 --- a/src/baseframe/forms/form.py +++ b/src/baseframe/forms/form.py @@ -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, @@ -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) diff --git a/src/baseframe/templates/baseframe/bootstrap3/forms.html.jinja2 b/src/baseframe/templates/baseframe/bootstrap3/forms.html.jinja2 index 4b13153c..958ccdc9 100644 --- a/src/baseframe/templates/baseframe/bootstrap3/forms.html.jinja2 +++ b/src/baseframe/templates/baseframe/bootstrap3/forms.html.jinja2 @@ -23,7 +23,7 @@ {{ field.label.text }} {%- 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 %} @@ -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) }} diff --git a/src/baseframe/templates/baseframe/mui/forms.html.jinja2 b/src/baseframe/templates/baseframe/mui/forms.html.jinja2 index e4fb1069..3eb62795 100644 --- a/src/baseframe/templates/baseframe/mui/forms.html.jinja2 +++ b/src/baseframe/templates/baseframe/mui/forms.html.jinja2 @@ -24,7 +24,7 @@ {{ field.label.text }} {%- 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 %} @@ -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) }}