From 909ce5b27d8b4d2aa49c43505474183c3559888d Mon Sep 17 00:00:00 2001 From: Richard Schreiber Date: Fri, 22 May 2026 13:51:28 +0200 Subject: [PATCH] Add NoUrlValidator to validators (#6208) --- src/pretix/base/validators.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pretix/base/validators.py b/src/pretix/base/validators.py index 6a1ede39c0..4ff47bee7d 100644 --- a/src/pretix/base/validators.py +++ b/src/pretix/base/validators.py @@ -24,10 +24,12 @@ import calendar from dateutil.rrule import DAILY, MONTHLY, WEEKLY, YEARLY, rrule, rrulestr from django.conf import settings from django.core.exceptions import ValidationError -from django.core.validators import validate_email +from django.core.validators import RegexValidator, validate_email from django.utils.deconstruct import deconstructible from django.utils.translation import gettext_lazy as _ +from pretix.base.templatetags.rich_text import URL_RE + # This file is based on an earlier version of pretix which was released under the Apache License 2.0. The full text of # the Apache License 2.0 can be obtained at . # @@ -113,6 +115,33 @@ def multimail_validate(val): return s +class RegexValidatorInverseMatchAndParam(RegexValidator): + inverse_match = True + + def __call__(self, value): + regex_matches = self.regex.search(str(value)) + if regex_matches: + raise ValidationError( + self.message, + code=self.code, + params={ + "value": value, + "match": regex_matches.group(0) if regex_matches else "", + } + ) + + +class NoUrlValidator(RegexValidatorInverseMatchAndParam): + regex = URL_RE + + def __init__(self, **kwargs): + if not kwargs.get("message"): + kwargs["message"] = _('You entered an URL, which is not allowed. Please remove %(match)s from your input.') + if not kwargs.get("code"): + kwargs["code"] = "contains_url" + super().__init__(**kwargs) + + class RRuleValidator: def __init__(self, enforce_simple=False): self.enforce_simple = enforce_simple