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