1 | """Exporters for student related stuff. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from zope.catalog.interfaces import ICatalog |
---|
5 | from zope.component import queryUtility |
---|
6 | from waeup.kofa.interfaces import ICSVExporter |
---|
7 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
8 | from waeup.kofa.students.interfaces import IStudent |
---|
9 | from waeup.kofa.utils.batching import ExporterBase |
---|
10 | from waeup.kofa.utils.helpers import iface_names |
---|
11 | |
---|
12 | class 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) |
---|