diff --git a/src/pretix/plugins/sendmail/models.py b/src/pretix/plugins/sendmail/models.py index 29001a6d73..2e3c049ccd 100644 --- a/src/pretix/plugins/sendmail/models.py +++ b/src/pretix/plugins/sendmail/models.py @@ -31,6 +31,7 @@ from django_scopes import ScopedManager from i18nfield.fields import I18nCharField, I18nTextField from pretix.base.email import get_email_context +from pretix.base.i18n import language from pretix.base.models import ( Event, InvoiceAddress, Item, Order, OrderPosition, SubEvent, ) @@ -133,44 +134,45 @@ class ScheduledMail(models.Model): send_to_attendees = self.rule.send_to in (Rule.ATTENDEES, Rule.BOTH) for o in orders: - positions = list(o.positions.all()) - o_sent = False + with language(o.locale, e.settings.region): + positions = list(o.positions.all()) + o_sent = False - try: - ia = o.invoice_address - except InvoiceAddress.DoesNotExist: - ia = InvoiceAddress(order=o) - - if send_to_orders and o.email: - email_ctx = get_email_context(event=e, order=o, invoice_address=ia) try: - o.send_mail(self.rule.subject, self.rule.template, email_ctx, - log_entry_type='pretix.plugins.sendmail.rule.order.email.sent') - o_sent = True - except SendMailException: - ... # ¯\_(ツ)_/¯ + ia = o.invoice_address + except InvoiceAddress.DoesNotExist: + ia = InvoiceAddress(order=o) - if send_to_attendees: - if not self.rule.all_products: - positions = [p for p in positions if p.item_id in limit_products] - if self.subevent_id: - positions = [p for p in positions if p.subevent_id == self.subevent_id] - - for p in positions: + if send_to_orders and o.email: + email_ctx = get_email_context(event=e, order=o, invoice_address=ia) try: - if p.attendee_email and (p.attendee_email != o.email or not o_sent): - email_ctx = get_email_context(event=e, order=o, invoice_address=ia, position=p) - p.send_mail(self.rule.subject, self.rule.template, email_ctx, - log_entry_type='pretix.plugins.sendmail.rule.order.position.email.sent') - elif not o_sent and o.email: - email_ctx = get_email_context(event=e, order=o, invoice_address=ia) - o.send_mail(self.rule.subject, self.rule.template, email_ctx, - log_entry_type='pretix.plugins.sendmail.rule.order.email.sent') - o_sent = True + o.send_mail(self.rule.subject, self.rule.template, email_ctx, + log_entry_type='pretix.plugins.sendmail.rule.order.email.sent') + o_sent = True except SendMailException: ... # ¯\_(ツ)_/¯ - self.last_successful_order_id = o.pk + if send_to_attendees: + if not self.rule.all_products: + positions = [p for p in positions if p.item_id in limit_products] + if self.subevent_id: + positions = [p for p in positions if p.subevent_id == self.subevent_id] + + for p in positions: + try: + if p.attendee_email and (p.attendee_email != o.email or not o_sent): + email_ctx = get_email_context(event=e, order=o, invoice_address=ia, position=p) + p.send_mail(self.rule.subject, self.rule.template, email_ctx, + log_entry_type='pretix.plugins.sendmail.rule.order.position.email.sent') + elif not o_sent and o.email: + email_ctx = get_email_context(event=e, order=o, invoice_address=ia) + o.send_mail(self.rule.subject, self.rule.template, email_ctx, + log_entry_type='pretix.plugins.sendmail.rule.order.email.sent') + o_sent = True + except SendMailException: + ... # ¯\_(ツ)_/¯ + + self.last_successful_order_id = o.pk class Rule(models.Model, LoggingMixin): diff --git a/src/tests/plugins/sendmail/test_rules.py b/src/tests/plugins/sendmail/test_rules.py index 972e11a2e3..bc1f25b17f 100644 --- a/src/tests/plugins/sendmail/test_rules.py +++ b/src/tests/plugins/sendmail/test_rules.py @@ -28,7 +28,7 @@ from django.core import mail as djmail from django.utils.timezone import now, utc from django_scopes import scopes_disabled -from pretix.base.models import Order +from pretix.base.models import InvoiceAddress, Order from pretix.plugins.sendmail.models import Rule, ScheduledMail from pretix.plugins.sendmail.signals import sendmail_run_rules @@ -361,3 +361,22 @@ def test_sendmail_rule_disabled(event, order): sendmail_run_rules(None) assert len(djmail.outbox) == 0 + + +@pytest.mark.django_db +@scopes_disabled() +def test_sendmail_context_localization(event, order, pos): + order.locale = 'de' + order.save() + event.settings.name_scheme = 'salutation_given_family' + InvoiceAddress.objects.create( + order=order, + name_parts={'_scheme': 'salutation_given_family', 'salutation': 'Mr', 'given_name': 'Max', 'family_name': 'Mustermann'} + ) + + djmail.outbox = [] + event.sendmail_rules.create(send_date=dt_now - datetime.timedelta(hours=1), include_pending=True, + subject='meow', template='Hallo {name_for_salutation}') + + sendmail_run_rules(None) + assert "Hallo Herr Mustermann" in djmail.outbox[0].body