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