1 | ## $Id: export.py 8160 2012-04-15 06:40:01Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """Exporters for student related stuff. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from zope.catalog.interfaces import ICatalog |
---|
22 | from zope.component import queryUtility |
---|
23 | from waeup.kofa.interfaces import ICSVExporter |
---|
24 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
25 | from waeup.kofa.students.interfaces import ( |
---|
26 | IStudent, IStudentStudyCourse, IStudentStudyLevel,) |
---|
27 | from waeup.kofa.utils.batching import ExporterBase |
---|
28 | from waeup.kofa.utils.helpers import iface_names |
---|
29 | |
---|
30 | class StudentsExporter(grok.GlobalUtility, ExporterBase): |
---|
31 | """Exporter for Students. |
---|
32 | """ |
---|
33 | grok.implements(ICSVExporter) |
---|
34 | grok.name('students') |
---|
35 | |
---|
36 | #: Fieldnames considered by this exporter |
---|
37 | fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo']))) |
---|
38 | |
---|
39 | #: The title under which this exporter will be displayed |
---|
40 | title = _(u'Students') |
---|
41 | |
---|
42 | def mangle_value(self, value, name, context=None): |
---|
43 | """Add the hash symbol at the end of date_of_birth. |
---|
44 | |
---|
45 | This is to avoid annoying automatic date transformation |
---|
46 | by Excel or Calc. |
---|
47 | """ |
---|
48 | if name == 'date_of_birth': |
---|
49 | value = str(value) + '#' |
---|
50 | return super( |
---|
51 | StudentsExporter, self).mangle_value( |
---|
52 | value, name, context=context) |
---|
53 | |
---|
54 | def export(self, students, filepath=None): |
---|
55 | """Export `students`, an iterable, as CSV file. |
---|
56 | |
---|
57 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
58 | """ |
---|
59 | writer, outfile = self.get_csv_writer(filepath) |
---|
60 | for student in students: |
---|
61 | self.write_item(student, writer) |
---|
62 | return self.close_outfile(filepath, outfile) |
---|
63 | |
---|
64 | def export_all(self, site, filepath=None): |
---|
65 | """Export students into filepath as CSV data. |
---|
66 | |
---|
67 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
68 | """ |
---|
69 | catalog = queryUtility( |
---|
70 | ICatalog, context=site, name='students_catalog', default=None) |
---|
71 | if catalog is None: |
---|
72 | return self.export([], filepath) |
---|
73 | students = catalog.searchResults( |
---|
74 | student_id=(None, None)) |
---|
75 | return self.export(students, filepath) |
---|
76 | |
---|
77 | class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase): |
---|
78 | """Exporter for StudentStudyCourses. |
---|
79 | """ |
---|
80 | grok.implements(ICSVExporter) |
---|
81 | grok.name('studentstudycourses') |
---|
82 | |
---|
83 | #: Fieldnames considered by this exporter |
---|
84 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) |
---|
85 | |
---|
86 | #: The title under which this exporter will be displayed |
---|
87 | title = _(u'Student Study Courses') |
---|
88 | |
---|
89 | def mangle_value(self, value, name, context=None): |
---|
90 | """Add the hash symbol at the end of date_of_birth. |
---|
91 | |
---|
92 | This is to avoid annoying automatic date transformation |
---|
93 | by Excel or Calc. |
---|
94 | """ |
---|
95 | if name == 'certificate' and value is not None: |
---|
96 | # XXX: hopefully cert codes are unique site-wide |
---|
97 | value = value.code |
---|
98 | return super( |
---|
99 | StudentStudyCourseExporter, self).mangle_value( |
---|
100 | value, name, context=context) |
---|
101 | |
---|
102 | |
---|
103 | def export(self, study_courses, filepath=None): |
---|
104 | """Export `study_courses`, an iterable, as CSV file. |
---|
105 | |
---|
106 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
107 | """ |
---|
108 | writer, outfile = self.get_csv_writer(filepath) |
---|
109 | for study_course in study_courses: |
---|
110 | self.write_item(study_course, writer) |
---|
111 | return self.close_outfile(filepath, outfile) |
---|
112 | |
---|
113 | def export_all(self, site, filepath=None): |
---|
114 | """Export study courses into filepath as CSV data. |
---|
115 | |
---|
116 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
117 | """ |
---|
118 | catalog = queryUtility( |
---|
119 | ICatalog, context=site, name='students_catalog', default=None) |
---|
120 | if catalog is None: |
---|
121 | return self.export([], filepath) |
---|
122 | students = catalog.searchResults( |
---|
123 | student_id=(None, None)) |
---|
124 | study_courses = [x.get('studycourse', None) for x in students |
---|
125 | if x is not None] |
---|
126 | return self.export(study_courses, filepath) |
---|
127 | |
---|
128 | class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase): |
---|
129 | """Exporter for StudentStudyLevels. |
---|
130 | """ |
---|
131 | grok.implements(ICSVExporter) |
---|
132 | grok.name('studentstudylevels') |
---|
133 | |
---|
134 | #: Fieldnames considered by this exporter |
---|
135 | fields = tuple(sorted(iface_names(IStudentStudyLevel) + [ |
---|
136 | 'reg_number', 'matric_number', 'level'])) |
---|
137 | |
---|
138 | #: The title under which this exporter will be displayed |
---|
139 | title = _(u'Student Study Levels') |
---|
140 | |
---|
141 | def mangle_value(self, value, name, context=None): |
---|
142 | """Add the hash symbol at the end of date_of_birth. |
---|
143 | |
---|
144 | This is to avoid annoying automatic date transformation |
---|
145 | by Excel or Calc. |
---|
146 | """ |
---|
147 | |
---|
148 | if name == 'reg_number' and context is not None: |
---|
149 | value = getattr( |
---|
150 | getattr(getattr(context, '__parent__', None), |
---|
151 | '__parent__', None), 'reg_number', None) |
---|
152 | if name == 'matric_number' and context is not None: |
---|
153 | value = getattr( |
---|
154 | getattr(getattr(context, '__parent__', None), |
---|
155 | '__parent__', None), 'matric_number', None) |
---|
156 | return super( |
---|
157 | StudentStudyLevelExporter, self).mangle_value( |
---|
158 | value, name, context=context) |
---|
159 | |
---|
160 | def export(self, study_levels, filepath=None): |
---|
161 | """Export `study_levels`, an iterable, as CSV file. |
---|
162 | |
---|
163 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
164 | """ |
---|
165 | writer, outfile = self.get_csv_writer(filepath) |
---|
166 | for study_level in study_levels: |
---|
167 | self.write_item(study_level, writer) |
---|
168 | return self.close_outfile(filepath, outfile) |
---|
169 | |
---|
170 | def export_all(self, site, filepath=None): |
---|
171 | """Export study levels into filepath as CSV data. |
---|
172 | |
---|
173 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
174 | """ |
---|
175 | catalog = queryUtility( |
---|
176 | ICatalog, context=site, name='students_catalog', default=None) |
---|
177 | if catalog is None: |
---|
178 | return self.export([], filepath) |
---|
179 | students = catalog.searchResults( |
---|
180 | student_id=(None, None)) |
---|
181 | levels = [] |
---|
182 | study_courses = [x.get('studycourse', None) for x in students |
---|
183 | if x is not None] |
---|
184 | for course in study_courses: |
---|
185 | for level in course.values(): |
---|
186 | levels.append(level) |
---|
187 | return self.export(levels, filepath) |
---|