Improve algorithm for {name} placeholder (#1745)

Co-authored-by: Felix Rindt <felix@rindt.me>
This commit is contained in:
Raphael Michel
2020-08-19 11:30:34 +02:00
committed by GitHub
parent b2357b7e29
commit c94d384e86
3 changed files with 26 additions and 13 deletions

View File

@@ -258,9 +258,30 @@ def _placeholder_payment(order, payment):
return str(payment.payment_provider.order_pending_mail_render(order))
def get_best_name(position_or_address, parts=False):
"""
Return the best name we got for either an invoice address or an order position, falling back to the respective other
"""
from pretix.base.models import InvoiceAddress, OrderPosition
if isinstance(position_or_address, InvoiceAddress):
if position_or_address.name:
return position_or_address.name_parts if parts else position_or_address.name
position_or_address = position_or_address.order.positions.exclude(attendee_name_cached="").exclude(attendee_name_cached__isnull=True).first()
if isinstance(position_or_address, OrderPosition):
if position_or_address.attendee_name:
return position_or_address.attendee_name_parts if parts else position_or_address.attendee_name
elif position_or_address.order:
try:
return position_or_address.order.invoice_address.name_parts if parts else position_or_address.order.invoice_address.name
except InvoiceAddress.DoesNotExist:
pass
return {} if parts else ""
@receiver(register_mail_placeholders, dispatch_uid="pretixbase_register_mail_placeholders")
def base_placeholders(sender, **kwargs):
from pretix.base.models import InvoiceAddress
from pretix.multidomain.urlreverse import build_absolute_uri
ph = [
@@ -474,11 +495,7 @@ def base_placeholders(sender, **kwargs):
),
SimpleFunctionalMailTextPlaceholder(
'name', ['position_or_address'],
lambda position_or_address: (
position_or_address.name
if isinstance(position_or_address, InvoiceAddress)
else position_or_address.attendee_name
),
get_best_name,
_('John Doe'),
),
]
@@ -493,11 +510,7 @@ def base_placeholders(sender, **kwargs):
))
ph.append(SimpleFunctionalMailTextPlaceholder(
'name_%s' % f, ['position_or_address'],
lambda position_or_address, f=f: (
position_or_address.name_parts.get(f, '')
if isinstance(position_or_address, InvoiceAddress)
else position_or_address.attendee_name_parts.get(f, '')
),
lambda position_or_address, f=f: get_best_name(position_or_address, parts=True).get(f, ''),
name_scheme['sample'][f]
))

View File

@@ -45,7 +45,7 @@ def _send_mail(order: Order, subject: LazyI18nString, message: LazyI18nString, s
try:
ia = order.invoice_address
except InvoiceAddress.DoesNotExist:
ia = InvoiceAddress()
ia = InvoiceAddress(order=order)
email_context = get_email_context(event_or_subevent=subevent or order.event, refund_amount=refund_amount,
order=order, position_or_address=ia, event=order.event)

View File

@@ -23,7 +23,7 @@ def send_mails(event: Event, user: int, subject: dict, message: dict, orders: li
try:
ia = o.invoice_address
except InvoiceAddress.DoesNotExist:
ia = InvoiceAddress()
ia = InvoiceAddress(order=o)
if recipients in ('both', 'attendees'):
for p in o.positions.prefetch_related('addons'):