diff options
Diffstat (limited to 'webmentions_ssg')
| -rw-r--r-- | webmentions_ssg/forms.py | 51 |
1 files changed, 5 insertions, 46 deletions
diff --git a/webmentions_ssg/forms.py b/webmentions_ssg/forms.py index d557907..af24423 100644 --- a/webmentions_ssg/forms.py +++ b/webmentions_ssg/forms.py @@ -72,51 +72,6 @@ class HTTPXURL: raise StopValidation(field.gettext("Invalid URL.")) -class NotEqualTo: - """Validate that a field does not equal another field. - - :param fieldname: Name of the field to compare against. - :param message: Optional validation error message. - """ - - def __init__(self, fieldname: str, message: str | None = None) -> None: - """Initialize the validator. - - :param fieldname: Name of the field to compare against. - :param message: Optional validation error message. - """ - self.fieldname = fieldname - self.message = message - - def __call__(self, form: BaseForm, field: Field) -> None: - """Validate that two fields do not contain equal values. - - :param form: Form containing the fields to compare. - :param field: Field being validated. - :raises ValidationError: If the comparison field does not exist or both - fields contain equal values. - """ - try: - other = form[self.fieldname] - except KeyError as exc: - raise ValidationError( - field.gettext("Invalid field name '%s'.") % self.fieldname - ) from exc - - if field.data != other.data: - return - - d = { - "other_label": (hasattr(other, "label") and other.label.text) - or self.fieldname, - "other_name": self.fieldname, - } - message = self.message or field.gettext( - "Field must not be equal to %(other_name)s." - ) - raise ValidationError(message % d) - - class AllowedHostname: """Validate that a URL uses an allowed hostname. @@ -194,7 +149,6 @@ class EndpointForm(FlaskForm): flags=re.IGNORECASE, message="source must begin with http or https", ), - NotEqualTo("target"), PublicIPLiteral(), ], ) @@ -214,6 +168,11 @@ class EndpointForm(FlaskForm): ], ) + def validate_source(self, field: Field) -> None: + """Reject a source URL that equals the target URL.""" + if field.data == self.target.data: + raise ValidationError(field.gettext("Field must not be equal to target.")) + class AdminActionForm(FlaskForm): """Form for confirming an administrative action.""" |
