Allow to customize description of calendar files (#2415)

Co-authored-by: Martin Gross <gross@rami.io>
This commit is contained in:
Raphael Michel
2022-01-27 14:58:16 +01:00
committed by GitHub
parent c9530c56af
commit 2daf35c39e
10 changed files with 162 additions and 29 deletions

View File

@@ -28,11 +28,16 @@ from django.conf import settings
from django.utils.formats import date_format
from django.utils.translation import gettext as _
from pretix.base.email import get_email_context
from pretix.base.models import Event
from pretix.multidomain.urlreverse import build_absolute_uri
def get_ical(events):
def get_public_ical(events):
"""
Return an ical feed for a sequence of events or subevents. The calendar files will only include public
information.
"""
cal = vobject.iCalendar()
cal.add('prodid').value = '-//pretix//{}//'.format(settings.PRETIX_INSTANCE_NAME.replace(" ", "_"))
creation_time = datetime.datetime.now(pytz.utc)
@@ -83,3 +88,91 @@ def get_ical(events):
vevent.add('description').value = '\n'.join(descr)
return cal
def get_private_icals(event, positions):
"""
Return a list of ical objects based on a sequence of positions.
Unlike get_public_ical, this will
- Generate multiple ical files instead of one (but with deduplication applied)
- Respect the mail_attach_ical_description setting
It is private in the sense that mail_attach_ical_description may contain content not suited for
public display.
We however intentionally do not allow using placeholders based on the order and position
specifically. This is for two reasons:
- In reality, many people will add their invite to their calendar which is shared with a larger
team. People are probably not aware that they're sharing sensitive information such as their
secret ticket link with everyone they share their calendar with.
- It would be pretty hard to implement it in a way that doesn't require us to use distinct
settings fields for emails to customers and to attendees, which feels like an overcomplication.
"""
from pretix.base.services.mail import TolerantDict
tz = pytz.timezone(event.settings.timezone)
creation_time = datetime.datetime.now(pytz.utc)
calobjects = []
evs = set(p.subevent or event for p in positions)
for ev in evs:
if isinstance(ev, Event):
url = build_absolute_uri(event, 'presale:event.index')
else:
url = build_absolute_uri(event, 'presale:event.index', {
'subevent': ev.pk
})
if event.settings.mail_attach_ical_description:
ctx = get_email_context(event=event, event_or_subevent=ev)
description = str(event.settings.mail_attach_ical_description).format_map(TolerantDict(ctx))
else:
# Default description
descr = []
descr.append(_('Tickets: {url}').format(url=url))
if ev.date_admission:
descr.append(str(_('Admission: {datetime}')).format(
datetime=date_format(ev.date_admission.astimezone(tz), 'SHORT_DATETIME_FORMAT')
))
descr.append(_('Organizer: {organizer}').format(organizer=event.organizer.name))
description = '\n'.join(descr)
cal = vobject.iCalendar()
cal.add('prodid').value = '-//pretix//{}//'.format(settings.PRETIX_INSTANCE_NAME.replace(" ", "_"))
vevent = cal.add('vevent')
vevent.add('summary').value = str(ev.name)
vevent.add('description').value = description
vevent.add('dtstamp').value = creation_time
if ev.location:
vevent.add('location').value = str(ev.location)
vevent.add('uid').value = 'pretix-{}-{}-{}@{}'.format(
event.organizer.slug,
event.organizer.slug, event.slug,
ev.pk if not isinstance(ev, Event) else '0',
urlparse(url).netloc
)
if event.settings.show_times:
vevent.add('dtstart').value = ev.date_from.astimezone(tz)
else:
vevent.add('dtstart').value = ev.date_from.astimezone(tz).date()
if event.settings.show_date_to and ev.date_to:
if event.settings.show_times:
vevent.add('dtend').value = ev.date_to.astimezone(tz)
else:
# with full-day events date_to in pretix is included (e.g. last day)
# whereas dtend in vcalendar is non-inclusive => add one day for export
vevent.add('dtend').value = ev.date_to.astimezone(tz).date() + datetime.timedelta(days=1)
calobjects.append(cal)
return calobjects