"""Exporters for student related stuff. """ import grok from zope.catalog.interfaces import ICatalog from zope.component import queryUtility from waeup.kofa.interfaces import ICSVExporter from waeup.kofa.interfaces import MessageFactory as _ from waeup.kofa.students.interfaces import IStudent from waeup.kofa.utils.batching import ExporterBase from waeup.kofa.utils.helpers import iface_names class StudentsExporter(grok.GlobalUtility, ExporterBase): """Exporter for Applicants. """ grok.implements(ICSVExporter) grok.name('students') #: Fieldnames considered by this exporter fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo']))) #: The title under which this exporter will be displayed title = _(u'Students') def export(self, students, filepath=None): """Export `students`, an iterable, as CSV file. If `filepath` is ``None``, a raw string with CSV data is returned. """ writer, outfile = self.get_csv_writer(filepath) for student in students: self.write_item(student, writer) return self.close_outfile(filepath, outfile) def export_all(self, site, filepath=None): """Export students into filepath as CSV data. If `filepath` is ``None``, a raw string with CSV data is returned. """ catalog = queryUtility( ICatalog, context=site, name='students_catalog', default=None) if catalog is None: return self.export([], filepath) students = catalog.searchResults( student_id=(None, None)) return self.export(students, filepath)