[8057] | 1 | ## $Id: export.py 11757 2014-07-10 12:20:30Z 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 | #: A tuple containing all exporter names referring to students or |
---|
| 36 | #: subobjects thereof. |
---|
[9834] | 37 | EXPORTER_NAMES = ('students', 'studentstudycourses', |
---|
| 38 | 'studentstudylevels', 'coursetickets', |
---|
| 39 | 'studentpayments', 'bedtickets', 'paymentsoverview', |
---|
[10233] | 40 | 'studylevelsoverview', 'combocard', 'bursary') |
---|
[8400] | 41 | |
---|
[9787] | 42 | def get_students(site, stud_filter=StudentsQuery()): |
---|
[8414] | 43 | """Get all students registered in catalog in `site`. |
---|
[7944] | 44 | """ |
---|
[9787] | 45 | return stud_filter.query() |
---|
[8414] | 46 | |
---|
| 47 | def get_studycourses(students): |
---|
| 48 | """Get studycourses of `students`. |
---|
| 49 | """ |
---|
| 50 | return [x.get('studycourse', None) for x in students |
---|
| 51 | if x is not None] |
---|
| 52 | |
---|
| 53 | def get_levels(students): |
---|
| 54 | """Get all studylevels of `students`. |
---|
| 55 | """ |
---|
| 56 | levels = [] |
---|
| 57 | for course in get_studycourses(students): |
---|
| 58 | for level in course.values(): |
---|
| 59 | levels.append(level) |
---|
| 60 | return levels |
---|
| 61 | |
---|
[9844] | 62 | def get_tickets(students, **kw): |
---|
| 63 | """Get course tickets of `students`. |
---|
| 64 | |
---|
[10017] | 65 | If code is passed through, filter course tickets |
---|
| 66 | which belong to this course code and meets level |
---|
| 67 | and level_session. |
---|
[8414] | 68 | """ |
---|
| 69 | tickets = [] |
---|
[9844] | 70 | code = kw.get('code', None) |
---|
[10017] | 71 | level = kw.get('level', None) |
---|
| 72 | level_session = kw.get('level_session', None) |
---|
[9844] | 73 | if code is None: |
---|
[10017] | 74 | for level_obj in get_levels(students): |
---|
| 75 | for ticket in level_obj.values(): |
---|
[9844] | 76 | tickets.append(ticket) |
---|
| 77 | else: |
---|
[10017] | 78 | for level_obj in get_levels(students): |
---|
| 79 | for ticket in level_obj.values(): |
---|
| 80 | if ticket.code != code: |
---|
| 81 | continue |
---|
[11483] | 82 | if level is not None: |
---|
| 83 | level = int(level) |
---|
| 84 | if level_obj.level in (10, 999, None) \ |
---|
| 85 | and int(level) != level_obj.level: |
---|
| 86 | continue |
---|
| 87 | if level_obj.level not in range(level, level+100, 10): |
---|
| 88 | continue |
---|
[10017] | 89 | if level_session is not None and \ |
---|
| 90 | int(level_session) != level_obj.level_session: |
---|
| 91 | continue |
---|
| 92 | tickets.append(ticket) |
---|
[8414] | 93 | return tickets |
---|
| 94 | |
---|
[11730] | 95 | def get_payments(students, paid=False, **kw): |
---|
| 96 | """Get all payments of `students` within given payment_date period. |
---|
[8414] | 97 | |
---|
[10296] | 98 | """ |
---|
[11730] | 99 | date_format = '%d/%m/%Y' |
---|
[10296] | 100 | payments = [] |
---|
[11730] | 101 | payments_start = kw.get('payments_start') |
---|
| 102 | payments_end = kw.get('payments_end') |
---|
| 103 | if payments_start and payments_end: |
---|
| 104 | # Payment period given |
---|
| 105 | payments_start = datetime.strptime(payments_start, date_format) |
---|
| 106 | payments_end = datetime.strptime(payments_end, date_format) |
---|
[11757] | 107 | tz = getUtility(IKofaUtils).tzinfo |
---|
| 108 | payments_start = tz.localize(payments_start) |
---|
| 109 | payments_end = tz.localize(payments_end) |
---|
[11730] | 110 | if paid: |
---|
| 111 | # Only paid tickets in payment period are considered |
---|
| 112 | for student in students: |
---|
| 113 | for payment in student.get('payments', {}).values(): |
---|
[11757] | 114 | if payment.payment_date and payment.p_state == 'paid': |
---|
| 115 | payment_date = to_timezone(payment.payment_date, tz) |
---|
| 116 | if payment_date > payments_start and \ |
---|
| 117 | payment_date < payments_end: |
---|
| 118 | payments.append(payment) |
---|
[11730] | 119 | else: |
---|
| 120 | # All tickets in payment period are considered |
---|
| 121 | for student in students: |
---|
| 122 | for payment in student.get('payments', {}).values(): |
---|
[11757] | 123 | if payment.payment_date: |
---|
| 124 | payment_date = to_timezone(payment.payment_date, tz) |
---|
| 125 | if payment_date > payments_start and \ |
---|
| 126 | payment_date < payments_end: |
---|
| 127 | payments.append(payment) |
---|
[11730] | 128 | else: |
---|
| 129 | # Payment period not given |
---|
| 130 | if paid: |
---|
| 131 | # Only paid tickets are considered |
---|
| 132 | for student in students: |
---|
| 133 | for payment in student.get('payments', {}).values(): |
---|
| 134 | if payment.p_state == 'paid': |
---|
| 135 | payments.append(payment) |
---|
| 136 | else: |
---|
| 137 | # All tickets are considered |
---|
| 138 | for student in students: |
---|
| 139 | for payment in student.get('payments', {}).values(): |
---|
| 140 | payments.append(payment) |
---|
[10296] | 141 | return payments |
---|
| 142 | |
---|
[9427] | 143 | def get_bedtickets(students): |
---|
| 144 | """Get all bedtickets of `students`. |
---|
| 145 | """ |
---|
| 146 | tickets = [] |
---|
| 147 | for student in students: |
---|
| 148 | for ticket in student.get('accommodation', {}).values(): |
---|
| 149 | tickets.append(ticket) |
---|
| 150 | return tickets |
---|
[8414] | 151 | |
---|
| 152 | class StudentExporterBase(ExporterBase): |
---|
| 153 | """Exporter for students or related objects. |
---|
| 154 | |
---|
| 155 | This is a baseclass. |
---|
| 156 | """ |
---|
| 157 | grok.baseclass() |
---|
[8411] | 158 | grok.implements(ICSVStudentExporter) |
---|
| 159 | grok.provides(ICSVStudentExporter) |
---|
[8414] | 160 | |
---|
[9844] | 161 | def filter_func(self, x, **kw): |
---|
[9802] | 162 | return x |
---|
[9797] | 163 | |
---|
[9801] | 164 | def get_filtered(self, site, **kw): |
---|
[9843] | 165 | """Get students from a catalog filtered by keywords. |
---|
[9801] | 166 | |
---|
[9843] | 167 | students_catalog is the default catalog. The keys must be valid |
---|
[9933] | 168 | catalog index names. |
---|
[9843] | 169 | Returns a simple empty list, a list with `Student` |
---|
| 170 | objects or a catalog result set with `Student` |
---|
[9801] | 171 | objects. |
---|
| 172 | |
---|
| 173 | .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog` |
---|
| 174 | |
---|
| 175 | """ |
---|
[9843] | 176 | # Pass only given keywords to create FilteredCatalogQuery objects. |
---|
| 177 | # This way we avoid |
---|
[9801] | 178 | # trouble with `None` value ambivalences and queries are also |
---|
| 179 | # faster (normally less indexes to ask). Drawback is, that |
---|
| 180 | # developers must look into catalog to see what keywords are |
---|
| 181 | # valid. |
---|
[9845] | 182 | if kw.get('catalog', None) == 'coursetickets': |
---|
[9843] | 183 | coursetickets = CourseTicketsQuery(**kw).query() |
---|
| 184 | students = [] |
---|
| 185 | for ticket in coursetickets: |
---|
| 186 | students.append(ticket.student) |
---|
| 187 | return list(set(students)) |
---|
[11730] | 188 | # Payments can be filtered by payment_date. The period boundaries |
---|
| 189 | # are not keys of the catalog and must thus be removed from kw. |
---|
| 190 | try: |
---|
| 191 | del kw['payments_start'] |
---|
| 192 | del kw['payments_end'] |
---|
| 193 | except KeyError: |
---|
| 194 | pass |
---|
[9801] | 195 | query = StudentsQuery(**kw) |
---|
[9797] | 196 | return query.query() |
---|
| 197 | |
---|
[8414] | 198 | def export(self, values, filepath=None): |
---|
| 199 | """Export `values`, an iterable, as CSV file. |
---|
| 200 | |
---|
| 201 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 202 | """ |
---|
| 203 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 204 | for value in values: |
---|
| 205 | self.write_item(value, writer) |
---|
| 206 | return self.close_outfile(filepath, outfile) |
---|
| 207 | |
---|
[9797] | 208 | def export_all(self, site, filepath=None): |
---|
| 209 | """Export students into filepath as CSV data. |
---|
| 210 | |
---|
| 211 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
[9763] | 212 | """ |
---|
[9802] | 213 | return self.export(self.filter_func(get_students(site)), filepath) |
---|
[8414] | 214 | |
---|
[9797] | 215 | def export_student(self, student, filepath=None): |
---|
| 216 | return self.export(self.filter_func([student]), filepath=filepath) |
---|
[9734] | 217 | |
---|
[9802] | 218 | def export_filtered(self, site, filepath=None, **kw): |
---|
| 219 | """Export items denoted by `kw`. |
---|
[9797] | 220 | |
---|
[9802] | 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 | |
---|
| 227 | |
---|
[8414] | 228 | class StudentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 229 | """Exporter for Students. |
---|
| 230 | """ |
---|
[7944] | 231 | grok.name('students') |
---|
| 232 | |
---|
| 233 | #: Fieldnames considered by this exporter |
---|
[9936] | 234 | fields = tuple(sorted(iface_names(IStudent))) + ( |
---|
[9253] | 235 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
| 236 | 'current_level', 'current_session') |
---|
[7944] | 237 | |
---|
| 238 | #: The title under which this exporter will be displayed |
---|
| 239 | title = _(u'Students') |
---|
| 240 | |
---|
[8493] | 241 | def mangle_value(self, value, name, context=None): |
---|
| 242 | if name == 'history': |
---|
| 243 | value = value.messages |
---|
[8971] | 244 | if name == 'phone' and value is not None: |
---|
| 245 | # Append hash '#' to phone numbers to circumvent |
---|
| 246 | # unwanted excel automatic |
---|
[8947] | 247 | value = str('%s#' % value) |
---|
[8493] | 248 | return super( |
---|
| 249 | StudentsExporter, self).mangle_value( |
---|
| 250 | value, name, context=context) |
---|
| 251 | |
---|
[7944] | 252 | |
---|
[8414] | 253 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[7994] | 254 | """Exporter for StudentStudyCourses. |
---|
| 255 | """ |
---|
| 256 | grok.name('studentstudycourses') |
---|
| 257 | |
---|
| 258 | #: Fieldnames considered by this exporter |
---|
[8493] | 259 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
[7994] | 260 | |
---|
| 261 | #: The title under which this exporter will be displayed |
---|
| 262 | title = _(u'Student Study Courses') |
---|
| 263 | |
---|
[9844] | 264 | def filter_func(self, x, **kw): |
---|
[9797] | 265 | return get_studycourses(x) |
---|
| 266 | |
---|
[7994] | 267 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 268 | """Treat location values special. |
---|
[7994] | 269 | """ |
---|
| 270 | if name == 'certificate' and value is not None: |
---|
| 271 | # XXX: hopefully cert codes are unique site-wide |
---|
| 272 | value = value.code |
---|
[8493] | 273 | if name == 'student_id' and context is not None: |
---|
[8736] | 274 | student = context.student |
---|
[8493] | 275 | value = getattr(student, name, None) |
---|
[7994] | 276 | return super( |
---|
| 277 | StudentStudyCourseExporter, self).mangle_value( |
---|
| 278 | value, name, context=context) |
---|
| 279 | |
---|
| 280 | |
---|
[8414] | 281 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8015] | 282 | """Exporter for StudentStudyLevels. |
---|
| 283 | """ |
---|
| 284 | grok.name('studentstudylevels') |
---|
| 285 | |
---|
| 286 | #: Fieldnames considered by this exporter |
---|
[8493] | 287 | fields = tuple(sorted(iface_names( |
---|
[9253] | 288 | IStudentStudyLevel) + ['level'])) + ( |
---|
| 289 | 'student_id', 'number_of_tickets','certcode') |
---|
[8015] | 290 | |
---|
| 291 | #: The title under which this exporter will be displayed |
---|
| 292 | title = _(u'Student Study Levels') |
---|
| 293 | |
---|
[9844] | 294 | def filter_func(self, x, **kw): |
---|
[9802] | 295 | return get_levels(x) |
---|
| 296 | |
---|
[8015] | 297 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 298 | """Treat location values special. |
---|
[8015] | 299 | """ |
---|
[8493] | 300 | if name == 'student_id' and context is not None: |
---|
[8736] | 301 | student = context.student |
---|
[8493] | 302 | value = getattr(student, name, None) |
---|
[8015] | 303 | return super( |
---|
| 304 | StudentStudyLevelExporter, self).mangle_value( |
---|
| 305 | value, name, context=context) |
---|
| 306 | |
---|
[8414] | 307 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8342] | 308 | """Exporter for CourseTickets. |
---|
| 309 | """ |
---|
| 310 | grok.name('coursetickets') |
---|
| 311 | |
---|
| 312 | #: Fieldnames considered by this exporter |
---|
[8493] | 313 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
[11484] | 314 | ['level', 'code', 'level_session'])) + ('student_id', |
---|
| 315 | 'certcode', 'display_fullname') |
---|
[8342] | 316 | |
---|
| 317 | #: The title under which this exporter will be displayed |
---|
| 318 | title = _(u'Course Tickets') |
---|
| 319 | |
---|
[9844] | 320 | def filter_func(self, x, **kw): |
---|
| 321 | return get_tickets(x, **kw) |
---|
[9802] | 322 | |
---|
[8342] | 323 | def mangle_value(self, value, name, context=None): |
---|
| 324 | """Treat location values special. |
---|
| 325 | """ |
---|
| 326 | if context is not None: |
---|
[8736] | 327 | student = context.student |
---|
[11484] | 328 | if name in ('student_id', 'display_fullname') and student is not None: |
---|
[8342] | 329 | value = getattr(student, name, None) |
---|
[11484] | 330 | #if name == 'level': |
---|
| 331 | # value = getattr(context, 'level', lambda: None) |
---|
| 332 | #if name == 'level_session': |
---|
| 333 | # value = getattr(context, 'level_session', lambda: None) |
---|
[8342] | 334 | return super( |
---|
| 335 | CourseTicketExporter, self).mangle_value( |
---|
| 336 | value, name, context=context) |
---|
| 337 | |
---|
| 338 | |
---|
[9832] | 339 | class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8371] | 340 | """Exporter for OnlinePayment instances. |
---|
| 341 | """ |
---|
| 342 | grok.name('studentpayments') |
---|
| 343 | |
---|
| 344 | #: Fieldnames considered by this exporter |
---|
| 345 | fields = tuple( |
---|
[8493] | 346 | sorted(iface_names( |
---|
[9984] | 347 | IStudentOnlinePayment, exclude_attribs=False, |
---|
| 348 | omit=['display_item']))) + ( |
---|
[10232] | 349 | 'student_id','state','current_session') |
---|
[8371] | 350 | |
---|
| 351 | #: The title under which this exporter will be displayed |
---|
[8576] | 352 | title = _(u'Student Payments') |
---|
[8371] | 353 | |
---|
[9844] | 354 | def filter_func(self, x, **kw): |
---|
[11730] | 355 | return get_payments(x, **kw) |
---|
[9802] | 356 | |
---|
[8371] | 357 | def mangle_value(self, value, name, context=None): |
---|
| 358 | """Treat location values special. |
---|
| 359 | """ |
---|
| 360 | if context is not None: |
---|
[8736] | 361 | student = context.student |
---|
[10232] | 362 | if name in ['student_id','state', |
---|
| 363 | 'current_session'] and student is not None: |
---|
[8371] | 364 | value = getattr(student, name, None) |
---|
| 365 | return super( |
---|
[9832] | 366 | StudentPaymentsExporter, self).mangle_value( |
---|
[8371] | 367 | value, name, context=context) |
---|
| 368 | |
---|
[10233] | 369 | class DataForBursaryExporter(StudentPaymentsExporter): |
---|
| 370 | """Exporter for OnlinePayment instances. |
---|
| 371 | """ |
---|
| 372 | grok.name('bursary') |
---|
| 373 | |
---|
[10296] | 374 | def filter_func(self, x, **kw): |
---|
[11730] | 375 | return get_payments(x, paid=True, **kw) |
---|
[10296] | 376 | |
---|
[10233] | 377 | #: Fieldnames considered by this exporter |
---|
| 378 | fields = tuple( |
---|
| 379 | sorted(iface_names( |
---|
| 380 | IStudentOnlinePayment, exclude_attribs=False, |
---|
| 381 | omit=['display_item']))) + ( |
---|
[11702] | 382 | 'student_id','matric_number','reg_number', |
---|
[10236] | 383 | 'firstname', 'middlename', 'lastname', |
---|
| 384 | 'state','current_session', |
---|
| 385 | 'entry_session', 'entry_mode', |
---|
[11702] | 386 | 'faccode', 'depcode','certcode') |
---|
[10233] | 387 | |
---|
| 388 | #: The title under which this exporter will be displayed |
---|
| 389 | title = _(u'Payment Data for Bursary') |
---|
| 390 | |
---|
| 391 | def mangle_value(self, value, name, context=None): |
---|
| 392 | """Treat location values special. |
---|
| 393 | """ |
---|
| 394 | if context is not None: |
---|
| 395 | student = context.student |
---|
[10236] | 396 | if name in [ |
---|
[11702] | 397 | 'student_id','matric_number', 'reg_number', |
---|
[10236] | 398 | 'firstname', 'middlename', 'lastname', |
---|
[11702] | 399 | 'state', 'current_session', |
---|
[10236] | 400 | 'entry_session', 'entry_mode', |
---|
[11702] | 401 | 'faccode', 'depcode', 'certcode'] and student is not None: |
---|
[10233] | 402 | value = getattr(student, name, None) |
---|
| 403 | return super( |
---|
| 404 | StudentPaymentsExporter, self).mangle_value( |
---|
| 405 | value, name, context=context) |
---|
| 406 | |
---|
[9427] | 407 | class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 408 | """Exporter for BedTicket instances. |
---|
| 409 | """ |
---|
| 410 | grok.name('bedtickets') |
---|
| 411 | |
---|
| 412 | #: Fieldnames considered by this exporter |
---|
| 413 | fields = tuple( |
---|
| 414 | sorted(iface_names( |
---|
[9984] | 415 | IBedTicket, exclude_attribs=False, |
---|
| 416 | omit=['display_coordinates']))) + ( |
---|
| 417 | 'student_id', 'actual_bed_type') |
---|
[9427] | 418 | |
---|
| 419 | #: The title under which this exporter will be displayed |
---|
| 420 | title = _(u'Bed Tickets') |
---|
| 421 | |
---|
[9844] | 422 | def filter_func(self, x, **kw): |
---|
[9802] | 423 | return get_bedtickets(x) |
---|
| 424 | |
---|
[9427] | 425 | def mangle_value(self, value, name, context=None): |
---|
| 426 | """Treat location values and others special. |
---|
| 427 | """ |
---|
| 428 | if context is not None: |
---|
| 429 | student = context.student |
---|
| 430 | if name in ['student_id'] and student is not None: |
---|
| 431 | value = getattr(student, name, None) |
---|
| 432 | if name == 'bed' and value is not None: |
---|
| 433 | value = getattr(value, 'bed_id', None) |
---|
| 434 | if name == 'actual_bed_type': |
---|
| 435 | value = getattr(getattr(context, 'bed', None), 'bed_type') |
---|
| 436 | return super( |
---|
| 437 | BedTicketsExporter, self).mangle_value( |
---|
| 438 | value, name, context=context) |
---|
| 439 | |
---|
[9574] | 440 | class StudentPaymentsOverviewExporter(StudentsExporter): |
---|
| 441 | """Exporter for students with payment overview. |
---|
| 442 | """ |
---|
| 443 | grok.name('paymentsoverview') |
---|
| 444 | |
---|
| 445 | curr_year = datetime.now().year |
---|
| 446 | year_range = range(curr_year - 9, curr_year + 1) |
---|
| 447 | year_range_tuple = tuple([str(year) for year in year_range]) |
---|
| 448 | |
---|
| 449 | #: Fieldnames considered by this exporter |
---|
[9983] | 450 | fields = ('student_id', 'matric_number', 'display_fullname', |
---|
[9574] | 451 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9807] | 452 | 'current_level', 'current_session', 'current_mode', |
---|
[9574] | 453 | ) + year_range_tuple |
---|
| 454 | |
---|
| 455 | #: The title under which this exporter will be displayed |
---|
| 456 | title = _(u'Student Payments Overview') |
---|
| 457 | |
---|
| 458 | def mangle_value(self, value, name, context=None): |
---|
| 459 | if name in self.year_range_tuple and context is not None: |
---|
[11661] | 460 | value = 0 |
---|
[9574] | 461 | for ticket in context['payments'].values(): |
---|
| 462 | if ticket.p_state == 'paid' and \ |
---|
| 463 | ticket.p_category == 'schoolfee' and \ |
---|
| 464 | ticket.p_session == int(name): |
---|
[11662] | 465 | try: |
---|
| 466 | value += ticket.amount_auth |
---|
| 467 | except TypeError: |
---|
| 468 | pass |
---|
[11661] | 469 | if value == 0: |
---|
| 470 | value = '' |
---|
[9574] | 471 | return super( |
---|
| 472 | StudentsExporter, self).mangle_value( |
---|
[9734] | 473 | value, name, context=context) |
---|
[9744] | 474 | |
---|
| 475 | class StudentStudyLevelsOverviewExporter(StudentsExporter): |
---|
| 476 | """Exporter for students with study level overview. |
---|
| 477 | """ |
---|
| 478 | grok.name('studylevelsoverview') |
---|
| 479 | |
---|
[9787] | 480 | avail_levels = tuple([str(x) for x in study_levels(None)]) |
---|
[9744] | 481 | |
---|
| 482 | #: Fieldnames considered by this exporter |
---|
| 483 | fields = ('student_id', ) + ( |
---|
| 484 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9761] | 485 | 'entry_session', 'current_level', 'current_session', |
---|
[9787] | 486 | ) + avail_levels |
---|
[9744] | 487 | |
---|
| 488 | #: The title under which this exporter will be displayed |
---|
| 489 | title = _(u'Student Study Levels Overview') |
---|
| 490 | |
---|
| 491 | def mangle_value(self, value, name, context=None): |
---|
[9787] | 492 | if name in self.avail_levels and context is not None: |
---|
[9744] | 493 | value = '' |
---|
| 494 | for level in context['studycourse'].values(): |
---|
| 495 | if level.level == int(name): |
---|
[9761] | 496 | #value = '%s|%s|%s|%s' % ( |
---|
| 497 | # level.level_session, |
---|
| 498 | # len(level), |
---|
| 499 | # level.validated_by, |
---|
| 500 | # level.level_verdict) |
---|
| 501 | value = '%s' % level.level_session |
---|
[9744] | 502 | break |
---|
| 503 | return super( |
---|
| 504 | StudentsExporter, self).mangle_value( |
---|
| 505 | value, name, context=context) |
---|
[9936] | 506 | |
---|
| 507 | class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 508 | """Exporter for Interswitch Combo Card Data. |
---|
| 509 | """ |
---|
| 510 | grok.name('combocard') |
---|
| 511 | |
---|
| 512 | #: Fieldnames considered by this exporter |
---|
| 513 | fields = ('display_fullname', |
---|
[9937] | 514 | 'student_id','matric_number', |
---|
| 515 | 'certificate', 'faculty', 'department', 'passport_path') |
---|
[9936] | 516 | |
---|
| 517 | #: The title under which this exporter will be displayed |
---|
| 518 | title = _(u'Combo Card Data') |
---|
| 519 | |
---|
| 520 | def mangle_value(self, value, name, context=None): |
---|
| 521 | certificate = context['studycourse'].certificate |
---|
| 522 | if name == 'certificate' and certificate is not None: |
---|
| 523 | value = certificate.title |
---|
| 524 | if name == 'department' and certificate is not None: |
---|
[10650] | 525 | value = certificate.__parent__.__parent__.longtitle |
---|
[9936] | 526 | if name == 'faculty' and certificate is not None: |
---|
[10650] | 527 | value = certificate.__parent__.__parent__.__parent__.longtitle |
---|
[9937] | 528 | if name == 'passport_path' and certificate is not None: |
---|
| 529 | file_id = IFileStoreNameChooser(context).chooseName(attr='passport.jpg') |
---|
| 530 | os_path = getUtility(IExtFileStore)._pathFromFileID(file_id) |
---|
| 531 | if not os.path.exists(os_path): |
---|
| 532 | value = None |
---|
| 533 | else: |
---|
| 534 | value = '/'.join(os_path.split('/')[-4:]) |
---|
[9936] | 535 | return super( |
---|
| 536 | ComboCardDataExporter, self).mangle_value( |
---|
| 537 | value, name, context=context) |
---|