Exemple #1
0
def export_ical_task(self, event_id, temp=True):
    event = safe_query(Event, 'id', event_id, 'event_id')

    try:
        file_path = generate_ics_file(event_id, temp)

        filename = os.path.basename(file_path)
        ical_file = UploadedFile(file_path=file_path, filename=filename)
        if temp:
            ical_url = upload(
                ical_file, UPLOAD_PATHS['exports-temp']['ical'].format(event_id=event_id)
            )
        else:
            ical_url = upload(
                ical_file, UPLOAD_PATHS['exports']['ical'].format(event_id=event_id)
            )
        result = {'download_url': ical_url}
        if not temp:
            event.ical_url = ical_url
            save_to_db(event)

    except Exception as e:
        result = {'__error': True, 'result': str(e)}
        logger.exception('Error in ical download')

    return result
Exemple #2
0
def send_email_to_attendees(order):
    attachments = None
    if current_app.config['ATTACH_ORDER_PDF']:
        attachments = [order.ticket_pdf_path, order.invoice_pdf_path]

    event = order.event
    ical_file_path = generate_ics_file(event.id, include_sessions=False)

    if os.path.exists(ical_file_path):
        if attachments is None:
            attachments = [ical_file_path]
        else:
            attachments.append(ical_file_path)

    attendees = (
        TicketHolder.query.options(
            joinedload(TicketHolder.ticket), joinedload(TicketHolder.user)
        )
        .filter_by(order_id=order.id)
        .all()
    )
    email_group = groupby(attendees, lambda a: a.email)

    buyer_email = order.user.email
    event_date = convert_to_user_locale(
        buyer_email,
        date=order.event.starts_at,
    )
    event_end_date = convert_to_user_locale(
        buyer_email,
        date_time=order.event.ends_at,
        tz=order.event.timezone,
    )
    event_time = convert_to_user_locale(
        buyer_email,
        time=order.event.starts_at,
        tz=order.event.timezone,
    )

    context = dict(
        order=order,
        starts_at=convert_to_user_locale(
            buyer_email,
            date_time=order.event.starts_at,
            tz=order.event.timezone,
        ),
        ends_at=event_end_date,
        event_time=event_time,
        settings=get_settings(),
        order_view_url=order.site_view_link,
    )

    action = MailType.TICKET_PURCHASED
    mail = MAILS[action]
    send_email(
        to=buyer_email,
        action=action,
        subject=mail['subject'].format(
            event_name=event.name,
            invoice_id=order.invoice_number,
            event_date=event_date,
            event_time=event_time,
        ),
        html=render_template(mail['template'], attendees=attendees, **context),
        attachments=attachments,
    )

    action = MailType.TICKET_PURCHASED_ATTENDEE
    mail = MAILS[action]
    for email, attendees_group in email_group:
        if email == buyer_email:
            # Ticket holder is the purchaser
            continue

        # The Ticket holder is not the purchaser
        send_email(
            to=email,
            action=action,
            subject=mail['subject'].format(
                event_name=event.name,
                invoice_id=order.invoice_number,
                event_date=event_date,
                event_time=event_time,
            ),
            html=render_template(
                mail['template'],
                attendees=list(attendees_group),
                **context,
            ),
            attachments=attachments,
        )