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

Last change on this file since 7981 was 7973, checked in by Henrik Bettermann, 13 years ago

Add the hash symbol at the end of date_of_birth.

This is to avoid annoying automatic date transformation by Excel or Calc.

File size: 2.0 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 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)
Note: See TracBrowser for help on using the repository browser.