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