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 mangle_value(self, value, name, context=None): |
---|
25 | """Add the hash symbol at the end of date_of_birth. |
---|
26 | |
---|
27 | This is to avoid annoying automatic date transformation |
---|
28 | by Excel or Calc. |
---|
29 | """ |
---|
30 | if name == 'date_of_birth': |
---|
31 | value = str(value) + '#' |
---|
32 | return super( |
---|
33 | StudentsExporter, self).mangle_value( |
---|
34 | value, name, context=context) |
---|
35 | |
---|
36 | def export(self, students, filepath=None): |
---|
37 | """Export `students`, an iterable, as CSV file. |
---|
38 | |
---|
39 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
40 | """ |
---|
41 | writer, outfile = self.get_csv_writer(filepath) |
---|
42 | for student in students: |
---|
43 | self.write_item(student, writer) |
---|
44 | return self.close_outfile(filepath, outfile) |
---|
45 | |
---|
46 | def export_all(self, site, filepath=None): |
---|
47 | """Export students into filepath as CSV data. |
---|
48 | |
---|
49 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
50 | """ |
---|
51 | catalog = queryUtility( |
---|
52 | ICatalog, context=site, name='students_catalog', default=None) |
---|
53 | if catalog is None: |
---|
54 | return self.export([], filepath) |
---|
55 | students = catalog.searchResults( |
---|
56 | student_id=(None, None)) |
---|
57 | return self.export(students, filepath) |
---|