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

Last change on this file since 8016 was 8015, checked in by uli, 12 years ago

Exporters for study levels.

File size: 6.2 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 (
9    IStudent, IStudentStudyCourse, IStudentStudyLevel,)
10from waeup.kofa.utils.batching import ExporterBase
11from waeup.kofa.utils.helpers import iface_names
12
13class StudentsExporter(grok.GlobalUtility, ExporterBase):
14    """Exporter for Applicants.
15    """
16    grok.implements(ICSVExporter)
17    grok.name('students')
18
19    #: Fieldnames considered by this exporter
20    fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo'])))
21
22    #: The title under which this exporter will be displayed
23    title = _(u'Students')
24
25    def mangle_value(self, value, name, context=None):
26        """Add the hash symbol at the end of date_of_birth.
27
28        This is to avoid annoying automatic date transformation
29        by Excel or Calc.
30        """
31        if name == 'date_of_birth':
32            value = str(value) + '#'
33        return super(
34            StudentsExporter, self).mangle_value(
35            value, name, context=context)
36
37    def export(self, students, filepath=None):
38        """Export `students`, an iterable, as CSV file.
39
40        If `filepath` is ``None``, a raw string with CSV data is returned.
41        """
42        writer, outfile = self.get_csv_writer(filepath)
43        for student in students:
44            self.write_item(student, writer)
45        return self.close_outfile(filepath, outfile)
46
47    def export_all(self, site, filepath=None):
48        """Export students into filepath as CSV data.
49
50        If `filepath` is ``None``, a raw string with CSV data is returned.
51        """
52        catalog = queryUtility(
53            ICatalog, context=site, name='students_catalog', default=None)
54        if catalog is None:
55            return self.export([], filepath)
56        students = catalog.searchResults(
57            student_id=(None, None))
58        return self.export(students, filepath)
59
60class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase):
61    """Exporter for StudentStudyCourses.
62    """
63    grok.implements(ICSVExporter)
64    grok.name('studentstudycourses')
65
66    #: Fieldnames considered by this exporter
67    fields = tuple(sorted(iface_names(IStudentStudyCourse)))
68
69    #: The title under which this exporter will be displayed
70    title = _(u'Student Study Courses')
71
72    def mangle_value(self, value, name, context=None):
73        """Add the hash symbol at the end of date_of_birth.
74
75        This is to avoid annoying automatic date transformation
76        by Excel or Calc.
77        """
78        if name == 'certificate' and value is not None:
79            # XXX: hopefully cert codes are unique site-wide
80            value = value.code
81        return super(
82            StudentStudyCourseExporter, self).mangle_value(
83            value, name, context=context)
84
85
86    def export(self, study_courses, filepath=None):
87        """Export `study_courses`, an iterable, as CSV file.
88
89        If `filepath` is ``None``, a raw string with CSV data is returned.
90        """
91        writer, outfile = self.get_csv_writer(filepath)
92        for study_course in study_courses:
93            self.write_item(study_course, writer)
94        return self.close_outfile(filepath, outfile)
95
96    def export_all(self, site, filepath=None):
97        """Export study courses into filepath as CSV data.
98
99        If `filepath` is ``None``, a raw string with CSV data is returned.
100        """
101        catalog = queryUtility(
102            ICatalog, context=site, name='students_catalog', default=None)
103        if catalog is None:
104            return self.export([], filepath)
105        students = catalog.searchResults(
106            student_id=(None, None))
107        study_courses = [x.get('studycourse', None) for x in students
108                         if x is not None]
109        return self.export(study_courses, filepath)
110
111class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase):
112    """Exporter for StudentStudyLevels.
113    """
114    grok.implements(ICSVExporter)
115    grok.name('studentstudylevels')
116
117    #: Fieldnames considered by this exporter
118    fields = tuple(sorted(iface_names(IStudentStudyLevel) + [
119        'reg_number', 'matric_number', 'level']))
120
121    #: The title under which this exporter will be displayed
122    title = _(u'Student Study Levels')
123
124    def mangle_value(self, value, name, context=None):
125        """Add the hash symbol at the end of date_of_birth.
126
127        This is to avoid annoying automatic date transformation
128        by Excel or Calc.
129        """
130
131        if name == 'reg_number' and context is not None:
132            value = getattr(
133                getattr(getattr(context, '__parent__', None),
134                        '__parent__', None), 'reg_number', None)
135        if name == 'matric_number' and context is not None:
136            value = getattr(
137                getattr(getattr(context, '__parent__', None),
138                        '__parent__', None), 'matric_number', None)
139        return super(
140            StudentStudyLevelExporter, self).mangle_value(
141            value, name, context=context)
142
143    def export(self, study_levels, filepath=None):
144        """Export `study_levels`, an iterable, as CSV file.
145
146        If `filepath` is ``None``, a raw string with CSV data is returned.
147        """
148        writer, outfile = self.get_csv_writer(filepath)
149        for study_level in study_levels:
150            self.write_item(study_level, writer)
151        return self.close_outfile(filepath, outfile)
152
153    def export_all(self, site, filepath=None):
154        """Export study levels into filepath as CSV data.
155
156        If `filepath` is ``None``, a raw string with CSV data is returned.
157        """
158        catalog = queryUtility(
159            ICatalog, context=site, name='students_catalog', default=None)
160        if catalog is None:
161            return self.export([], filepath)
162        students = catalog.searchResults(
163            student_id=(None, None))
164        levels = []
165        study_courses = [x.get('studycourse', None) for x in students
166                         if x is not None]
167        for course in study_courses:
168            for level in course.values():
169                levels.append(level)
170        return self.export(levels, filepath)
Note: See TracBrowser for help on using the repository browser.