[8057] | 1 | ## $Id: export.py 9834 2013-01-06 08:22: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 | """ |
---|
| 20 | import grok |
---|
[9574] | 21 | from datetime import datetime |
---|
[7944] | 22 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[9787] | 23 | from waeup.kofa.students.catalog import StudentsQuery |
---|
[8015] | 24 | from waeup.kofa.students.interfaces import ( |
---|
[8371] | 25 | IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket, |
---|
[9427] | 26 | IStudentOnlinePayment, ICSVStudentExporter, IBedTicket) |
---|
[9787] | 27 | from waeup.kofa.students.vocabularies import study_levels |
---|
[7944] | 28 | from waeup.kofa.utils.batching import ExporterBase |
---|
| 29 | from waeup.kofa.utils.helpers import iface_names |
---|
| 30 | |
---|
[8400] | 31 | #: A tuple containing all exporter names referring to students or |
---|
| 32 | #: subobjects thereof. |
---|
[9834] | 33 | EXPORTER_NAMES = ('students', 'studentstudycourses', |
---|
| 34 | 'studentstudylevels', 'coursetickets', |
---|
| 35 | 'studentpayments', 'bedtickets', 'paymentsoverview', |
---|
| 36 | 'studylevelsoverview') |
---|
[8400] | 37 | |
---|
[9787] | 38 | def get_students(site, stud_filter=StudentsQuery()): |
---|
[8414] | 39 | """Get all students registered in catalog in `site`. |
---|
[7944] | 40 | """ |
---|
[9787] | 41 | return stud_filter.query() |
---|
[8414] | 42 | |
---|
| 43 | def get_studycourses(students): |
---|
| 44 | """Get studycourses of `students`. |
---|
| 45 | """ |
---|
| 46 | return [x.get('studycourse', None) for x in students |
---|
| 47 | if x is not None] |
---|
| 48 | |
---|
| 49 | def get_levels(students): |
---|
| 50 | """Get all studylevels of `students`. |
---|
| 51 | """ |
---|
| 52 | levels = [] |
---|
| 53 | for course in get_studycourses(students): |
---|
| 54 | for level in course.values(): |
---|
| 55 | levels.append(level) |
---|
| 56 | return levels |
---|
| 57 | |
---|
| 58 | def get_tickets(students): |
---|
| 59 | """Get all course tickets of `students`. |
---|
| 60 | """ |
---|
| 61 | tickets = [] |
---|
| 62 | for level in get_levels(students): |
---|
| 63 | for ticket in level.values(): |
---|
| 64 | tickets.append(ticket) |
---|
| 65 | return tickets |
---|
| 66 | |
---|
| 67 | def get_payments(students): |
---|
| 68 | """Get all payments of `students`. |
---|
| 69 | """ |
---|
| 70 | payments = [] |
---|
| 71 | for student in students: |
---|
| 72 | for payment in student.get('payments', {}).values(): |
---|
| 73 | payments.append(payment) |
---|
| 74 | return payments |
---|
| 75 | |
---|
[9427] | 76 | def get_bedtickets(students): |
---|
| 77 | """Get all bedtickets of `students`. |
---|
| 78 | """ |
---|
| 79 | tickets = [] |
---|
| 80 | for student in students: |
---|
| 81 | for ticket in student.get('accommodation', {}).values(): |
---|
| 82 | tickets.append(ticket) |
---|
| 83 | return tickets |
---|
[8414] | 84 | |
---|
| 85 | class StudentExporterBase(ExporterBase): |
---|
| 86 | """Exporter for students or related objects. |
---|
| 87 | |
---|
| 88 | This is a baseclass. |
---|
| 89 | """ |
---|
| 90 | grok.baseclass() |
---|
[8411] | 91 | grok.implements(ICSVStudentExporter) |
---|
| 92 | grok.provides(ICSVStudentExporter) |
---|
[8414] | 93 | |
---|
[9797] | 94 | def filter_func(self, x): |
---|
[9802] | 95 | return x |
---|
[9797] | 96 | |
---|
[9801] | 97 | def get_filtered(self, site, **kw): |
---|
| 98 | """Get students filtered by keywords. |
---|
| 99 | |
---|
| 100 | The keys must be valid students catalog index names. Returns a |
---|
| 101 | simple empty list or a catalog result set with `Student` |
---|
| 102 | objects. |
---|
| 103 | |
---|
| 104 | .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog` |
---|
| 105 | |
---|
| 106 | """ |
---|
| 107 | # Pass only given keywords to StudentsQuery. This way we avoid |
---|
| 108 | # trouble with `None` value ambivalences and queries are also |
---|
| 109 | # faster (normally less indexes to ask). Drawback is, that |
---|
| 110 | # developers must look into catalog to see what keywords are |
---|
| 111 | # valid. |
---|
| 112 | query = StudentsQuery(**kw) |
---|
[9797] | 113 | return query.query() |
---|
| 114 | |
---|
[8414] | 115 | def export(self, values, filepath=None): |
---|
| 116 | """Export `values`, an iterable, as CSV file. |
---|
| 117 | |
---|
| 118 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 119 | """ |
---|
| 120 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 121 | for value in values: |
---|
| 122 | self.write_item(value, writer) |
---|
| 123 | return self.close_outfile(filepath, outfile) |
---|
| 124 | |
---|
[9797] | 125 | def export_all(self, site, filepath=None): |
---|
| 126 | """Export students into filepath as CSV data. |
---|
| 127 | |
---|
| 128 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
[9763] | 129 | """ |
---|
[9802] | 130 | return self.export(self.filter_func(get_students(site)), filepath) |
---|
[8414] | 131 | |
---|
[9797] | 132 | def export_student(self, student, filepath=None): |
---|
| 133 | return self.export(self.filter_func([student]), filepath=filepath) |
---|
[9734] | 134 | |
---|
[9802] | 135 | def export_filtered(self, site, filepath=None, **kw): |
---|
| 136 | """Export items denoted by `kw`. |
---|
[9797] | 137 | |
---|
[9802] | 138 | If `filepath` is ``None``, a raw string with CSV data should |
---|
| 139 | be returned. |
---|
| 140 | """ |
---|
| 141 | data = self.get_filtered(site, **kw) |
---|
| 142 | return self.export(self.filter_func(data), filepath=filepath) |
---|
| 143 | |
---|
| 144 | |
---|
[8414] | 145 | class StudentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 146 | """Exporter for Students. |
---|
| 147 | """ |
---|
[7944] | 148 | grok.name('students') |
---|
| 149 | |
---|
| 150 | #: Fieldnames considered by this exporter |
---|
[8493] | 151 | fields = tuple(sorted(iface_names( |
---|
| 152 | IStudent, omit=['loggerInfo']))) + ( |
---|
[9253] | 153 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
| 154 | 'current_level', 'current_session') |
---|
[7944] | 155 | |
---|
| 156 | #: The title under which this exporter will be displayed |
---|
| 157 | title = _(u'Students') |
---|
| 158 | |
---|
[8493] | 159 | def mangle_value(self, value, name, context=None): |
---|
| 160 | if name == 'history': |
---|
| 161 | value = value.messages |
---|
[8971] | 162 | if name == 'phone' and value is not None: |
---|
| 163 | # Append hash '#' to phone numbers to circumvent |
---|
| 164 | # unwanted excel automatic |
---|
[8947] | 165 | value = str('%s#' % value) |
---|
[8493] | 166 | return super( |
---|
| 167 | StudentsExporter, self).mangle_value( |
---|
| 168 | value, name, context=context) |
---|
| 169 | |
---|
[7944] | 170 | |
---|
[8414] | 171 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[7994] | 172 | """Exporter for StudentStudyCourses. |
---|
| 173 | """ |
---|
| 174 | grok.name('studentstudycourses') |
---|
| 175 | |
---|
| 176 | #: Fieldnames considered by this exporter |
---|
[8493] | 177 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
[7994] | 178 | |
---|
| 179 | #: The title under which this exporter will be displayed |
---|
| 180 | title = _(u'Student Study Courses') |
---|
| 181 | |
---|
[9797] | 182 | def filter_func(self, x): |
---|
| 183 | return get_studycourses(x) |
---|
| 184 | |
---|
[7994] | 185 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 186 | """Treat location values special. |
---|
[7994] | 187 | """ |
---|
| 188 | if name == 'certificate' and value is not None: |
---|
| 189 | # XXX: hopefully cert codes are unique site-wide |
---|
| 190 | value = value.code |
---|
[8493] | 191 | if name == 'student_id' and context is not None: |
---|
[8736] | 192 | student = context.student |
---|
[8493] | 193 | value = getattr(student, name, None) |
---|
[7994] | 194 | return super( |
---|
| 195 | StudentStudyCourseExporter, self).mangle_value( |
---|
| 196 | value, name, context=context) |
---|
| 197 | |
---|
| 198 | |
---|
[8414] | 199 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8015] | 200 | """Exporter for StudentStudyLevels. |
---|
| 201 | """ |
---|
| 202 | grok.name('studentstudylevels') |
---|
| 203 | |
---|
| 204 | #: Fieldnames considered by this exporter |
---|
[8493] | 205 | fields = tuple(sorted(iface_names( |
---|
[9253] | 206 | IStudentStudyLevel) + ['level'])) + ( |
---|
| 207 | 'student_id', 'number_of_tickets','certcode') |
---|
[8015] | 208 | |
---|
| 209 | #: The title under which this exporter will be displayed |
---|
| 210 | title = _(u'Student Study Levels') |
---|
| 211 | |
---|
[9802] | 212 | def filter_func(self, x): |
---|
| 213 | return get_levels(x) |
---|
| 214 | |
---|
[8015] | 215 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 216 | """Treat location values special. |
---|
[8015] | 217 | """ |
---|
[8493] | 218 | if name == 'student_id' and context is not None: |
---|
[8736] | 219 | student = context.student |
---|
[8493] | 220 | value = getattr(student, name, None) |
---|
[8015] | 221 | return super( |
---|
| 222 | StudentStudyLevelExporter, self).mangle_value( |
---|
| 223 | value, name, context=context) |
---|
| 224 | |
---|
[8414] | 225 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8342] | 226 | """Exporter for CourseTickets. |
---|
| 227 | """ |
---|
| 228 | grok.name('coursetickets') |
---|
| 229 | |
---|
| 230 | #: Fieldnames considered by this exporter |
---|
[8493] | 231 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
[9420] | 232 | ['level', 'code'])) + ('student_id', 'certcode') |
---|
[8342] | 233 | |
---|
| 234 | #: The title under which this exporter will be displayed |
---|
| 235 | title = _(u'Course Tickets') |
---|
| 236 | |
---|
[9802] | 237 | def filter_func(self, x): |
---|
| 238 | return get_tickets(x) |
---|
| 239 | |
---|
[8342] | 240 | def mangle_value(self, value, name, context=None): |
---|
| 241 | """Treat location values special. |
---|
| 242 | """ |
---|
| 243 | if context is not None: |
---|
[8736] | 244 | student = context.student |
---|
[8493] | 245 | if name == 'student_id' and student is not None: |
---|
[8342] | 246 | value = getattr(student, name, None) |
---|
| 247 | if name == 'level': |
---|
[8393] | 248 | value = getattr(context, 'getLevel', lambda: None)() |
---|
[8342] | 249 | return super( |
---|
| 250 | CourseTicketExporter, self).mangle_value( |
---|
| 251 | value, name, context=context) |
---|
| 252 | |
---|
| 253 | |
---|
[9832] | 254 | class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8371] | 255 | """Exporter for OnlinePayment instances. |
---|
| 256 | """ |
---|
| 257 | grok.name('studentpayments') |
---|
| 258 | |
---|
| 259 | #: Fieldnames considered by this exporter |
---|
| 260 | fields = tuple( |
---|
[8493] | 261 | sorted(iface_names( |
---|
[9258] | 262 | IStudentOnlinePayment, exclude_attribs=False))) + ( |
---|
[9278] | 263 | 'student_id','student_state','current_session') |
---|
[8371] | 264 | |
---|
| 265 | #: The title under which this exporter will be displayed |
---|
[8576] | 266 | title = _(u'Student Payments') |
---|
[8371] | 267 | |
---|
[9802] | 268 | def filter_func(self, x): |
---|
| 269 | return get_payments(x) |
---|
| 270 | |
---|
[8371] | 271 | def mangle_value(self, value, name, context=None): |
---|
| 272 | """Treat location values special. |
---|
| 273 | """ |
---|
| 274 | if context is not None: |
---|
[8736] | 275 | student = context.student |
---|
[8493] | 276 | if name in ['student_id'] and student is not None: |
---|
[8371] | 277 | value = getattr(student, name, None) |
---|
| 278 | return super( |
---|
[9832] | 279 | StudentPaymentsExporter, self).mangle_value( |
---|
[8371] | 280 | value, name, context=context) |
---|
| 281 | |
---|
[9427] | 282 | class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 283 | """Exporter for BedTicket instances. |
---|
| 284 | """ |
---|
| 285 | grok.name('bedtickets') |
---|
| 286 | |
---|
| 287 | #: Fieldnames considered by this exporter |
---|
| 288 | fields = tuple( |
---|
| 289 | sorted(iface_names( |
---|
| 290 | IBedTicket, exclude_attribs=False))) + ( |
---|
| 291 | 'student_id', 'actual_bed_type') |
---|
| 292 | |
---|
| 293 | #: The title under which this exporter will be displayed |
---|
| 294 | title = _(u'Bed Tickets') |
---|
| 295 | |
---|
[9802] | 296 | def filter_func(self, x): |
---|
| 297 | return get_bedtickets(x) |
---|
| 298 | |
---|
[9427] | 299 | def mangle_value(self, value, name, context=None): |
---|
| 300 | """Treat location values and others special. |
---|
| 301 | """ |
---|
| 302 | if context is not None: |
---|
| 303 | student = context.student |
---|
| 304 | if name in ['student_id'] and student is not None: |
---|
| 305 | value = getattr(student, name, None) |
---|
| 306 | if name == 'bed' and value is not None: |
---|
| 307 | value = getattr(value, 'bed_id', None) |
---|
| 308 | if name == 'actual_bed_type': |
---|
| 309 | value = getattr(getattr(context, 'bed', None), 'bed_type') |
---|
| 310 | return super( |
---|
| 311 | BedTicketsExporter, self).mangle_value( |
---|
| 312 | value, name, context=context) |
---|
| 313 | |
---|
[9574] | 314 | class StudentPaymentsOverviewExporter(StudentsExporter): |
---|
| 315 | """Exporter for students with payment overview. |
---|
| 316 | """ |
---|
| 317 | grok.name('paymentsoverview') |
---|
| 318 | |
---|
| 319 | curr_year = datetime.now().year |
---|
| 320 | year_range = range(curr_year - 9, curr_year + 1) |
---|
| 321 | year_range_tuple = tuple([str(year) for year in year_range]) |
---|
| 322 | |
---|
| 323 | #: Fieldnames considered by this exporter |
---|
| 324 | fields = ('student_id', ) + ( |
---|
| 325 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9807] | 326 | 'current_level', 'current_session', 'current_mode', |
---|
[9574] | 327 | ) + year_range_tuple |
---|
| 328 | |
---|
| 329 | #: The title under which this exporter will be displayed |
---|
| 330 | title = _(u'Student Payments Overview') |
---|
| 331 | |
---|
| 332 | def mangle_value(self, value, name, context=None): |
---|
| 333 | if name in self.year_range_tuple and context is not None: |
---|
| 334 | value = '' |
---|
| 335 | for ticket in context['payments'].values(): |
---|
| 336 | if ticket.p_state == 'paid' and \ |
---|
| 337 | ticket.p_category == 'schoolfee' and \ |
---|
| 338 | ticket.p_session == int(name): |
---|
| 339 | value = ticket.amount_auth |
---|
| 340 | break |
---|
| 341 | return super( |
---|
| 342 | StudentsExporter, self).mangle_value( |
---|
[9734] | 343 | value, name, context=context) |
---|
[9744] | 344 | |
---|
| 345 | class StudentStudyLevelsOverviewExporter(StudentsExporter): |
---|
| 346 | """Exporter for students with study level overview. |
---|
| 347 | """ |
---|
| 348 | grok.name('studylevelsoverview') |
---|
| 349 | |
---|
[9787] | 350 | avail_levels = tuple([str(x) for x in study_levels(None)]) |
---|
[9744] | 351 | |
---|
| 352 | #: Fieldnames considered by this exporter |
---|
| 353 | fields = ('student_id', ) + ( |
---|
| 354 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
[9761] | 355 | 'entry_session', 'current_level', 'current_session', |
---|
[9787] | 356 | ) + avail_levels |
---|
[9744] | 357 | |
---|
| 358 | #: The title under which this exporter will be displayed |
---|
| 359 | title = _(u'Student Study Levels Overview') |
---|
| 360 | |
---|
| 361 | def mangle_value(self, value, name, context=None): |
---|
[9787] | 362 | if name in self.avail_levels and context is not None: |
---|
[9744] | 363 | value = '' |
---|
| 364 | for level in context['studycourse'].values(): |
---|
| 365 | if level.level == int(name): |
---|
[9761] | 366 | #value = '%s|%s|%s|%s' % ( |
---|
| 367 | # level.level_session, |
---|
| 368 | # len(level), |
---|
| 369 | # level.validated_by, |
---|
| 370 | # level.level_verdict) |
---|
| 371 | value = '%s' % level.level_session |
---|
[9744] | 372 | break |
---|
| 373 | return super( |
---|
| 374 | StudentsExporter, self).mangle_value( |
---|
| 375 | value, name, context=context) |
---|