source: main/waeup.kofa/trunk/src/waeup/kofa/students/export.py @ 7949

Last change on this file since 7949 was 7944, checked in by uli, 13 years ago

Add first student exporter draft.

File size: 1.6 KB
Line 
1"""Exporters for student related stuff.
2"""
3import grok
4from zope.catalog.interfaces import ICatalog
5from zope.component import queryUtility
6from waeup.kofa.interfaces import ICSVExporter
7from waeup.kofa.interfaces import MessageFactory as _
8from waeup.kofa.students.interfaces import IStudent
9from waeup.kofa.utils.batching import ExporterBase
10from waeup.kofa.utils.helpers import iface_names
11
12class StudentsExporter(grok.GlobalUtility, ExporterBase):
13    """Exporter for Applicants.
14    """
15    grok.implements(ICSVExporter)
16    grok.name('students')
17
18    #: Fieldnames considered by this exporter
19    fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo'])))
20
21    #: The title under which this exporter will be displayed
22    title = _(u'Students')
23
24    def export(self, students, filepath=None):
25        """Export `students`, an iterable, as CSV file.
26
27        If `filepath` is ``None``, a raw string with CSV data is returned.
28        """
29        writer, outfile = self.get_csv_writer(filepath)
30        for student in students:
31            self.write_item(student, writer)
32        return self.close_outfile(filepath, outfile)
33
34    def export_all(self, site, filepath=None):
35        """Export students into filepath as CSV data.
36
37        If `filepath` is ``None``, a raw string with CSV data is returned.
38        """
39        catalog = queryUtility(
40            ICatalog, context=site, name='students_catalog', default=None)
41        if catalog is None:
42            return self.export([], filepath)
43        students = catalog.searchResults(
44            student_id=(None, None))
45        return self.export(students, filepath)
Note: See TracBrowser for help on using the repository browser.