Extending the order import process
It’s possible through the backend to import orders into eventyay, for example from a legacy ticketing system. If your plugins defines additional data structures around orders, it might be useful to make it possible to import them as well.
Import process
Here’s a short description of eventyay’ import process to show you where the system will need to interact with your plugin. You can find more detailed descriptions of the attributes and methods further below.
The user uploads a CSV file. The system tries to parse the CSV file and understand its column headers.
A preview of the file is shown to the user and the user is asked to assign the various different input parameters to columns of the file or static values. For example, the user either needs to manually select a product or specify a column that contains a product. For this purpose, a select field is rendered for every possible input column, allowing the user to choose between a default/empty value (defined by your
default_value/default_label) attributes, the columns of the uploaded file, or a static value (defined by yourstatic_choicesmethod).The user submits its assignment and the system uses the
resolvemethod of all columns to get the raw value for all columns.The system uses the
cleanmethod of all columns to verify that all input fields are valid and transformed to the correct data type.The system prepares internal model objects (
Orderetc) and uses theassignmethod of all columns to assign these objects with actual values.The system saves all of these model objects to the database in a database transaction. Plugins can create additional objects in this stage through their
savemethod.
Column registration
The import API does not make a lot of usage from signals, however, it
does use a signal to get a list of all available import columns. Your plugin
should listen for this signal and return the subclass of eventyay.base.orderimport.ImportColumn
that we’ll provide in this plugin:
1from django.dispatch import receiver
2
3from eventyay.base.signals import order_import_columns
4
5
6@receiver(order_import_columns, dispatch_uid="custom_columns")
7def register_column(sender, **kwargs):
8 return [
9 EmailColumn(sender),
10 ]
The column class API
- class eventyay.base.orderimport.ImportColumn
The central object of each import extension is the subclass of
ImportColumn.- ImportColumn.event
The default constructor sets this property to the event we are currently working for.
- identifier
Unique, internal name of the column.
This is an abstract attribute, you must override this!
- verbose_name
Human-readable description of the column.
This is an abstract attribute, you must override this!
- default_value
Internal default value for the assignment of this column. Defaults to
empty. ReturnNoneto disable this option.
- default_label
Human-readable description of the default assignment of this column, defaults to “Keep empty”.
- initial
Initial value for the form component.
- static_choices()
This will be called when rendering the form component and allows you to return a list of values that can be selected by the user statically during import.
- Returns:
list of 2-tuples of strings
- resolve(settings, record)
This method will be called to get the raw value for this field, usually by either using a static value or inspecting the CSV file for the assigned header.
- clean(value, previous_values)
Allows you to validate the raw input value for your column. Raise
ValidationErrorif the value is invalid.- Parameters:
value – The raw value of your column as returned by
resolve.previous_values – Dictionary containing the validated values of all columns that have already been validated.
- assign(value, order, position, invoice_address, **kwargs)
This will be called to perform the actual import. Set attributes on the
order,position, orinvoice_addressobjects based on the inputvalue.
- save(order)
This will be called to perform the actual import inside the database transaction. The input object
orderhas already been saved to the database.
Example
For example, the import column responsible for assigning email addresses looks like this:
1class EmailColumn(ImportColumn):
2 identifier = 'email'
3 verbose_name = _('E-mail address')
4
5 def clean(self, value, previous_values):
6 if value:
7 EmailValidator()(value)
8 return value
9
10 def assign(self, value, order, position, invoice_address, **kwargs):
11 order.email = value