mirror of
https://github.com/pretix/pretix.git
synced 2026-05-04 15:04:03 +00:00
* Integrated PlaceholderValidator to MailForm at plugins/sendmail * Integrated PlaceholderValidator to MailForm and MailSettingsForm * Typo
This commit is contained in:
committed by
Raphael Michel
parent
990d5815f2
commit
bd5337a2c2
@@ -6,6 +6,7 @@ from django.utils import six
|
||||
from django.utils.crypto import get_random_string
|
||||
from hierarkey.forms import HierarkeyForm
|
||||
|
||||
from .validators import PlaceholderValidator
|
||||
from pretix.base.models import Event
|
||||
|
||||
logger = logging.getLogger('pretix.plugins.ticketoutputpdf')
|
||||
|
||||
31
src/pretix/base/forms/validators.py
Normal file
31
src/pretix/base/forms/validators.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import re
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import BaseValidator
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
||||
class PlaceholderValidator(BaseValidator):
|
||||
"""
|
||||
Takes list of allowed placeholders,
|
||||
validates form field by checking for placeholders,
|
||||
which are not presented in taken list.
|
||||
"""
|
||||
|
||||
def __init__(self, limit_value):
|
||||
super().__init__(limit_value)
|
||||
self.limit_value = limit_value
|
||||
|
||||
def __call__(self, value):
|
||||
data_placeholders = list(re.findall(r'({[\w\s]*})', value, re.X))
|
||||
invalid_placeholders = []
|
||||
for placeholder in data_placeholders:
|
||||
if placeholder not in self.limit_value:
|
||||
invalid_placeholders.append(placeholder)
|
||||
if invalid_placeholders:
|
||||
raise ValidationError(
|
||||
_('Invalid placeholder(s): %(value)s'),
|
||||
code='invalid',
|
||||
params={'value': ", ".join(invalid_placeholders,)})
|
||||
|
||||
def clean(self, x):
|
||||
return x
|
||||
Reference in New Issue
Block a user