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

Last change on this file since 7997 was 7994, checked in by uli, 13 years ago

Add study course exporter.

File size: 3.9 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, IStudentStudyCourse
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)
58
59class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase):
60    """Exporter for StudentStudyCourses.
61    """
62    grok.implements(ICSVExporter)
63    grok.name('studentstudycourses')
64
65    #: Fieldnames considered by this exporter
66    fields = tuple(sorted(iface_names(IStudentStudyCourse)))
67
68    #: The title under which this exporter will be displayed
69    title = _(u'Student Study Courses')
70
71    def mangle_value(self, value, name, context=None):
72        """Add the hash symbol at the end of date_of_birth.
73
74        This is to avoid annoying automatic date transformation
75        by Excel or Calc.
76        """
77        if name == 'certificate' and value is not None:
78            # XXX: hopefully cert codes are unique site-wide
79            value = value.code
80        return super(
81            StudentStudyCourseExporter, self).mangle_value(
82            value, name, context=context)
83
84
85    def export(self, study_courses, filepath=None):
86        """Export `study_courses`, an iterable, as CSV file.
87
88        If `filepath` is ``None``, a raw string with CSV data is returned.
89        """
90        writer, outfile = self.get_csv_writer(filepath)
91        for study_course in study_courses:
92            self.write_item(study_course, writer)
93        return self.close_outfile(filepath, outfile)
94
95    def export_all(self, site, filepath=None):
96        """Export study courses into filepath as CSV data.
97
98        If `filepath` is ``None``, a raw string with CSV data is returned.
99        """
100        catalog = queryUtility(
101            ICatalog, context=site, name='students_catalog', default=None)
102        if catalog is None:
103            return self.export([], filepath)
104        students = catalog.searchResults(
105            student_id=(None, None))
106        study_courses = [x.get('studycourse', None) for x in students
107                         if x is not None]
108        return self.export(study_courses, filepath)
Note: See TracBrowser for help on using the repository browser.