# -*- coding: utf-8 -*-

"""Table export utility.

   Used to format course lists for download.
"""

import csv
import io

from flask import make_response, url_for, redirect, flash


def export_course_list(courses, format):
    if format == 'csv': return csv_export(courses)
    else:
        flash('Ungültiges Export-Format: {0}'.format(format), 'error')
        return redirect(url_for('lists'))


def csv_export(courses):
    buf = io.StringIO()
    out = csv.writer(buf, delimiter=";", dialect=csv.excel)

    # XXX: header -- not standardized
    out.writerow(['Kurs', 'Kursplatz', 'Bewerbernummer', 'Vorname', 'Nachname', 'Mail',
                  'Matrikelnummer', 'Telefon', 'Studienabschluss', 'Semester', 'Bewerberkreis'])

    def maybe(x):
        return x if x else ''

    for course in courses:
        active_no_debt = [attendance.applicant for attendance in course.attendances
                          if not attendance.waiting and (not attendance.has_to_pay or attendance.amountpaid > 0)]

        idx = 1
        for applicant in active_no_debt:
            out.writerow(['{0}'.format(course.full_name()),
                          '{0}'.format(idx),
                          '{0}'.format(applicant.id),
                          applicant.first_name,
                          applicant.last_name,
                          applicant.mail,
                          maybe(applicant.tag),
                          maybe(applicant.phone),
                          applicant.degree.name if applicant.degree else '',
                          '{0}'.format(maybe(applicant.semester)),
                          applicant.origin.name if applicant.origin else ''])
            idx += 1

    resp = make_response(buf.getvalue())
    resp.headers['Content-Disposition'] = 'attachment; filename="Kursliste.csv"'
    resp.mimetype = 'text/csv'

    return resp

@@ -128,40 +128,77 @@
 io%0D%0A
-%0D%0A
 from 
-flask import make_respons
+tempfile import NamedTemporaryFile%0D%0A%0D%0Afrom flask import send_fil
 e, u
@@ -291,16 +291,25 @@
 = 'csv':
+%0D%0A       
  return 
@@ -329,16 +329,82 @@
 urses)%0D%0A
+    elif format == 'xlsx':%0D%0A        return excel_export(courses)%0D%0A
     else
@@ -428,9 +428,10 @@
 'Ung
-%C3%BC
+ue
 ltig
@@ -2035,8 +2035,180 @@
 n resp%0D%0A
+%0D%0A%0D%0Adef excel_export(courses):%0D%0A    file = NamedTemporaryFile()%0D%0A    resp = send_file(file, as_attachment = True, attachment_filename = 'Kursliste.xlsx')%0D%0A    return resp%0D%0A

Add functionality to send temporary .xlsx files to the