Allow template syntax in event text (Z#23140046) (#3815)

* remove duplicate context generation

* allow text templates in frontpage_text

* refactor: move placeholder functionality to separate file

* fix wrong class name, code style

* update year in license header

* undo license header update

* use new function name

* render only the placeholders that are actually used in the message

* refactoring

* add str(...) call

* Update doc/development/api/placeholder.rst

Co-authored-by: Raphael Michel <michel@rami.io>

* rename register_mail_placeholders to register_template_placeholders
(deprecate old name)

* isort

* add signals to docs

---------

Co-authored-by: Raphael Michel <michel@rami.io>
This commit is contained in:
Mira
2024-02-06 11:32:03 +01:00
committed by GitHub
parent 45ac391998
commit bac673f3ab
13 changed files with 654 additions and 582 deletions

View File

@@ -60,12 +60,11 @@ from i18nfield.forms import (
from pytz import common_timezones
from pretix.base.channels import get_all_sales_channels
from pretix.base.email import get_available_placeholders
from pretix.base.forms import I18nModelForm, PlaceholderValidator, SettingsForm
from pretix.base.forms.widgets import format_placeholders_help_text
from pretix.base.models import Event, Organizer, TaxRule, Team
from pretix.base.models.event import EventFooterLink, EventMetaValue, SubEvent
from pretix.base.reldate import RelativeDateField, RelativeDateTimeField
from pretix.base.services.placeholders import FormPlaceholderMixin
from pretix.base.settings import (
COUNTRIES_WITH_STATE_IN_ADDRESS, DEFAULTS, PERSON_NAME_SCHEMES,
PERSON_NAME_TITLE_GROUPS, validate_event_settings,
@@ -503,7 +502,7 @@ class EventSettingsValidationMixin:
del self.cleaned_data[field]
class EventSettingsForm(EventSettingsValidationMixin, SettingsForm):
class EventSettingsForm(EventSettingsValidationMixin, FormPlaceholderMixin, SettingsForm):
timezone = forms.ChoiceField(
choices=((a, a) for a in common_timezones),
label=_("Event timezone"),
@@ -593,6 +592,10 @@ class EventSettingsForm(EventSettingsValidationMixin, SettingsForm):
'og_image',
]
base_context = {
'frontpage_text': ['event'],
}
def _resolve_virtual_keys_input(self, data, prefix=''):
# set all dependants of virtual_keys and
# delete all virtual_fields to prevent them from being saved
@@ -682,6 +685,9 @@ class EventSettingsForm(EventSettingsValidationMixin, SettingsForm):
else:
self.initial[virtual_key] = 'do_not_ask'
for k, v in self.base_context.items():
self._set_field_placeholders(k, v)
@cached_property
def changed_data(self):
data = []
@@ -932,7 +938,7 @@ def contains_web_channel_validate(val):
raise ValidationError(_("The online shop must be selected to receive these emails."))
class MailSettingsForm(SettingsForm):
class MailSettingsForm(FormPlaceholderMixin, SettingsForm):
auto_fields = [
'mail_prefix',
'mail_from_name',
@@ -1344,17 +1350,6 @@ class MailSettingsForm(SettingsForm):
'mail_attach_ical_description': ['event', 'event_or_subevent'],
}
def _set_field_placeholders(self, fn, base_parameters):
placeholders = get_available_placeholders(self.event, base_parameters)
ht = format_placeholders_help_text(placeholders, self.event)
if self.fields[fn].help_text:
self.fields[fn].help_text += ' ' + str(ht)
else:
self.fields[fn].help_text = ht
self.fields[fn].validators.append(
PlaceholderValidator(['{%s}' % p for p in placeholders.keys()])
)
def __init__(self, *args, **kwargs):
self.event = event = kwargs.get('obj')
super().__init__(*args, **kwargs)