1 | ## $Id: export.py 9427 2012-10-26 17:57:26Z 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 MessageFactory as _ |
---|
24 | from waeup.kofa.students.interfaces import ( |
---|
25 | IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket, |
---|
26 | IStudentOnlinePayment, ICSVStudentExporter, IBedTicket) |
---|
27 | from waeup.kofa.utils.batching import ExporterBase |
---|
28 | from waeup.kofa.utils.helpers import iface_names |
---|
29 | |
---|
30 | #: A tuple containing all exporter names referring to students or |
---|
31 | #: subobjects thereof. |
---|
32 | EXPORTER_NAMES = ('students', 'studentstudycourses', 'studentstudylevels', |
---|
33 | 'coursetickets', 'studentpayments') |
---|
34 | |
---|
35 | def get_students(site): |
---|
36 | """Get all students registered in catalog in `site`. |
---|
37 | """ |
---|
38 | catalog = queryUtility( |
---|
39 | ICatalog, context=site, name='students_catalog', default=None) |
---|
40 | if catalog is None: |
---|
41 | return [] |
---|
42 | students = catalog.searchResults(student_id=(None, None)) |
---|
43 | return students |
---|
44 | |
---|
45 | def get_studycourses(students): |
---|
46 | """Get studycourses of `students`. |
---|
47 | """ |
---|
48 | return [x.get('studycourse', None) for x in students |
---|
49 | if x is not None] |
---|
50 | |
---|
51 | def get_levels(students): |
---|
52 | """Get all studylevels of `students`. |
---|
53 | """ |
---|
54 | levels = [] |
---|
55 | for course in get_studycourses(students): |
---|
56 | for level in course.values(): |
---|
57 | levels.append(level) |
---|
58 | return levels |
---|
59 | |
---|
60 | def get_tickets(students): |
---|
61 | """Get all course tickets of `students`. |
---|
62 | """ |
---|
63 | tickets = [] |
---|
64 | for level in get_levels(students): |
---|
65 | for ticket in level.values(): |
---|
66 | tickets.append(ticket) |
---|
67 | return tickets |
---|
68 | |
---|
69 | def get_payments(students): |
---|
70 | """Get all payments of `students`. |
---|
71 | """ |
---|
72 | payments = [] |
---|
73 | for student in students: |
---|
74 | for payment in student.get('payments', {}).values(): |
---|
75 | payments.append(payment) |
---|
76 | return payments |
---|
77 | |
---|
78 | def get_bedtickets(students): |
---|
79 | """Get all bedtickets of `students`. |
---|
80 | """ |
---|
81 | tickets = [] |
---|
82 | for student in students: |
---|
83 | for ticket in student.get('accommodation', {}).values(): |
---|
84 | tickets.append(ticket) |
---|
85 | return tickets |
---|
86 | |
---|
87 | class StudentExporterBase(ExporterBase): |
---|
88 | """Exporter for students or related objects. |
---|
89 | |
---|
90 | This is a baseclass. |
---|
91 | """ |
---|
92 | grok.baseclass() |
---|
93 | grok.implements(ICSVStudentExporter) |
---|
94 | grok.provides(ICSVStudentExporter) |
---|
95 | |
---|
96 | def export(self, values, filepath=None): |
---|
97 | """Export `values`, an iterable, as CSV file. |
---|
98 | |
---|
99 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
100 | """ |
---|
101 | writer, outfile = self.get_csv_writer(filepath) |
---|
102 | for value in values: |
---|
103 | self.write_item(value, writer) |
---|
104 | return self.close_outfile(filepath, outfile) |
---|
105 | |
---|
106 | |
---|
107 | class StudentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
108 | """Exporter for Students. |
---|
109 | """ |
---|
110 | grok.name('students') |
---|
111 | |
---|
112 | #: Fieldnames considered by this exporter |
---|
113 | fields = tuple(sorted(iface_names( |
---|
114 | IStudent, omit=['loggerInfo']))) + ( |
---|
115 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
116 | 'current_level', 'current_session') |
---|
117 | |
---|
118 | #: The title under which this exporter will be displayed |
---|
119 | title = _(u'Students') |
---|
120 | |
---|
121 | def mangle_value(self, value, name, context=None): |
---|
122 | if name == 'history': |
---|
123 | value = value.messages |
---|
124 | if name == 'phone' and value is not None: |
---|
125 | # Append hash '#' to phone numbers to circumvent |
---|
126 | # unwanted excel automatic |
---|
127 | value = str('%s#' % value) |
---|
128 | return super( |
---|
129 | StudentsExporter, self).mangle_value( |
---|
130 | value, name, context=context) |
---|
131 | |
---|
132 | def export_all(self, site, filepath=None): |
---|
133 | """Export students into filepath as CSV data. |
---|
134 | |
---|
135 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
136 | """ |
---|
137 | return self.export(get_students(site), filepath) |
---|
138 | |
---|
139 | def export_student(self, student, filepath=None): |
---|
140 | return self.export([student], filepath=filepath) |
---|
141 | |
---|
142 | |
---|
143 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
144 | """Exporter for StudentStudyCourses. |
---|
145 | """ |
---|
146 | grok.name('studentstudycourses') |
---|
147 | |
---|
148 | #: Fieldnames considered by this exporter |
---|
149 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
150 | |
---|
151 | #: The title under which this exporter will be displayed |
---|
152 | title = _(u'Student Study Courses') |
---|
153 | |
---|
154 | def mangle_value(self, value, name, context=None): |
---|
155 | """Treat location values special. |
---|
156 | """ |
---|
157 | if name == 'certificate' and value is not None: |
---|
158 | # XXX: hopefully cert codes are unique site-wide |
---|
159 | value = value.code |
---|
160 | if name == 'student_id' and context is not None: |
---|
161 | student = context.student |
---|
162 | value = getattr(student, name, None) |
---|
163 | return super( |
---|
164 | StudentStudyCourseExporter, self).mangle_value( |
---|
165 | value, name, context=context) |
---|
166 | |
---|
167 | def export_all(self, site, filepath=None): |
---|
168 | """Export study courses into filepath as CSV data. |
---|
169 | |
---|
170 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
171 | """ |
---|
172 | return self.export(get_studycourses(get_students(site)), filepath) |
---|
173 | |
---|
174 | def export_student(self, student, filepath=None): |
---|
175 | """Export studycourse of a single student object. |
---|
176 | """ |
---|
177 | return self.export(get_studycourses([student]), filepath) |
---|
178 | |
---|
179 | |
---|
180 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
181 | """Exporter for StudentStudyLevels. |
---|
182 | """ |
---|
183 | grok.name('studentstudylevels') |
---|
184 | |
---|
185 | #: Fieldnames considered by this exporter |
---|
186 | fields = tuple(sorted(iface_names( |
---|
187 | IStudentStudyLevel) + ['level'])) + ( |
---|
188 | 'student_id', 'number_of_tickets','certcode') |
---|
189 | |
---|
190 | #: The title under which this exporter will be displayed |
---|
191 | title = _(u'Student Study Levels') |
---|
192 | |
---|
193 | def mangle_value(self, value, name, context=None): |
---|
194 | """Treat location values special. |
---|
195 | """ |
---|
196 | if name == 'student_id' and context is not None: |
---|
197 | student = context.student |
---|
198 | value = getattr(student, name, None) |
---|
199 | return super( |
---|
200 | StudentStudyLevelExporter, self).mangle_value( |
---|
201 | value, name, context=context) |
---|
202 | |
---|
203 | def export_all(self, site, filepath=None): |
---|
204 | """Export study levels into filepath as CSV data. |
---|
205 | |
---|
206 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
207 | """ |
---|
208 | return self.export(get_levels(get_students(site)), filepath) |
---|
209 | |
---|
210 | def export_student(self, student, filepath=None): |
---|
211 | return self.export(get_levels([student]), filepath) |
---|
212 | |
---|
213 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
214 | """Exporter for CourseTickets. |
---|
215 | """ |
---|
216 | grok.name('coursetickets') |
---|
217 | |
---|
218 | #: Fieldnames considered by this exporter |
---|
219 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
220 | ['level', 'code'])) + ('student_id', 'certcode') |
---|
221 | |
---|
222 | #: The title under which this exporter will be displayed |
---|
223 | title = _(u'Course Tickets') |
---|
224 | |
---|
225 | def mangle_value(self, value, name, context=None): |
---|
226 | """Treat location values special. |
---|
227 | """ |
---|
228 | if context is not None: |
---|
229 | student = context.student |
---|
230 | if name == 'student_id' and student is not None: |
---|
231 | value = getattr(student, name, None) |
---|
232 | if name == 'level': |
---|
233 | value = getattr(context, 'getLevel', lambda: None)() |
---|
234 | return super( |
---|
235 | CourseTicketExporter, self).mangle_value( |
---|
236 | value, name, context=context) |
---|
237 | |
---|
238 | def export_all(self, site, filepath=None): |
---|
239 | """Export course tickets into filepath as CSV data. |
---|
240 | |
---|
241 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
242 | """ |
---|
243 | return self.export(get_tickets(get_students(site)), filepath) |
---|
244 | |
---|
245 | def export_student(self, student, filepath=None): |
---|
246 | return self.export(get_tickets([student]), filepath) |
---|
247 | |
---|
248 | |
---|
249 | class PaymentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
250 | """Exporter for OnlinePayment instances. |
---|
251 | """ |
---|
252 | grok.name('studentpayments') |
---|
253 | |
---|
254 | #: Fieldnames considered by this exporter |
---|
255 | fields = tuple( |
---|
256 | sorted(iface_names( |
---|
257 | IStudentOnlinePayment, exclude_attribs=False))) + ( |
---|
258 | 'student_id','student_state','current_session') |
---|
259 | |
---|
260 | #: The title under which this exporter will be displayed |
---|
261 | title = _(u'Student Payments') |
---|
262 | |
---|
263 | def mangle_value(self, value, name, context=None): |
---|
264 | """Treat location values special. |
---|
265 | """ |
---|
266 | if context is not None: |
---|
267 | student = context.student |
---|
268 | if name in ['student_id'] and student is not None: |
---|
269 | value = getattr(student, name, None) |
---|
270 | return super( |
---|
271 | PaymentsExporter, self).mangle_value( |
---|
272 | value, name, context=context) |
---|
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 | return self.export(get_payments(get_students(site)), filepath) |
---|
280 | |
---|
281 | def export_student(self, student, filepath=None): |
---|
282 | return self.export(get_payments([student]), filepath) |
---|
283 | |
---|
284 | class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
285 | """Exporter for BedTicket instances. |
---|
286 | """ |
---|
287 | grok.name('bedtickets') |
---|
288 | |
---|
289 | #: Fieldnames considered by this exporter |
---|
290 | fields = tuple( |
---|
291 | sorted(iface_names( |
---|
292 | IBedTicket, exclude_attribs=False))) + ( |
---|
293 | 'student_id', 'actual_bed_type') |
---|
294 | |
---|
295 | #: The title under which this exporter will be displayed |
---|
296 | title = _(u'Bed Tickets') |
---|
297 | |
---|
298 | def mangle_value(self, value, name, context=None): |
---|
299 | """Treat location values and others special. |
---|
300 | """ |
---|
301 | if context is not None: |
---|
302 | student = context.student |
---|
303 | if name in ['student_id'] and student is not None: |
---|
304 | value = getattr(student, name, None) |
---|
305 | if name == 'bed' and value is not None: |
---|
306 | value = getattr(value, 'bed_id', None) |
---|
307 | if name == 'actual_bed_type': |
---|
308 | value = getattr(getattr(context, 'bed', None), 'bed_type') |
---|
309 | return super( |
---|
310 | BedTicketsExporter, self).mangle_value( |
---|
311 | value, name, context=context) |
---|
312 | |
---|
313 | def export_all(self, site, filepath=None): |
---|
314 | """Export payments into filepath as CSV data. |
---|
315 | |
---|
316 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
317 | """ |
---|
318 | return self.export(get_bedtickets(get_students(site)), filepath) |
---|
319 | |
---|
320 | def export_student(self, student, filepath=None): |
---|
321 | return self.export(get_bedtickets([student]), filepath) |
---|