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