1 | ## $Id: export.py 8393 2012-05-09 11:31:01Z uli $ |
---|
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, ICourseTicket, |
---|
27 | IStudentOnlinePayment) |
---|
28 | from waeup.kofa.utils.batching import ExporterBase |
---|
29 | from waeup.kofa.utils.helpers import iface_names |
---|
30 | |
---|
31 | class StudentsExporter(grok.GlobalUtility, ExporterBase): |
---|
32 | """Exporter for Students. |
---|
33 | """ |
---|
34 | grok.implements(ICSVExporter) |
---|
35 | grok.name('students') |
---|
36 | |
---|
37 | #: Fieldnames considered by this exporter |
---|
38 | fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo']))) |
---|
39 | |
---|
40 | #: The title under which this exporter will be displayed |
---|
41 | title = _(u'Students') |
---|
42 | |
---|
43 | def export(self, students, filepath=None): |
---|
44 | """Export `students`, an iterable, as CSV file. |
---|
45 | |
---|
46 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
47 | """ |
---|
48 | writer, outfile = self.get_csv_writer(filepath) |
---|
49 | for student in students: |
---|
50 | self.write_item(student, writer) |
---|
51 | return self.close_outfile(filepath, outfile) |
---|
52 | |
---|
53 | def export_all(self, site, filepath=None): |
---|
54 | """Export students into filepath as CSV data. |
---|
55 | |
---|
56 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
57 | """ |
---|
58 | catalog = queryUtility( |
---|
59 | ICatalog, context=site, name='students_catalog', default=None) |
---|
60 | if catalog is None: |
---|
61 | return self.export([], filepath) |
---|
62 | students = catalog.searchResults( |
---|
63 | student_id=(None, None)) |
---|
64 | return self.export(students, filepath) |
---|
65 | |
---|
66 | class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase): |
---|
67 | """Exporter for StudentStudyCourses. |
---|
68 | """ |
---|
69 | grok.implements(ICSVExporter) |
---|
70 | grok.name('studentstudycourses') |
---|
71 | |
---|
72 | #: Fieldnames considered by this exporter |
---|
73 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) |
---|
74 | |
---|
75 | #: The title under which this exporter will be displayed |
---|
76 | title = _(u'Student Study Courses') |
---|
77 | |
---|
78 | def mangle_value(self, value, name, context=None): |
---|
79 | """Add the hash symbol at the end of date_of_birth. |
---|
80 | |
---|
81 | This is to avoid annoying automatic date transformation |
---|
82 | by Excel or Calc. |
---|
83 | """ |
---|
84 | if name == 'certificate' and value is not None: |
---|
85 | # XXX: hopefully cert codes are unique site-wide |
---|
86 | value = value.code |
---|
87 | return super( |
---|
88 | StudentStudyCourseExporter, self).mangle_value( |
---|
89 | value, name, context=context) |
---|
90 | |
---|
91 | |
---|
92 | def export(self, study_courses, filepath=None): |
---|
93 | """Export `study_courses`, an iterable, as CSV file. |
---|
94 | |
---|
95 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
96 | """ |
---|
97 | writer, outfile = self.get_csv_writer(filepath) |
---|
98 | for study_course in study_courses: |
---|
99 | self.write_item(study_course, writer) |
---|
100 | return self.close_outfile(filepath, outfile) |
---|
101 | |
---|
102 | def export_all(self, site, filepath=None): |
---|
103 | """Export study courses into filepath as CSV data. |
---|
104 | |
---|
105 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
106 | """ |
---|
107 | catalog = queryUtility( |
---|
108 | ICatalog, context=site, name='students_catalog', default=None) |
---|
109 | if catalog is None: |
---|
110 | return self.export([], filepath) |
---|
111 | students = catalog.searchResults( |
---|
112 | student_id=(None, None)) |
---|
113 | study_courses = [x.get('studycourse', None) for x in students |
---|
114 | if x is not None] |
---|
115 | return self.export(study_courses, filepath) |
---|
116 | |
---|
117 | class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase): |
---|
118 | """Exporter for StudentStudyLevels. |
---|
119 | """ |
---|
120 | grok.implements(ICSVExporter) |
---|
121 | grok.name('studentstudylevels') |
---|
122 | |
---|
123 | #: Fieldnames considered by this exporter |
---|
124 | fields = tuple(sorted(iface_names(IStudentStudyLevel) + [ |
---|
125 | 'reg_number', 'matric_number', 'level'])) |
---|
126 | |
---|
127 | #: The title under which this exporter will be displayed |
---|
128 | title = _(u'Student Study Levels') |
---|
129 | |
---|
130 | def mangle_value(self, value, name, context=None): |
---|
131 | """Add the hash symbol at the end of date_of_birth. |
---|
132 | |
---|
133 | This is to avoid annoying automatic date transformation |
---|
134 | by Excel or Calc. |
---|
135 | """ |
---|
136 | |
---|
137 | if name == 'reg_number' and context is not None: |
---|
138 | value = getattr( |
---|
139 | getattr(getattr(context, '__parent__', None), |
---|
140 | '__parent__', None), 'reg_number', None) |
---|
141 | if name == 'matric_number' and context is not None: |
---|
142 | value = getattr( |
---|
143 | getattr(getattr(context, '__parent__', None), |
---|
144 | '__parent__', None), 'matric_number', None) |
---|
145 | return super( |
---|
146 | StudentStudyLevelExporter, self).mangle_value( |
---|
147 | value, name, context=context) |
---|
148 | |
---|
149 | def export(self, study_levels, filepath=None): |
---|
150 | """Export `study_levels`, an iterable, as CSV file. |
---|
151 | |
---|
152 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
153 | """ |
---|
154 | writer, outfile = self.get_csv_writer(filepath) |
---|
155 | for study_level in study_levels: |
---|
156 | self.write_item(study_level, writer) |
---|
157 | return self.close_outfile(filepath, outfile) |
---|
158 | |
---|
159 | def export_all(self, site, filepath=None): |
---|
160 | """Export study levels into filepath as CSV data. |
---|
161 | |
---|
162 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
163 | """ |
---|
164 | catalog = queryUtility( |
---|
165 | ICatalog, context=site, name='students_catalog', default=None) |
---|
166 | if catalog is None: |
---|
167 | return self.export([], filepath) |
---|
168 | students = catalog.searchResults( |
---|
169 | student_id=(None, None)) |
---|
170 | levels = [] |
---|
171 | study_courses = [x.get('studycourse', None) for x in students |
---|
172 | if x is not None] |
---|
173 | for course in study_courses: |
---|
174 | for level in course.values(): |
---|
175 | levels.append(level) |
---|
176 | return self.export(levels, filepath) |
---|
177 | |
---|
178 | class CourseTicketExporter(grok.GlobalUtility, ExporterBase): |
---|
179 | """Exporter for CourseTickets. |
---|
180 | """ |
---|
181 | grok.implements(ICSVExporter) |
---|
182 | grok.name('coursetickets') |
---|
183 | |
---|
184 | #: Fieldnames considered by this exporter |
---|
185 | fields = tuple(sorted(iface_names(ICourseTicket) + [ |
---|
186 | 'reg_number', 'matric_number', 'level', 'code', 'title', 'credits', |
---|
187 | 'passmark', 'semester', 'fcode', 'dcode'])) |
---|
188 | |
---|
189 | #: The title under which this exporter will be displayed |
---|
190 | title = _(u'Course Tickets') |
---|
191 | |
---|
192 | def mangle_value(self, value, name, context=None): |
---|
193 | """Treat location values special. |
---|
194 | """ |
---|
195 | if context is not None: |
---|
196 | student = context.getStudent() |
---|
197 | if name in ['reg_number', 'matric_number'] and student is not None: |
---|
198 | value = getattr(student, name, None) |
---|
199 | if name == 'level': |
---|
200 | value = getattr(context, 'getLevel', lambda: None)() |
---|
201 | return super( |
---|
202 | CourseTicketExporter, self).mangle_value( |
---|
203 | value, name, context=context) |
---|
204 | |
---|
205 | def export(self, course_tickets, filepath=None): |
---|
206 | """Export `course_tickets`, an iterable, as CSV file. |
---|
207 | |
---|
208 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
209 | """ |
---|
210 | writer, outfile = self.get_csv_writer(filepath) |
---|
211 | for ticket in course_tickets: |
---|
212 | self.write_item(ticket, writer) |
---|
213 | return self.close_outfile(filepath, outfile) |
---|
214 | |
---|
215 | def export_all(self, site, filepath=None): |
---|
216 | """Export course tickets into filepath as CSV data. |
---|
217 | |
---|
218 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
219 | """ |
---|
220 | catalog = queryUtility( |
---|
221 | ICatalog, context=site, name='students_catalog', default=None) |
---|
222 | if catalog is None: |
---|
223 | return self.export([], filepath) |
---|
224 | students = catalog.searchResults( |
---|
225 | student_id=(None, None)) |
---|
226 | levels = [] |
---|
227 | study_courses = [x.get('studycourse', None) for x in students |
---|
228 | if x is not None] |
---|
229 | for course in study_courses: |
---|
230 | for level in course.values(): |
---|
231 | levels.append(level) |
---|
232 | |
---|
233 | tickets = [] |
---|
234 | for level in levels: |
---|
235 | for ticket in level.values(): |
---|
236 | tickets.append(ticket) |
---|
237 | return self.export(tickets, filepath) |
---|
238 | |
---|
239 | class PaymentsExporter(grok.GlobalUtility, ExporterBase): |
---|
240 | """Exporter for OnlinePayment instances. |
---|
241 | """ |
---|
242 | grok.implements(ICSVExporter) |
---|
243 | grok.name('studentpayments') |
---|
244 | |
---|
245 | #: Fieldnames considered by this exporter |
---|
246 | fields = tuple( |
---|
247 | sorted(['reg_number', 'matric_number'] + |
---|
248 | iface_names(IStudentOnlinePayment, exclude_attribs=False))) |
---|
249 | |
---|
250 | #: The title under which this exporter will be displayed |
---|
251 | title = _(u'Course Tickets') |
---|
252 | |
---|
253 | def mangle_value(self, value, name, context=None): |
---|
254 | """Treat location values special. |
---|
255 | """ |
---|
256 | if context is not None: |
---|
257 | student = context.getStudent() |
---|
258 | if name in ['reg_number', 'matric_number'] and student is not None: |
---|
259 | value = getattr(student, name, None) |
---|
260 | return super( |
---|
261 | PaymentsExporter, self).mangle_value( |
---|
262 | value, name, context=context) |
---|
263 | |
---|
264 | def export(self, payments, filepath=None): |
---|
265 | """Export `payments`, an iterable, as CSV file. |
---|
266 | |
---|
267 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
268 | """ |
---|
269 | writer, outfile = self.get_csv_writer(filepath) |
---|
270 | for payment in payments: |
---|
271 | self.write_item(payment, writer) |
---|
272 | return self.close_outfile(filepath, outfile) |
---|
273 | |
---|
274 | def export_all(self, site, filepath=None): |
---|
275 | """Export payments into filepath as CSV data. |
---|
276 | |
---|
277 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
278 | """ |
---|
279 | catalog = queryUtility( |
---|
280 | ICatalog, context=site, name='students_catalog', default=None) |
---|
281 | if catalog is None: |
---|
282 | return self.export([], filepath) |
---|
283 | students = catalog.searchResults( |
---|
284 | student_id=(None, None)) |
---|
285 | payments = [] |
---|
286 | for student in students: |
---|
287 | if not 'payments' in student.keys(): |
---|
288 | continue |
---|
289 | for payment in student['payments'].values(): |
---|
290 | payments.append(payment) |
---|
291 | return self.export(payments, filepath) |
---|