Writing a ticket output plugin

A ticket output is a method to offer a ticket (an order) for the user to download.

In this document, we will walk through the creation of a ticket output plugin. This is very similar to creating an export output.

Please read Creating a plugin first, if you haven’t already.

Output registration

The ticket output API does not make a lot of usage from signals, however, it does use a signal to get a list of all available ticket outputs. Your plugin should listen for this signal and return the subclass of pretix.base.ticketoutput.BaseTicketOutput that we’ll provide in this plugin:

1
2
3
4
5
6
7
8
9
from django.dispatch import receiver

from pretix.base.signals import register_ticket_outputs


@receiver(register_ticket_outputs, dispatch_uid="output_pdf")
def register_ticket_output(sender, **kwargs):
    from .ticketoutput import PdfTicketOutput
    return PdfTicketOutput

The output class

class pretix.base.ticketoutput.BaseTicketOutput

The central object of each ticket output is the subclass of BaseTicketOutput.

BaseTicketOutput.event

The default constructor sets this property to the event we are currently working for.

BaseTicketOutput.settings

The default constructor sets this property to a SettingsSandbox object. You can use this object to store settings using its get and set methods. All settings you store are transparently prefixed, so you get your very own settings namespace.

BaseTicketOutput.identifier

A short and unique identifier for this ticket output. This should only contain lowercase letters and in most cases will be the same as your package name.

This is an abstract attribute, you must override this!

BaseTicketOutput.verbose_name

A human-readable name for this ticket output. This should be short but self-explanatory. Good examples include ‘PDF tickets’ and ‘Passbook’.

This is an abstract attribute, you must override this!

BaseTicketOutput.is_enabled

Returns whether or whether not this output is enabled. By default, this is determined by the value of the _enabled setting.

BaseTicketOutput.multi_download_enabled

Returns whether or not the generate_order method may be called. Returns True by default.

BaseTicketOutput.settings_form_fields

When the event’s administrator visits the event configuration page, this method is called to return the configuration fields available.

It should therefore return a dictionary where the keys should be (unprefixed) settings keys and the values should be corresponding Django form fields.

The default implementation returns the appropriate fields for the _enabled setting mentioned above.

We suggest that you return an OrderedDict object instead of a dictionary and make use of the default implementation. Your implementation could look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
@property
def settings_form_fields(self):
    return OrderedDict(
        list(super().settings_form_fields.items()) + [
            ('paper_size',
             forms.CharField(
                 label=_('Paper size'),
                 required=False
             ))
        ]
    )

Warning

It is highly discouraged to alter the _enabled field of the default implementation.

BaseTicketOutput.settings_content_render(request: django.http.request.HttpRequest) → str

When the event’s administrator visits the event configuration page, this method is called. It may return HTML containing additional information that is displayed below the form fields configured in settings_form_fields.

BaseTicketOutput.generate(position: pretix.base.models.orders.OrderPosition) → typing.Tuple[[str, str], str]

This method should generate the download file and return a tuple consisting of a filename, a file type and file content. The extension will be taken from the filename which is otherwise ignored.

Note

If the event uses the event series feature (internally called subevents) and your generated ticket contains information like the event name or date, you probably want to display the properties of the subevent. A common pattern to do this would be a declaration ev = position.subevent or position.order.event and then access properties that are present on both classes like ev.name or ev.date_from.

BaseTicketOutput.generate_order(order: pretix.base.models.orders.Order) → typing.Tuple[[str, str], str]

This method is the same as order() but should not generate one file per order position but instead one file for the full order.

This method is optional to implement. If you don’t implement it, the default implementation will offer a zip file of the generate() results for the order positions.

This method should generate a download file and return a tuple consisting of a filename, a file type and file content. The extension will be taken from the filename which is otherwise ignored.

If you override this method, make sure that positions that are addons (i.e. addon_to is set) are only outputted if the event setting ticket_download_addons is active. Do the same for positions that are non-admission without ticket_download_nonadm active.

BaseTicketOutput.download_button_text

The text on the download button in the frontend.