[8057] | 1 | ## $Id: export.py 12873 2015-04-23 19:27:29Z 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 | ## |
---|
[7944] | 18 | """Exporters for student related stuff. |
---|
| 19 | """ |
---|
[9937] | 20 | import os |
---|
[7944] | 21 | import grok |
---|
[9574] | 22 | from datetime import datetime |
---|
[9937] | 23 | from zope.component import getUtility |
---|
[11757] | 24 | from waeup.kofa.interfaces import ( |
---|
| 25 | IExtFileStore, IFileStoreNameChooser, IKofaUtils) |
---|
[7944] | 26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[9843] | 27 | from waeup.kofa.students.catalog import StudentsQuery, CourseTicketsQuery |
---|
[8015] | 28 | from waeup.kofa.students.interfaces import ( |
---|
[8371] | 29 | IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket, |
---|
[9427] | 30 | IStudentOnlinePayment, ICSVStudentExporter, IBedTicket) |
---|
[9787] | 31 | from waeup.kofa.students.vocabularies import study_levels |
---|
[7944] | 32 | from waeup.kofa.utils.batching import ExporterBase |
---|
[11757] | 33 | from waeup.kofa.utils.helpers import iface_names, to_timezone |
---|
[7944] | 34 | |
---|
[8400] | 35 | |
---|
[9787] | 36 | def get_students(site, stud_filter=StudentsQuery()): |
---|
[8414] | 37 | """Get all students registered in catalog in `site`. |
---|
[7944] | 38 | """ |
---|
[9787] | 39 | return stud_filter.query() |
---|
[8414] | 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 | |
---|
[9844] | 56 | def get_tickets(students, **kw): |
---|
| 57 | """Get course tickets of `students`. |
---|
[10017] | 58 | If code is passed through, filter course tickets |
---|
| 59 | which belong to this course code and meets level |
---|
| 60 | and level_session. |
---|
[8414] | 61 | """ |
---|
| 62 | tickets = [] |
---|
[9844] | 63 | code = kw.get('code', None) |
---|
[10017] | 64 | level = kw.get('level', None) |
---|
| 65 | level_session = kw.get('level_session', None) |
---|
[9844] | 66 | if code is None: |
---|
[10017] | 67 | for level_obj in get_levels(students): |
---|
| 68 | for ticket in level_obj.values(): |
---|
[9844] | 69 | tickets.append(ticket) |
---|
| 70 | else: |
---|
[10017] | 71 | for level_obj in get_levels(students): |
---|
| 72 | for ticket in level_obj.values(): |
---|
| 73 | if ticket.code != code: |
---|
| 74 | continue |
---|
[11483] | 75 | if level is not None: |
---|
| 76 | level = int(level) |
---|
| 77 | if level_obj.level in (10, 999, None) \ |
---|
| 78 | and int(level) != level_obj.level: |
---|
| 79 | continue |
---|
| 80 | if level_obj.level not in range(level, level+100, 10): |
---|
| 81 | continue |
---|
[10017] | 82 | if level_session is not None and \ |
---|
| 83 | int(level_session) != level_obj.level_session: |
---|
| 84 | continue |
---|
| 85 | tickets.append(ticket) |
---|
[8414] | 86 | return tickets |
---|
| 87 | |
---|
[11730] | 88 | def get_payments(students, paid=False, **kw): |
---|
| 89 | """Get all payments of `students` within given payment_date period. |
---|
[10296] | 90 | """ |
---|
[11730] | 91 | date_format = '%d/%m/%Y' |
---|
[10296] | 92 | payments = [] |
---|
[11730] | 93 | payments_start = kw.get('payments_start') |
---|
| 94 | payments_end = kw.get('payments_end') |
---|
| 95 | if payments_start and payments_end: |
---|
| 96 | # Payment period given |
---|
| 97 | payments_start = datetime.strptime(payments_start, date_format) |
---|
| 98 | payments_end = datetime.strptime(payments_end, date_format) |
---|
[11757] | 99 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 100 | payments_start = tz.localize(payments_start) |
---|
| 101 | payments_end = tz.localize(payments_end) |
---|
[11730] | 102 | if paid: |
---|
| 103 | # Only paid tickets in payment period are considered |
---|
| 104 | for student in students: |
---|
| 105 | for payment in student.get('payments', {}).values(): |
---|
[11757] | 106 | if payment.payment_date and payment.p_state == 'paid': |
---|
| 107 | payment_date = to_timezone(payment.payment_date, tz) |
---|
| 108 | if payment_date > payments_start and \ |
---|
| 109 | payment_date < payments_end: |
---|
| 110 | payments.append(payment) |
---|
[11730] | 111 | else: |
---|
| 112 | # All tickets in payment period are considered |
---|
| 113 | for student in students: |
---|
| 114 | for payment in student.get('payments', {}).values(): |
---|
[11757] | 115 | if payment.payment_date: |
---|
| 116 | payment_date = to_timezone(payment.payment_date, tz) |
---|
| 117 | if payment_date > payments_start and \ |
---|
| 118 | payment_date < payments_end: |
---|
| 119 | payments.append(payment) |
---|
[11730] | 120 | else: |
---|
| 121 | # Payment period not given |
---|
| 122 | if paid: |
---|
| 123 | # Only paid tickets are considered |
---|
| 124 | for student in students: |
---|
| 125 | for payment in student.get('payments', {}).values(): |
---|
| 126 | if payment.p_state == 'paid': |
---|
| 127 | payments.append(payment) |
---|
| 128 | else: |
---|
| 129 | # All tickets are considered |
---|
| 130 | for student in students: |
---|
| 131 | for payment in student.get('payments', {}).values(): |
---|
| 132 | payments.append(payment) |
---|
[10296] | 133 | return payments |
---|
| 134 | |
---|
[9427] | 135 | def get_bedtickets(students): |
---|
| 136 | """Get all bedtickets of `students`. |
---|
| 137 | """ |
---|
| 138 | tickets = [] |
---|
| 139 | for student in students: |
---|
| 140 | for ticket in student.get('accommodation', {}).values(): |
---|
| 141 | tickets.append(ticket) |
---|
| 142 | return tickets |
---|
[8414] | 143 | |
---|
| 144 | class StudentExporterBase(ExporterBase): |
---|
| 145 | """Exporter for students or related objects. |
---|
| 146 | This is a baseclass. |
---|
| 147 | """ |
---|
| 148 | grok.baseclass() |
---|
[8411] | 149 | grok.implements(ICSVStudentExporter) |
---|
| 150 | grok.provides(ICSVStudentExporter) |
---|
[8414] | 151 | |
---|
[9844] | 152 | def filter_func(self, x, **kw): |
---|
[9802] | 153 | return x |
---|
[9797] | 154 | |
---|
[9801] | 155 | def get_filtered(self, site, **kw): |
---|
[9843] | 156 | """Get students from a catalog filtered by keywords. |
---|
| 157 | students_catalog is the default catalog. The keys must be valid |
---|
[9933] | 158 | catalog index names. |
---|
[9843] | 159 | Returns a simple empty list, a list with `Student` |
---|
| 160 | objects or a catalog result set with `Student` |
---|
[9801] | 161 | objects. |
---|
| 162 | |
---|
| 163 | .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog` |
---|
| 164 | |
---|
| 165 | """ |
---|
[9843] | 166 | # Pass only given keywords to create FilteredCatalogQuery objects. |
---|
| 167 | # This way we avoid |
---|
[9801] | 168 | # trouble with `None` value ambivalences and queries are also |
---|
| 169 | # faster (normally less indexes to ask). Drawback is, that |
---|
| 170 | # developers must look into catalog to see what keywords are |
---|
| 171 | # valid. |
---|
[9845] | 172 | if kw.get('catalog', None) == 'coursetickets': |
---|
[9843] | 173 | coursetickets = CourseTicketsQuery(**kw).query() |
---|
| 174 | students = [] |
---|
| 175 | for ticket in coursetickets: |
---|
| 176 | students.append(ticket.student) |
---|
| 177 | return list(set(students)) |
---|
[11730] | 178 | # Payments can be filtered by payment_date. The period boundaries |
---|
| 179 | # are not keys of the catalog and must thus be removed from kw. |
---|
| 180 | try: |
---|
| 181 | del kw['payments_start'] |
---|
| 182 | del kw['payments_end'] |
---|
| 183 | except KeyError: |
---|
| 184 | pass |
---|
[9801] | 185 | query = StudentsQuery(**kw) |
---|
[9797] | 186 | return query.query() |
---|
| 187 | |
---|
[12518] | 188 | def get_selected(self, site, selected): |
---|
| 189 | """Get set of selected students. |
---|
| 190 | Returns a simple empty list or a list with `Student` |
---|
| 191 | objects. |
---|
| 192 | """ |
---|
| 193 | students = [] |
---|
| 194 | students_container = site.get('students', {}) |
---|
| 195 | for id in selected: |
---|
| 196 | student = students_container.get(id, None) |
---|
| 197 | if student: |
---|
| 198 | students.append(student) |
---|
| 199 | return students |
---|
| 200 | |
---|
[8414] | 201 | def export(self, values, filepath=None): |
---|
| 202 | """Export `values`, an iterable, as CSV file. |
---|
| 203 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 204 | """ |
---|
| 205 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 206 | for value in values: |
---|
| 207 | self.write_item(value, writer) |
---|
| 208 | return self.close_outfile(filepath, outfile) |
---|
| 209 | |
---|
[9797] | 210 | def export_all(self, site, filepath=None): |
---|
| 211 | """Export students into filepath as CSV data. |
---|
| 212 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
[9763] | 213 | """ |
---|
[9802] | 214 | return self.export(self.filter_func(get_students(site)), filepath) |
---|
[8414] | 215 | |
---|
[9797] | 216 | def export_student(self, student, filepath=None): |
---|
| 217 | return self.export(self.filter_func([student]), filepath=filepath) |
---|
[9734] | 218 | |
---|
[9802] | 219 | def export_filtered(self, site, filepath=None, **kw): |
---|
| 220 | """Export items denoted by `kw`. |
---|
| 221 | If `filepath` is ``None``, a raw string with CSV data should |
---|
| 222 | be returned. |
---|
| 223 | """ |
---|
| 224 | data = self.get_filtered(site, **kw) |
---|
[9844] | 225 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
[9802] | 226 | |
---|
[12518] | 227 | def export_selected(self,site, filepath=None, **kw): |
---|
| 228 | """Export data for selected set of students. |
---|
| 229 | """ |
---|
| 230 | selected = kw.get('selected', []) |
---|
| 231 | data = self.get_selected(site, selected) |
---|
| 232 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
[9802] | 233 | |
---|
[12518] | 234 | |
---|
[12079] | 235 | class StudentExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[12861] | 236 | """The Student Exporter first filters the set of students by searching the |
---|
| 237 | students catalog. Then it exports student base data of this set of students. |
---|
[8414] | 238 | """ |
---|
[7944] | 239 | grok.name('students') |
---|
| 240 | |
---|
[9936] | 241 | fields = tuple(sorted(iface_names(IStudent))) + ( |
---|
[9253] | 242 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
| 243 | 'current_level', 'current_session') |
---|
[7944] | 244 | title = _(u'Students') |
---|
| 245 | |
---|
[8493] | 246 | def mangle_value(self, value, name, context=None): |
---|
[12861] | 247 | """The mangler prepares the history messages and adds a hash symbol at |
---|
| 248 | the end of the phone number to avoid annoying automatic number |
---|
| 249 | transformation by Excel or Calc.""" |
---|
[8493] | 250 | if name == 'history': |
---|
| 251 | value = value.messages |
---|
[8971] | 252 | if name == 'phone' and value is not None: |
---|
| 253 | # Append hash '#' to phone numbers to circumvent |
---|
| 254 | # unwanted excel automatic |
---|
[8947] | 255 | value = str('%s#' % value) |
---|
[8493] | 256 | return super( |
---|
[12079] | 257 | StudentExporter, self).mangle_value( |
---|
[8493] | 258 | value, name, context=context) |
---|
| 259 | |
---|
[7944] | 260 | |
---|
[8414] | 261 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[12861] | 262 | """The Student Study Course Exporter first filters the set of students |
---|
| 263 | by searching the students catalog. Then it exports the data of the current |
---|
| 264 | study course container of each student from this set. It does |
---|
| 265 | not export their content. |
---|
[7994] | 266 | """ |
---|
| 267 | grok.name('studentstudycourses') |
---|
| 268 | |
---|
[8493] | 269 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
[7994] | 270 | title = _(u'Student Study Courses') |
---|
| 271 | |
---|
[9844] | 272 | def filter_func(self, x, **kw): |
---|
[9797] | 273 | return get_studycourses(x) |
---|
| 274 | |
---|
[7994] | 275 | def mangle_value(self, value, name, context=None): |
---|
[12861] | 276 | """The mangler determines the certificate code and the student id. |
---|
[7994] | 277 | """ |
---|
| 278 | if name == 'certificate' and value is not None: |
---|
| 279 | # XXX: hopefully cert codes are unique site-wide |
---|
| 280 | value = value.code |
---|
[8493] | 281 | if name == 'student_id' and context is not None: |
---|
[8736] | 282 | student = context.student |
---|
[8493] | 283 | value = getattr(student, name, None) |
---|
[7994] | 284 | return super( |
---|
| 285 | StudentStudyCourseExporter, self).mangle_value( |
---|
| 286 | value, name, context=context) |
---|
| 287 | |
---|
| 288 | |
---|
[8414] | 289 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[12861] | 290 | """The Student Study Level Exporter first filters the set of students |
---|
| 291 | by searching the students catalog. Then it exports the data of the student's |
---|
[12862] | 292 | study level container data but not their content (course tickets). |
---|
[12861] | 293 | The exporter iterates over all objects in the students' ``studycourse`` |
---|
| 294 | containers. |
---|
[8015] | 295 | """ |
---|
| 296 | grok.name('studentstudylevels') |
---|
| 297 | |
---|
[8493] | 298 | fields = tuple(sorted(iface_names( |
---|
[12873] | 299 | IStudentStudyLevel))) + ( |
---|
[9253] | 300 | 'student_id', 'number_of_tickets','certcode') |
---|
[8015] | 301 | title = _(u'Student Study Levels') |
---|
| 302 | |
---|
[9844] | 303 | def filter_func(self, x, **kw): |
---|
[9802] | 304 | return get_levels(x) |
---|
| 305 | |
---|
[8015] | 306 | def mangle_value(self, value, name, context=None): |
---|
[12861] | 307 | """The mangler determines the student id, nothing else. |
---|
[8015] | 308 | """ |
---|
[8493] | 309 | if name == 'student_id' and context is not None: |
---|
[8736] | 310 | student = context.student |
---|
[8493] | 311 | value = getattr(student, name, None) |
---|
[8015] | 312 | return super( |
---|
| 313 | StudentStudyLevelExporter, self).mangle_value( |
---|
| 314 | value, name, context=context) |
---|
| 315 | |
---|
[8414] | 316 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[12861] | 317 | """The Course Ticket Exporter exports course tickets. Usually, |
---|
| 318 | the exporter first filters the set of students by searching the |
---|
| 319 | students catalog. Then it collects and iterates over all ``studylevel`` |
---|
| 320 | containers of the filtered student set and finally |
---|
| 321 | iterates over all items inside these containers. |
---|
| 322 | |
---|
| 323 | If the course code is passed through, the exporter uses a different |
---|
| 324 | catalog. It searches for students in the course tickets catalog and |
---|
| 325 | exports those course tickets which belong to the given course code and |
---|
| 326 | also meet level and level_session passed through at the same time. |
---|
[12862] | 327 | This happens if the exporter is called at course level in the academic |
---|
[12861] | 328 | section. |
---|
[8342] | 329 | """ |
---|
| 330 | grok.name('coursetickets') |
---|
| 331 | |
---|
[8493] | 332 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
[11484] | 333 | ['level', 'code', 'level_session'])) + ('student_id', |
---|
| 334 | 'certcode', 'display_fullname') |
---|
[8342] | 335 | title = _(u'Course Tickets') |
---|
| 336 | |
---|
[9844] | 337 | def filter_func(self, x, **kw): |
---|
| 338 | return get_tickets(x, **kw) |
---|
[9802] | 339 | |
---|
[8342] | 340 | def mangle_value(self, value, name, context=None): |
---|
[12861] | 341 | """The mangler determines the student's id and fullname. |
---|
[8342] | 342 | """ |
---|
| 343 | if context is not None: |
---|
[8736] | 344 | student = context.student |
---|
[11484] | 345 | if name in ('student_id', 'display_fullname') and student is not None: |
---|
[8342] | 346 | value = getattr(student, name, None) |
---|
| 347 | return super( |
---|
| 348 | CourseTicketExporter, self).mangle_value( |
---|
| 349 | value, name, context=context) |
---|
| 350 | |
---|
| 351 | |
---|
[12865] | 352 | class StudentPaymentExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 353 | """The Student Payment Exporter first filters the set of students |
---|
[12862] | 354 | by searching the students catalog. Then it exports student payment |
---|
| 355 | tickets by iterating over the items of the student's ``payments`` |
---|
| 356 | container. If the payment period is given only tickets, which were |
---|
| 357 | paid in payment period, are considered for export. |
---|
[8371] | 358 | """ |
---|
| 359 | grok.name('studentpayments') |
---|
| 360 | |
---|
| 361 | fields = tuple( |
---|
[8493] | 362 | sorted(iface_names( |
---|
[9984] | 363 | IStudentOnlinePayment, exclude_attribs=False, |
---|
| 364 | omit=['display_item']))) + ( |
---|
[10232] | 365 | 'student_id','state','current_session') |
---|
[8576] | 366 | title = _(u'Student Payments') |
---|
[8371] | 367 | |
---|
[9844] | 368 | def filter_func(self, x, **kw): |
---|
[11730] | 369 | return get_payments(x, **kw) |
---|
[9802] | 370 | |
---|
[8371] | 371 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 372 | """The mangler determines the student's id, registration |
---|
| 373 | state and current session. |
---|
[8371] | 374 | """ |
---|
| 375 | if context is not None: |
---|
[8736] | 376 | student = context.student |
---|
[10232] | 377 | if name in ['student_id','state', |
---|
| 378 | 'current_session'] and student is not None: |
---|
[8371] | 379 | value = getattr(student, name, None) |
---|
| 380 | return super( |
---|
[12865] | 381 | StudentPaymentExporter, self).mangle_value( |
---|
[8371] | 382 | value, name, context=context) |
---|
| 383 | |
---|
[12865] | 384 | class DataForBursaryExporter(StudentPaymentExporter): |
---|
[12862] | 385 | """The DataForBursary Exporter works just like the Student Payments Exporter |
---|
| 386 | but it exports much more information about the student. It combines |
---|
| 387 | payment and student data in one table in order to spare postprocessing of |
---|
| 388 | two seperate export files. The exporter is primarily used by bursary |
---|
| 389 | officers who have exclusively access to this exporter. |
---|
[10233] | 390 | """ |
---|
| 391 | grok.name('bursary') |
---|
| 392 | |
---|
[10296] | 393 | def filter_func(self, x, **kw): |
---|
[11730] | 394 | return get_payments(x, paid=True, **kw) |
---|
[10296] | 395 | |
---|
[10233] | 396 | fields = tuple( |
---|
| 397 | sorted(iface_names( |
---|
| 398 | IStudentOnlinePayment, exclude_attribs=False, |
---|
| 399 | omit=['display_item']))) + ( |
---|
[11702] | 400 | 'student_id','matric_number','reg_number', |
---|
[10236] | 401 | 'firstname', 'middlename', 'lastname', |
---|
| 402 | 'state','current_session', |
---|
| 403 | 'entry_session', 'entry_mode', |
---|
[11702] | 404 | 'faccode', 'depcode','certcode') |
---|
[10233] | 405 | title = _(u'Payment Data for Bursary') |
---|
| 406 | |
---|
| 407 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 408 | """The mangler fetches the student data. |
---|
[10233] | 409 | """ |
---|
| 410 | if context is not None: |
---|
| 411 | student = context.student |
---|
[10236] | 412 | if name in [ |
---|
[11702] | 413 | 'student_id','matric_number', 'reg_number', |
---|
[10236] | 414 | 'firstname', 'middlename', 'lastname', |
---|
[11702] | 415 | 'state', 'current_session', |
---|
[10236] | 416 | 'entry_session', 'entry_mode', |
---|
[11702] | 417 | 'faccode', 'depcode', 'certcode'] and student is not None: |
---|
[10233] | 418 | value = getattr(student, name, None) |
---|
| 419 | return super( |
---|
[12865] | 420 | StudentPaymentExporter, self).mangle_value( |
---|
[10233] | 421 | value, name, context=context) |
---|
| 422 | |
---|
[12865] | 423 | class BedTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 424 | """The Bed Ticket Exporter first filters the set of students |
---|
[12862] | 425 | by searching the students catalog. Then it exports bed |
---|
| 426 | tickets by iterating over the items of the student's ``accommodation`` |
---|
| 427 | container. |
---|
[9427] | 428 | """ |
---|
| 429 | grok.name('bedtickets') |
---|
| 430 | |
---|
| 431 | fields = tuple( |
---|
| 432 | sorted(iface_names( |
---|
[9984] | 433 | IBedTicket, exclude_attribs=False, |
---|
| 434 | omit=['display_coordinates']))) + ( |
---|
| 435 | 'student_id', 'actual_bed_type') |
---|
[9427] | 436 | title = _(u'Bed Tickets') |
---|
| 437 | |
---|
[9844] | 438 | def filter_func(self, x, **kw): |
---|
[9802] | 439 | return get_bedtickets(x) |
---|
| 440 | |
---|
[9427] | 441 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 442 | """The mangler determines the student id and the type of the bed |
---|
| 443 | which has been booked in the ticket. |
---|
[9427] | 444 | """ |
---|
| 445 | if context is not None: |
---|
| 446 | student = context.student |
---|
| 447 | if name in ['student_id'] and student is not None: |
---|
| 448 | value = getattr(student, name, None) |
---|
| 449 | if name == 'bed' and value is not None: |
---|
| 450 | value = getattr(value, 'bed_id', None) |
---|
| 451 | if name == 'actual_bed_type': |
---|
| 452 | value = getattr(getattr(context, 'bed', None), 'bed_type') |
---|
| 453 | return super( |
---|
[12865] | 454 | BedTicketExporter, self).mangle_value( |
---|
[9427] | 455 | value, name, context=context) |
---|
| 456 | |
---|
[12079] | 457 | class StudentPaymentsOverviewExporter(StudentExporter): |
---|
[12862] | 458 | """The Student Payments Overview Exporter first filters the set of students |
---|
| 459 | by searching the students catalog. Then it exports some student base data |
---|
| 460 | together with the total school fee amount paid in each year over a |
---|
| 461 | predefined year range (current year - 9, ... , current year + 1). |
---|
[9574] | 462 | """ |
---|
| 463 | grok.name('paymentsoverview') |
---|
| 464 | |
---|
| 465 | curr_year = datetime.now().year |
---|
| 466 | year_range = range(curr_year - 9, curr_year + 1) |
---|
| 467 | year_range_tuple = tuple([str(year) for year in year_range]) |
---|
| 468 | |
---|
[9983] | 469 | fields = ('student_id', 'matric_number', 'display_fullname', |
---|
[9574] | 470 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9807] | 471 | 'current_level', 'current_session', 'current_mode', |
---|
[9574] | 472 | ) + year_range_tuple |
---|
| 473 | title = _(u'Student Payments Overview') |
---|
| 474 | |
---|
| 475 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 476 | """The mangler summarizes the school fee amounts made per year. It |
---|
| 477 | iterates over all paid school fee payment tickets and |
---|
| 478 | adds together the amounts paid in a year. Waived payments |
---|
| 479 | are marked ``waived``. |
---|
| 480 | """ |
---|
[9574] | 481 | if name in self.year_range_tuple and context is not None: |
---|
[11661] | 482 | value = 0 |
---|
[9574] | 483 | for ticket in context['payments'].values(): |
---|
[12568] | 484 | if ticket.p_category == 'schoolfee' and \ |
---|
[9574] | 485 | ticket.p_session == int(name): |
---|
[12568] | 486 | if ticket.p_state == 'waived': |
---|
| 487 | value = 'waived' |
---|
| 488 | break |
---|
| 489 | if ticket.p_state == 'paid': |
---|
| 490 | try: |
---|
| 491 | value += ticket.amount_auth |
---|
| 492 | except TypeError: |
---|
| 493 | pass |
---|
[11661] | 494 | if value == 0: |
---|
| 495 | value = '' |
---|
[9574] | 496 | return super( |
---|
[12079] | 497 | StudentExporter, self).mangle_value( |
---|
[9734] | 498 | value, name, context=context) |
---|
[9744] | 499 | |
---|
[12079] | 500 | class StudentStudyLevelsOverviewExporter(StudentExporter): |
---|
[12862] | 501 | """The Student Study Levels Overview Exporter first filters the set of |
---|
| 502 | students by searching the students catalog. Then it exports some student |
---|
| 503 | base data together with the session key of registered levels. |
---|
| 504 | Sample output: |
---|
| 505 | |
---|
| 506 | header: ``...100,110,120,200,210,220,300...`` |
---|
| 507 | |
---|
| 508 | data: ``...2010,,,2011,2012,,2013...`` |
---|
| 509 | |
---|
| 510 | This csv data string means that level 100 was registered in session |
---|
| 511 | 2010/2011, level 200 in session 2011/2012, level 210 (200 on 1st probation) |
---|
| 512 | in session 2012/2013 and level 300 in session 2013/2014. |
---|
[9744] | 513 | """ |
---|
| 514 | grok.name('studylevelsoverview') |
---|
| 515 | |
---|
[9787] | 516 | avail_levels = tuple([str(x) for x in study_levels(None)]) |
---|
[9744] | 517 | |
---|
| 518 | fields = ('student_id', ) + ( |
---|
| 519 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9761] | 520 | 'entry_session', 'current_level', 'current_session', |
---|
[9787] | 521 | ) + avail_levels |
---|
[9744] | 522 | title = _(u'Student Study Levels Overview') |
---|
| 523 | |
---|
| 524 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 525 | """The mangler checks if a given level has been registered. It returns |
---|
| 526 | the ``level_session`` attribute of the student study level object |
---|
| 527 | if the named level exists. |
---|
| 528 | """ |
---|
[9787] | 529 | if name in self.avail_levels and context is not None: |
---|
[9744] | 530 | value = '' |
---|
| 531 | for level in context['studycourse'].values(): |
---|
| 532 | if level.level == int(name): |
---|
[9761] | 533 | value = '%s' % level.level_session |
---|
[9744] | 534 | break |
---|
| 535 | return super( |
---|
[12079] | 536 | StudentExporter, self).mangle_value( |
---|
[9744] | 537 | value, name, context=context) |
---|
[9936] | 538 | |
---|
| 539 | class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[12862] | 540 | """Like all other exporters the Combo Card Data Exporter first filters the |
---|
| 541 | set of students by searching the students catalog. Then it exports some |
---|
| 542 | student base data which are neccessary to print for the Interswitch combo |
---|
| 543 | card (identity card for students). The output contains a ``passport_path`` |
---|
| 544 | column which contains the filesystem path of the passport image file. |
---|
| 545 | If no path is given, no passport image file exists. |
---|
[9936] | 546 | """ |
---|
| 547 | grok.name('combocard') |
---|
| 548 | |
---|
| 549 | fields = ('display_fullname', |
---|
[9937] | 550 | 'student_id','matric_number', |
---|
| 551 | 'certificate', 'faculty', 'department', 'passport_path') |
---|
[9936] | 552 | title = _(u'Combo Card Data') |
---|
| 553 | |
---|
| 554 | def mangle_value(self, value, name, context=None): |
---|
[12862] | 555 | """The mangler determines the titles of faculty, department |
---|
| 556 | and certificate. It also computes the path of passport image file |
---|
| 557 | stored in the filesystem. |
---|
| 558 | """ |
---|
[9936] | 559 | certificate = context['studycourse'].certificate |
---|
| 560 | if name == 'certificate' and certificate is not None: |
---|
| 561 | value = certificate.title |
---|
| 562 | if name == 'department' and certificate is not None: |
---|
[10650] | 563 | value = certificate.__parent__.__parent__.longtitle |
---|
[9936] | 564 | if name == 'faculty' and certificate is not None: |
---|
[10650] | 565 | value = certificate.__parent__.__parent__.__parent__.longtitle |
---|
[9937] | 566 | if name == 'passport_path' and certificate is not None: |
---|
[12862] | 567 | file_id = IFileStoreNameChooser(context).chooseName( |
---|
| 568 | attr='passport.jpg') |
---|
[9937] | 569 | os_path = getUtility(IExtFileStore)._pathFromFileID(file_id) |
---|
| 570 | if not os.path.exists(os_path): |
---|
| 571 | value = None |
---|
| 572 | else: |
---|
| 573 | value = '/'.join(os_path.split('/')[-4:]) |
---|
[9936] | 574 | return super( |
---|
| 575 | ComboCardDataExporter, self).mangle_value( |
---|
| 576 | value, name, context=context) |
---|