Open Event Server – Export Speakers as CSV File
FOSSASIA‘s Open Event Server is the REST API backend for the event management platform, Open Event. Here, the event organizers can create their events, add tickets for it and manage all aspects from the schedule to the speakers. Also, once he/she makes his event public, others can view it and buy tickets if interested.
The organizer can see all the speakers in a very detailed view in the event management dashboard. He can see the statuses of all the speakers. The possible statuses are pending, accepted and rejected. He/she can take actions such as editing/viewing speakers.
If the organizer wants to download the list of all the speakers as a CSV file, he or she can do it very easily by simply clicking on the Export As CSV button in the top right-hand corner.
Let us see how this is done on the server.
Server side – generating the Speakers CSV file
Here we will be using the csv package provided by python for writing the csv file.
import csv
- We define a method export_speakers_csv which takes the speakers to be exported as a CSV file as the argument.
- Next, we define the headers of the CSV file. It is the first row of the CSV file.
def export_speakers_csv(speakers): headers = ['Speaker Name', 'Speaker Email', 'Speaker Session(s)', 'Speaker Mobile', 'Speaker Bio', 'Speaker Organisation', 'Speaker Position']
- A list is defined called rows. This contains the rows of the CSV file. As mentioned earlier, headers is the first row.
rows = [headers]
- We iterate over each speaker in speakers and form a row for that speaker by separating the values of each of the columns by a comma. Here, every row is one speaker.
- As a speaker can contain multiple sessions we iterate over each session for that particular speaker and append each session to a string. ‘;’ is used as a delimiter. This string is then added to the row. We also include the state of the session – accepted, rejected, confirmed.
- The newly formed row is added to the rows list.
for speaker in speakers: column = [speaker.name if speaker.name else '', speaker.email if speaker.email else ''] if speaker.sessions: session_details = '' for session in speaker.sessions: if not session.deleted_at: session_details += session.title + ' (' + session.state + '); ' column.append(session_details[:-2]) else: column.append('') column.append(speaker.mobile if speaker.mobile else '') column.append(speaker.short_biography if speaker.short_biography else '') column.append(speaker.organisation if speaker.organisation else '') column.append(speaker.position if speaker.position else '') rows.append(column)
- rows contains the contents of the CSV file and hence it is returned.
return rows
- We iterate over each item of rows and write it to the CSV file using the methods provided by the csv package.
with open(file_path, "w") as temp_file: writer = csv.writer(temp_file) from app.api.helpers.csv_jobs_util import export_speakers_csv content = export_speakers_csv(speakers) for row in content: writer.writerow(row)
Obtaining the Speakers CSV file:
Firstly, we have an API endpoint which starts the task on the server.
GET - /v1/events/{event_identifier}/export/speakers/csv
Here, event_identifier is the unique ID of the event. This endpoint starts a celery task on the server to export the speakers of the event as a CSV file. It returns the URL of the task to get the status of the export task. A sample response is as follows:
{ "task_url": "/v1/tasks/b7ca7088-876e-4c29-a0ee-b8029a64849a" }
The user can go to the above-returned URL and check the status of his/her Celery task. If the task completed successfully he/she will get the download URL. The endpoint to check the status of the task is:
and the corresponding response from the server –
{ "result": { "download_url": "/v1/events/1/exports/http://localhost/static/media/exports/1/zip/OGpMM0w2RH/event1.zip" }, "state": "SUCCESS" }
The file can be downloaded from the above-mentioned URL.
You must be logged in to post a comment.