1 | ## $Id: export.py 15055 2018-06-18 05:20:14Z 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 os |
---|
21 | import grok |
---|
22 | from datetime import datetime, timedelta |
---|
23 | from zope.component import getUtility |
---|
24 | from waeup.kofa.interfaces import ( |
---|
25 | IExtFileStore, IFileStoreNameChooser, IKofaUtils) |
---|
26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
27 | from waeup.kofa.students.catalog import StudentsQuery, CourseTicketsQuery |
---|
28 | from waeup.kofa.students.interfaces import ( |
---|
29 | IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket, |
---|
30 | IStudentOnlinePayment, ICSVStudentExporter, IBedTicket) |
---|
31 | from waeup.kofa.students.vocabularies import study_levels |
---|
32 | from waeup.kofa.utils.batching import ExporterBase |
---|
33 | from waeup.kofa.utils.helpers import iface_names, to_timezone |
---|
34 | |
---|
35 | |
---|
36 | def get_students(site, stud_filter=StudentsQuery()): |
---|
37 | """Get all students registered in catalog in `site`. |
---|
38 | """ |
---|
39 | return stud_filter.query() |
---|
40 | |
---|
41 | def get_studycourses(students): |
---|
42 | """Get studycourses of `students`. |
---|
43 | """ |
---|
44 | return [x.get('studycourse', None) for x in students |
---|
45 | if x is not None] |
---|
46 | |
---|
47 | def get_levels(students): |
---|
48 | """Get all studylevels of `students`. |
---|
49 | """ |
---|
50 | levels = [] |
---|
51 | for course in get_studycourses(students): |
---|
52 | for level in course.values(): |
---|
53 | levels.append(level) |
---|
54 | return levels |
---|
55 | |
---|
56 | def get_tickets(students, **kw): |
---|
57 | """Get course tickets of `students`. |
---|
58 | If code is passed through, filter course tickets |
---|
59 | which belong to this course code and meet level=level |
---|
60 | and level_session=level_session. |
---|
61 | If not, but ct_level and ct_session |
---|
62 | are passed through, filter course tickets |
---|
63 | which meet level==ct_level and level_session==ct_session. |
---|
64 | """ |
---|
65 | tickets = [] |
---|
66 | code = kw.get('code', None) |
---|
67 | level = kw.get('level', None) |
---|
68 | session = kw.get('session', None) |
---|
69 | ct_level = kw.get('ct_level', None) |
---|
70 | ct_session = kw.get('ct_session', None) |
---|
71 | if code is None: |
---|
72 | for level_obj in get_levels(students): |
---|
73 | for ticket in level_obj.values(): |
---|
74 | if ct_level not in ('all', None): |
---|
75 | if level_obj.level in (10, 999, None) \ |
---|
76 | and int(ct_level) != level_obj.level: |
---|
77 | continue |
---|
78 | if level_obj.level not in range( |
---|
79 | int(ct_level), int(ct_level)+100, 10): |
---|
80 | continue |
---|
81 | if ct_session not in ('all', None) and \ |
---|
82 | int(ct_session) != level_obj.level_session: |
---|
83 | continue |
---|
84 | tickets.append(ticket) |
---|
85 | else: |
---|
86 | for level_obj in get_levels(students): |
---|
87 | for ticket in level_obj.values(): |
---|
88 | if ticket.code != code: |
---|
89 | continue |
---|
90 | if level not in ('all', None): |
---|
91 | if level_obj.level in (10, 999, None) \ |
---|
92 | and int(level) != level_obj.level: |
---|
93 | continue |
---|
94 | if level_obj.level not in range( |
---|
95 | int(level), int(level)+100, 10): |
---|
96 | continue |
---|
97 | if session not in ('all', None) and \ |
---|
98 | int(session) != level_obj.level_session: |
---|
99 | continue |
---|
100 | tickets.append(ticket) |
---|
101 | return tickets |
---|
102 | |
---|
103 | def get_tickets_for_lecturer(students, **kw): |
---|
104 | """Get course tickets of `students`. |
---|
105 | Filter course tickets which belong to this course code and |
---|
106 | which are editable by lecturers. |
---|
107 | """ |
---|
108 | tickets = [] |
---|
109 | code = kw.get('code', None) |
---|
110 | level_session = kw.get('session', None) |
---|
111 | level = kw.get('level', None) |
---|
112 | for level_obj in get_levels(students): |
---|
113 | for ticket in level_obj.values(): |
---|
114 | if ticket.code != code: |
---|
115 | continue |
---|
116 | if not ticket.editable_by_lecturer: |
---|
117 | continue |
---|
118 | if level not in ('all', None): |
---|
119 | if level_obj.level in (10, 999, None) \ |
---|
120 | and int(level) != level_obj.level: |
---|
121 | continue |
---|
122 | if level_obj.level not in range(int(level), int(level)+100, 10): |
---|
123 | continue |
---|
124 | if level_session not in ('all', None) and \ |
---|
125 | int(level_session) != level_obj.level_session: |
---|
126 | continue |
---|
127 | tickets.append(ticket) |
---|
128 | return tickets |
---|
129 | |
---|
130 | def get_payments(students, p_states=None, **kw): |
---|
131 | """Get all payments of `students` within given payment_date period. |
---|
132 | """ |
---|
133 | date_format = '%d/%m/%Y' |
---|
134 | payments = [] |
---|
135 | p_start = kw.get('payments_start') |
---|
136 | p_end = kw.get('payments_end') |
---|
137 | paycat = kw.get('paycat') |
---|
138 | paysession = kw.get('paysession') |
---|
139 | for student in students: |
---|
140 | for payment in student.get('payments', {}).values(): |
---|
141 | if p_start and p_end: |
---|
142 | if not payment.payment_date: |
---|
143 | continue |
---|
144 | payments_start = datetime.strptime(p_start, date_format) |
---|
145 | payments_end = datetime.strptime(p_end, date_format) |
---|
146 | tz = getUtility(IKofaUtils).tzinfo |
---|
147 | payments_start = tz.localize(payments_start) |
---|
148 | payments_end = tz.localize(payments_end) + timedelta(days=1) |
---|
149 | payment_date = to_timezone(payment.payment_date, tz) |
---|
150 | if payment_date < payments_start or payment_date > payments_end: |
---|
151 | continue |
---|
152 | if p_states and not payment.p_state in p_states: |
---|
153 | continue |
---|
154 | if paycat not in ('all', None) and payment.p_category != paycat: |
---|
155 | continue |
---|
156 | if paysession not in ('all', None) and payment.p_session != paysession: |
---|
157 | continue |
---|
158 | payments.append(payment) |
---|
159 | return payments |
---|
160 | |
---|
161 | def get_bedtickets(students): |
---|
162 | """Get all bedtickets of `students`. |
---|
163 | """ |
---|
164 | tickets = [] |
---|
165 | for student in students: |
---|
166 | for ticket in student.get('accommodation', {}).values(): |
---|
167 | tickets.append(ticket) |
---|
168 | return tickets |
---|
169 | |
---|
170 | class StudentExporterBase(ExporterBase): |
---|
171 | """Exporter for students or related objects. |
---|
172 | This is a baseclass. |
---|
173 | """ |
---|
174 | grok.baseclass() |
---|
175 | grok.implements(ICSVStudentExporter) |
---|
176 | grok.provides(ICSVStudentExporter) |
---|
177 | |
---|
178 | def filter_func(self, x, **kw): |
---|
179 | return x |
---|
180 | |
---|
181 | def get_filtered(self, site, **kw): |
---|
182 | """Get students from a catalog filtered by keywords. |
---|
183 | students_catalog is the default catalog. The keys must be valid |
---|
184 | catalog index names. |
---|
185 | Returns a simple empty list, a list with `Student` |
---|
186 | objects or a catalog result set with `Student` |
---|
187 | objects. |
---|
188 | |
---|
189 | .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog` |
---|
190 | |
---|
191 | """ |
---|
192 | # Pass only given keywords to create FilteredCatalogQuery objects. |
---|
193 | # This way we avoid |
---|
194 | # trouble with `None` value ambivalences and queries are also |
---|
195 | # faster (normally less indexes to ask). Drawback is, that |
---|
196 | # developers must look into catalog to see what keywords are |
---|
197 | # valid. |
---|
198 | if kw.get('catalog', None) == 'coursetickets': |
---|
199 | coursetickets = CourseTicketsQuery(**kw).query() |
---|
200 | students = [] |
---|
201 | for ticket in coursetickets: |
---|
202 | students.append(ticket.student) |
---|
203 | return list(set(students)) |
---|
204 | # Payments can be filtered by payment date and payment category. |
---|
205 | # These parameters are not keys of the catalog and must thus be |
---|
206 | # removed from kw. |
---|
207 | try: |
---|
208 | del kw['payments_start'] |
---|
209 | del kw['payments_end'] |
---|
210 | del kw['paycat'] |
---|
211 | del kw['paysession'] |
---|
212 | except KeyError: |
---|
213 | pass |
---|
214 | # Coursetickets can be filtered by level and session. |
---|
215 | # These parameters are not keys of the catalog and must thus be |
---|
216 | # removed from kw. |
---|
217 | try: |
---|
218 | del kw['ct_level'] |
---|
219 | del kw['ct_session'] |
---|
220 | except KeyError: |
---|
221 | pass |
---|
222 | query = StudentsQuery(**kw) |
---|
223 | return query.query() |
---|
224 | |
---|
225 | def get_selected(self, site, selected): |
---|
226 | """Get set of selected students. |
---|
227 | Returns a simple empty list or a list with `Student` |
---|
228 | objects. |
---|
229 | """ |
---|
230 | students = [] |
---|
231 | students_container = site.get('students', {}) |
---|
232 | for id in selected: |
---|
233 | student = students_container.get(id, None) |
---|
234 | if student is None: |
---|
235 | # try matric number |
---|
236 | result = list(StudentsQuery(matric_number=id).query()) |
---|
237 | if result: |
---|
238 | student = result[0] |
---|
239 | else: |
---|
240 | continue |
---|
241 | students.append(student) |
---|
242 | return students |
---|
243 | |
---|
244 | def export(self, values, filepath=None): |
---|
245 | """Export `values`, an iterable, as CSV file. |
---|
246 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
247 | """ |
---|
248 | writer, outfile = self.get_csv_writer(filepath) |
---|
249 | for value in values: |
---|
250 | self.write_item(value, writer) |
---|
251 | return self.close_outfile(filepath, outfile) |
---|
252 | |
---|
253 | def export_all(self, site, filepath=None): |
---|
254 | """Export students into filepath as CSV data. |
---|
255 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
256 | """ |
---|
257 | return self.export(self.filter_func(get_students(site)), filepath) |
---|
258 | |
---|
259 | def export_student(self, student, filepath=None): |
---|
260 | return self.export(self.filter_func([student]), filepath=filepath) |
---|
261 | |
---|
262 | def export_filtered(self, site, filepath=None, **kw): |
---|
263 | """Export items denoted by `kw`. |
---|
264 | If `filepath` is ``None``, a raw string with CSV data should |
---|
265 | be returned. |
---|
266 | """ |
---|
267 | data = self.get_filtered(site, **kw) |
---|
268 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
269 | |
---|
270 | def export_selected(self,site, filepath=None, **kw): |
---|
271 | """Export data for selected set of students. |
---|
272 | """ |
---|
273 | selected = kw.get('selected', []) |
---|
274 | data = self.get_selected(site, selected) |
---|
275 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
276 | |
---|
277 | |
---|
278 | class StudentExporter(grok.GlobalUtility, StudentExporterBase): |
---|
279 | """The Student Exporter first filters the set of students by searching the |
---|
280 | students catalog. Then it exports student base data of this set of students. |
---|
281 | """ |
---|
282 | grok.name('students') |
---|
283 | |
---|
284 | fields = tuple(sorted(iface_names(IStudent))) + ( |
---|
285 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
286 | 'current_level', 'current_session') |
---|
287 | title = _(u'Students') |
---|
288 | |
---|
289 | def mangle_value(self, value, name, context=None): |
---|
290 | """The mangler prepares the history messages and adds a hash symbol at |
---|
291 | the end of the phone number to avoid annoying automatic number |
---|
292 | transformation by Excel or Calc.""" |
---|
293 | if name == 'history': |
---|
294 | value = value.messages |
---|
295 | if 'phone' in name and value is not None: |
---|
296 | # Append hash '#' to phone numbers to circumvent |
---|
297 | # unwanted excel automatic |
---|
298 | value = str('%s#' % value) |
---|
299 | return super( |
---|
300 | StudentExporter, self).mangle_value( |
---|
301 | value, name, context=context) |
---|
302 | |
---|
303 | |
---|
304 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
305 | """The Student Study Course Exporter first filters the set of students |
---|
306 | by searching the students catalog. Then it exports the data of the current |
---|
307 | study course container of each student from this set. It does |
---|
308 | not export their content. |
---|
309 | """ |
---|
310 | grok.name('studentstudycourses') |
---|
311 | |
---|
312 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
313 | title = _(u'Student Study Courses') |
---|
314 | |
---|
315 | def filter_func(self, x, **kw): |
---|
316 | return get_studycourses(x) |
---|
317 | |
---|
318 | def mangle_value(self, value, name, context=None): |
---|
319 | """The mangler determines the certificate code and the student id. |
---|
320 | """ |
---|
321 | if name == 'certificate' and value is not None: |
---|
322 | # XXX: hopefully cert codes are unique site-wide |
---|
323 | value = value.code |
---|
324 | if name == 'student_id' and context is not None: |
---|
325 | student = context.student |
---|
326 | value = getattr(student, name, None) |
---|
327 | return super( |
---|
328 | StudentStudyCourseExporter, self).mangle_value( |
---|
329 | value, name, context=context) |
---|
330 | |
---|
331 | |
---|
332 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
333 | """The Student Study Level Exporter first filters the set of students |
---|
334 | by searching the students catalog. Then it exports the data of the student's |
---|
335 | study level container data but not their content (course tickets). |
---|
336 | The exporter iterates over all objects in the students' ``studycourse`` |
---|
337 | containers. |
---|
338 | """ |
---|
339 | grok.name('studentstudylevels') |
---|
340 | |
---|
341 | fields = tuple(sorted(iface_names( |
---|
342 | IStudentStudyLevel))) + ( |
---|
343 | 'student_id', 'number_of_tickets','certcode') |
---|
344 | title = _(u'Student Study Levels') |
---|
345 | |
---|
346 | def filter_func(self, x, **kw): |
---|
347 | return get_levels(x) |
---|
348 | |
---|
349 | def mangle_value(self, value, name, context=None): |
---|
350 | """The mangler determines the student id, nothing else. |
---|
351 | """ |
---|
352 | if name == 'student_id' and context is not None: |
---|
353 | student = context.student |
---|
354 | value = getattr(student, name, None) |
---|
355 | return super( |
---|
356 | StudentStudyLevelExporter, self).mangle_value( |
---|
357 | value, name, context=context) |
---|
358 | |
---|
359 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
360 | """The Course Ticket Exporter exports course tickets. Usually, |
---|
361 | the exporter first filters the set of students by searching the |
---|
362 | students catalog. Then it collects and iterates over all ``studylevel`` |
---|
363 | containers of the filtered student set and finally |
---|
364 | iterates over all items inside these containers. |
---|
365 | |
---|
366 | If the course code is passed through, the exporter uses a different |
---|
367 | catalog. It searches for students in the course tickets catalog and |
---|
368 | exports those course tickets which belong to the given course code and |
---|
369 | also meet level and session passed through at the same time. |
---|
370 | This happens if the exporter is called at course level in the academic |
---|
371 | section. |
---|
372 | """ |
---|
373 | grok.name('coursetickets') |
---|
374 | |
---|
375 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
376 | ['level', 'code', 'level_session'])) + ('student_id', |
---|
377 | 'certcode', 'display_fullname') |
---|
378 | title = _(u'Course Tickets') |
---|
379 | |
---|
380 | def filter_func(self, x, **kw): |
---|
381 | return get_tickets(x, **kw) |
---|
382 | |
---|
383 | def mangle_value(self, value, name, context=None): |
---|
384 | """The mangler determines the student's id and fullname. |
---|
385 | """ |
---|
386 | if context is not None: |
---|
387 | student = context.student |
---|
388 | if name in ('student_id', 'display_fullname') and student is not None: |
---|
389 | value = getattr(student, name, None) |
---|
390 | return super( |
---|
391 | CourseTicketExporter, self).mangle_value( |
---|
392 | value, name, context=context) |
---|
393 | |
---|
394 | class DataForLecturerExporter(grok.GlobalUtility, StudentExporterBase): |
---|
395 | """The Data for Lecturer Exporter searches for students in the course |
---|
396 | tickets catalog and exports those course tickets which belong to the |
---|
397 | given course code, meet level and session passed through at the |
---|
398 | same time, and which are editable by lecturers. This exporter can only |
---|
399 | be called at course level in the academic section. |
---|
400 | """ |
---|
401 | grok.name('lecturer') |
---|
402 | |
---|
403 | fields = ('matric_number', 'student_id', 'display_fullname', |
---|
404 | 'level', 'code', 'level_session', 'score') |
---|
405 | |
---|
406 | title = _(u'Data for Lecturer') |
---|
407 | |
---|
408 | def filter_func(self, x, **kw): |
---|
409 | return get_tickets_for_lecturer(x, **kw) |
---|
410 | |
---|
411 | def mangle_value(self, value, name, context=None): |
---|
412 | """The mangler determines the student's id and fullname. |
---|
413 | """ |
---|
414 | if context is not None: |
---|
415 | student = context.student |
---|
416 | if name in ('matric_number', |
---|
417 | 'reg_number', |
---|
418 | 'student_id', |
---|
419 | 'display_fullname',) and student is not None: |
---|
420 | value = getattr(student, name, None) |
---|
421 | return super( |
---|
422 | DataForLecturerExporter, self).mangle_value( |
---|
423 | value, name, context=context) |
---|
424 | |
---|
425 | class StudentPaymentExporter(grok.GlobalUtility, StudentExporterBase): |
---|
426 | """The Student Payment Exporter first filters the set of students |
---|
427 | by searching the students catalog. Then it exports student payment |
---|
428 | tickets by iterating over the items of the student's ``payments`` |
---|
429 | container. If the payment period is given, only tickets, which were |
---|
430 | paid in payment period, are considered for export. |
---|
431 | """ |
---|
432 | grok.name('studentpayments') |
---|
433 | |
---|
434 | fields = tuple( |
---|
435 | sorted(iface_names( |
---|
436 | IStudentOnlinePayment, exclude_attribs=False, |
---|
437 | omit=['display_item', 'certificate', 'student']))) + ( |
---|
438 | 'student_id','state','current_session') |
---|
439 | title = _(u'Student Payments') |
---|
440 | |
---|
441 | def filter_func(self, x, **kw): |
---|
442 | return get_payments(x, **kw) |
---|
443 | |
---|
444 | def mangle_value(self, value, name, context=None): |
---|
445 | """The mangler determines the student's id, registration |
---|
446 | state and current session. |
---|
447 | """ |
---|
448 | if context is not None: |
---|
449 | student = context.student |
---|
450 | if name in ['student_id','state', |
---|
451 | 'current_session'] and student is not None: |
---|
452 | value = getattr(student, name, None) |
---|
453 | return super( |
---|
454 | StudentPaymentExporter, self).mangle_value( |
---|
455 | value, name, context=context) |
---|
456 | |
---|
457 | class StudentUnpaidPaymentExporter(StudentPaymentExporter): |
---|
458 | """The Student Unpaid Payment Exporter works just like the |
---|
459 | Student Payments Exporter but it exports only unpaid tickets. |
---|
460 | This exporter is designed for finding and finally purging outdated |
---|
461 | payment ticket. |
---|
462 | """ |
---|
463 | grok.name('studentunpaidpayments') |
---|
464 | |
---|
465 | title = _(u'Student Unpaid Payments') |
---|
466 | |
---|
467 | def filter_func(self, x, **kw): |
---|
468 | return get_payments(x, p_states=('unpaid',) , **kw) |
---|
469 | |
---|
470 | class DataForBursaryExporter(StudentPaymentExporter): |
---|
471 | """The DataForBursary Exporter works just like the Student Payment Exporter |
---|
472 | but it exports much more information about the student. It combines |
---|
473 | payment and student data in one table in order to spare postprocessing of |
---|
474 | two seperate export files. The exporter is primarily used by bursary |
---|
475 | officers who have exclusively access to this exporter. The exporter |
---|
476 | exports ``paid`` and ``waived`` payment tickets. |
---|
477 | """ |
---|
478 | grok.name('bursary') |
---|
479 | |
---|
480 | def filter_func(self, x, **kw): |
---|
481 | return get_payments(x, p_states=('paid', 'waived'), **kw) |
---|
482 | |
---|
483 | fields = tuple( |
---|
484 | sorted(iface_names( |
---|
485 | IStudentOnlinePayment, exclude_attribs=False, |
---|
486 | omit=['display_item', 'certificate', 'student']))) + ( |
---|
487 | 'student_id','matric_number','reg_number', |
---|
488 | 'firstname', 'middlename', 'lastname', |
---|
489 | 'state','current_session', |
---|
490 | 'entry_session', 'entry_mode', |
---|
491 | 'faccode', 'depcode','certcode') |
---|
492 | title = _(u'Payment Data for Bursary') |
---|
493 | |
---|
494 | def mangle_value(self, value, name, context=None): |
---|
495 | """The mangler fetches the student data. |
---|
496 | """ |
---|
497 | if context is not None: |
---|
498 | student = context.student |
---|
499 | if name in [ |
---|
500 | 'student_id','matric_number', 'reg_number', |
---|
501 | 'firstname', 'middlename', 'lastname', |
---|
502 | 'state', 'current_session', |
---|
503 | 'entry_session', 'entry_mode', |
---|
504 | 'faccode', 'depcode', 'certcode'] and student is not None: |
---|
505 | value = getattr(student, name, None) |
---|
506 | return super( |
---|
507 | StudentPaymentExporter, self).mangle_value( |
---|
508 | value, name, context=context) |
---|
509 | |
---|
510 | class BedTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
511 | """The Bed Ticket Exporter first filters the set of students |
---|
512 | by searching the students catalog. Then it exports bed |
---|
513 | tickets by iterating over the items of the student's ``accommodation`` |
---|
514 | container. |
---|
515 | """ |
---|
516 | grok.name('bedtickets') |
---|
517 | |
---|
518 | fields = tuple( |
---|
519 | sorted(iface_names( |
---|
520 | IBedTicket, exclude_attribs=False, |
---|
521 | omit=['display_coordinates', 'maint_payment_made']))) + ( |
---|
522 | 'student_id', 'actual_bed_type') |
---|
523 | title = _(u'Bed Tickets') |
---|
524 | |
---|
525 | def filter_func(self, x, **kw): |
---|
526 | return get_bedtickets(x) |
---|
527 | |
---|
528 | def mangle_value(self, value, name, context=None): |
---|
529 | """The mangler determines the student id and the type of the bed |
---|
530 | which has been booked in the ticket. |
---|
531 | """ |
---|
532 | if context is not None: |
---|
533 | student = context.student |
---|
534 | if name in ['student_id'] and student is not None: |
---|
535 | value = getattr(student, name, None) |
---|
536 | if name == 'bed' and value is not None: |
---|
537 | value = getattr(value, 'bed_id', None) |
---|
538 | if name == 'actual_bed_type': |
---|
539 | value = getattr(getattr(context, 'bed', None), 'bed_type', None) |
---|
540 | return super( |
---|
541 | BedTicketExporter, self).mangle_value( |
---|
542 | value, name, context=context) |
---|
543 | |
---|
544 | class SchoolFeePaymentsOverviewExporter(StudentExporter): |
---|
545 | """The School Fee Payments Overview Exporter first filters the set of students |
---|
546 | by searching the students catalog. Then it exports some student base data |
---|
547 | together with the total school fee amount paid in each year over a |
---|
548 | predefined year range (current year - 9, ... , current year + 1). |
---|
549 | """ |
---|
550 | grok.name('sfpaymentsoverview') |
---|
551 | |
---|
552 | curr_year = datetime.now().year |
---|
553 | year_range = range(curr_year - 11, curr_year + 1) |
---|
554 | year_range_tuple = tuple([str(year) for year in year_range]) |
---|
555 | fields = ('student_id', 'matric_number', 'display_fullname', |
---|
556 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
557 | 'current_level', 'current_session', 'current_mode', |
---|
558 | 'entry_session', 'reg_number' |
---|
559 | ) + year_range_tuple |
---|
560 | title = _(u'Student School Fee Payments Overview') |
---|
561 | |
---|
562 | def mangle_value(self, value, name, context=None): |
---|
563 | """The mangler summarizes the school fee amounts made per year. It |
---|
564 | iterates over all paid school fee payment tickets and |
---|
565 | adds together the amounts paid in a year. Waived payments |
---|
566 | are marked ``waived``. |
---|
567 | """ |
---|
568 | if name in self.year_range_tuple and context is not None: |
---|
569 | value = 0 |
---|
570 | for ticket in context['payments'].values(): |
---|
571 | if ticket.p_category == 'schoolfee' and \ |
---|
572 | ticket.p_session == int(name): |
---|
573 | if ticket.p_state == 'waived': |
---|
574 | value = 'waived' |
---|
575 | break |
---|
576 | if ticket.p_state == 'paid': |
---|
577 | try: |
---|
578 | value += ticket.amount_auth |
---|
579 | except TypeError: |
---|
580 | pass |
---|
581 | if value == 0: |
---|
582 | value = '' |
---|
583 | elif isinstance(value, float): |
---|
584 | value = round(value, 2) |
---|
585 | return super( |
---|
586 | StudentExporter, self).mangle_value( |
---|
587 | value, name, context=context) |
---|
588 | |
---|
589 | class SessionPaymentsOverviewExporter(StudentExporter): |
---|
590 | """The Session Payments Overview Exporter first filters the set of students |
---|
591 | by searching the students catalog. Then it exports some student base data |
---|
592 | together with lists of payment years for a predefined range of payment |
---|
593 | categories. |
---|
594 | """ |
---|
595 | grok.name('sessionpaymentsoverview') |
---|
596 | |
---|
597 | paycats = ('schoolfee', 'clearance', 'gown', 'transcript') |
---|
598 | fields = ('student_id', 'matric_number', 'display_fullname', |
---|
599 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
600 | 'current_level', 'current_session', 'current_mode', |
---|
601 | 'entry_session', 'reg_number' |
---|
602 | ) + paycats |
---|
603 | title = _(u'Session Payments Overview') |
---|
604 | |
---|
605 | def mangle_value(self, value, name, context=None): |
---|
606 | """ |
---|
607 | """ |
---|
608 | if name in self.paycats and context is not None: |
---|
609 | value = '' |
---|
610 | for ticket in context['payments'].values(): |
---|
611 | if ticket.p_category == name: |
---|
612 | if ticket.p_state in ('waived', 'paid'): |
---|
613 | value += '%s ' % ticket.p_session |
---|
614 | value = value.strip().replace(' ', '+') |
---|
615 | return super( |
---|
616 | StudentExporter, self).mangle_value( |
---|
617 | value, name, context=context) |
---|
618 | |
---|
619 | class StudentStudyLevelsOverviewExporter(StudentExporter): |
---|
620 | """The Student Study Levels Overview Exporter first filters the set of |
---|
621 | students by searching the students catalog. Then it exports some student |
---|
622 | base data together with the session key of registered levels. |
---|
623 | Sample output: |
---|
624 | |
---|
625 | header: ``...100,110,120,200,210,220,300...`` |
---|
626 | |
---|
627 | data: ``...2010,,,2011,2012,,2013...`` |
---|
628 | |
---|
629 | This csv data string means that level 100 was registered in session |
---|
630 | 2010/2011, level 200 in session 2011/2012, level 210 (200 on 1st probation) |
---|
631 | in session 2012/2013 and level 300 in session 2013/2014. |
---|
632 | """ |
---|
633 | grok.name('studylevelsoverview') |
---|
634 | |
---|
635 | avail_levels = tuple([str(x) for x in study_levels(None)]) |
---|
636 | |
---|
637 | fields = ('student_id', ) + ( |
---|
638 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
639 | 'entry_session', 'current_level', 'current_session', |
---|
640 | ) + avail_levels |
---|
641 | title = _(u'Student Study Levels Overview') |
---|
642 | |
---|
643 | def mangle_value(self, value, name, context=None): |
---|
644 | """The mangler checks if a given level has been registered. It returns |
---|
645 | the ``level_session`` attribute of the student study level object |
---|
646 | if the named level exists. |
---|
647 | """ |
---|
648 | if name in self.avail_levels and context is not None: |
---|
649 | value = '' |
---|
650 | for level in context['studycourse'].values(): |
---|
651 | if level.level == int(name): |
---|
652 | value = '%s' % level.level_session |
---|
653 | break |
---|
654 | return super( |
---|
655 | StudentExporter, self).mangle_value( |
---|
656 | value, name, context=context) |
---|
657 | |
---|
658 | class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase): |
---|
659 | """Like all other exporters the Combo Card Data Exporter first filters the |
---|
660 | set of students by searching the students catalog. Then it exports some |
---|
661 | student base data which are neccessary to print for the Interswitch combo |
---|
662 | card (identity card for students). The output contains a ``passport_path`` |
---|
663 | column which contains the filesystem path of the passport image file. |
---|
664 | If no path is given, no passport image file exists. |
---|
665 | """ |
---|
666 | grok.name('combocard') |
---|
667 | |
---|
668 | fields = ('display_fullname', |
---|
669 | 'student_id','matric_number', |
---|
670 | 'certificate', 'faculty', 'department', 'passport_path') |
---|
671 | title = _(u'Combo Card Data') |
---|
672 | |
---|
673 | def mangle_value(self, value, name, context=None): |
---|
674 | """The mangler determines the titles of faculty, department |
---|
675 | and certificate. It also computes the path of passport image file |
---|
676 | stored in the filesystem. |
---|
677 | """ |
---|
678 | certificate = context['studycourse'].certificate |
---|
679 | if name == 'certificate' and certificate is not None: |
---|
680 | value = certificate.title |
---|
681 | if name == 'department' and certificate is not None: |
---|
682 | value = certificate.__parent__.__parent__.longtitle |
---|
683 | if name == 'faculty' and certificate is not None: |
---|
684 | value = certificate.__parent__.__parent__.__parent__.longtitle |
---|
685 | if name == 'passport_path' and certificate is not None: |
---|
686 | file_id = IFileStoreNameChooser(context).chooseName( |
---|
687 | attr='passport.jpg') |
---|
688 | os_path = getUtility(IExtFileStore)._pathFromFileID(file_id) |
---|
689 | if not os.path.exists(os_path): |
---|
690 | value = None |
---|
691 | else: |
---|
692 | value = '/'.join(os_path.split('/')[-4:]) |
---|
693 | return super( |
---|
694 | ComboCardDataExporter, self).mangle_value( |
---|
695 | value, name, context=context) |
---|