[8057] | 1 | ## $Id: export.py 9574 2012-11-08 13:25:38Z 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 zope.catalog.interfaces import ICatalog |
---|
| 23 | from zope.component import queryUtility |
---|
| 24 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[8015] | 25 | from waeup.kofa.students.interfaces import ( |
---|
[8371] | 26 | IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket, |
---|
[9427] | 27 | IStudentOnlinePayment, ICSVStudentExporter, IBedTicket) |
---|
[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 | |
---|
[8414] | 36 | def get_students(site): |
---|
| 37 | """Get all students registered in catalog in `site`. |
---|
[7944] | 38 | """ |
---|
[8414] | 39 | catalog = queryUtility( |
---|
| 40 | ICatalog, context=site, name='students_catalog', default=None) |
---|
| 41 | if catalog is None: |
---|
| 42 | return [] |
---|
| 43 | students = catalog.searchResults(student_id=(None, None)) |
---|
| 44 | return students |
---|
| 45 | |
---|
| 46 | def get_studycourses(students): |
---|
| 47 | """Get studycourses of `students`. |
---|
| 48 | """ |
---|
| 49 | return [x.get('studycourse', None) for x in students |
---|
| 50 | if x is not None] |
---|
| 51 | |
---|
| 52 | def get_levels(students): |
---|
| 53 | """Get all studylevels of `students`. |
---|
| 54 | """ |
---|
| 55 | levels = [] |
---|
| 56 | for course in get_studycourses(students): |
---|
| 57 | for level in course.values(): |
---|
| 58 | levels.append(level) |
---|
| 59 | return levels |
---|
| 60 | |
---|
| 61 | def get_tickets(students): |
---|
| 62 | """Get all course tickets of `students`. |
---|
| 63 | """ |
---|
| 64 | tickets = [] |
---|
| 65 | for level in get_levels(students): |
---|
| 66 | for ticket in level.values(): |
---|
| 67 | tickets.append(ticket) |
---|
| 68 | return tickets |
---|
| 69 | |
---|
| 70 | def get_payments(students): |
---|
| 71 | """Get all payments of `students`. |
---|
| 72 | """ |
---|
| 73 | payments = [] |
---|
| 74 | for student in students: |
---|
| 75 | for payment in student.get('payments', {}).values(): |
---|
| 76 | payments.append(payment) |
---|
| 77 | return payments |
---|
| 78 | |
---|
[9427] | 79 | def get_bedtickets(students): |
---|
| 80 | """Get all bedtickets of `students`. |
---|
| 81 | """ |
---|
| 82 | tickets = [] |
---|
| 83 | for student in students: |
---|
| 84 | for ticket in student.get('accommodation', {}).values(): |
---|
| 85 | tickets.append(ticket) |
---|
| 86 | return tickets |
---|
[8414] | 87 | |
---|
| 88 | class StudentExporterBase(ExporterBase): |
---|
| 89 | """Exporter for students or related objects. |
---|
| 90 | |
---|
| 91 | This is a baseclass. |
---|
| 92 | """ |
---|
| 93 | grok.baseclass() |
---|
[8411] | 94 | grok.implements(ICSVStudentExporter) |
---|
| 95 | grok.provides(ICSVStudentExporter) |
---|
[8414] | 96 | |
---|
| 97 | def export(self, values, filepath=None): |
---|
| 98 | """Export `values`, an iterable, as CSV file. |
---|
| 99 | |
---|
| 100 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 101 | """ |
---|
| 102 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 103 | for value in values: |
---|
| 104 | self.write_item(value, writer) |
---|
| 105 | return self.close_outfile(filepath, outfile) |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | class StudentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 109 | """Exporter for Students. |
---|
| 110 | """ |
---|
[7944] | 111 | grok.name('students') |
---|
| 112 | |
---|
| 113 | #: Fieldnames considered by this exporter |
---|
[8493] | 114 | fields = tuple(sorted(iface_names( |
---|
| 115 | IStudent, omit=['loggerInfo']))) + ( |
---|
[9253] | 116 | 'password', 'state', 'history', 'certcode', 'is_postgrad', |
---|
| 117 | 'current_level', 'current_session') |
---|
[7944] | 118 | |
---|
| 119 | #: The title under which this exporter will be displayed |
---|
| 120 | title = _(u'Students') |
---|
| 121 | |
---|
[8493] | 122 | def mangle_value(self, value, name, context=None): |
---|
| 123 | if name == 'history': |
---|
| 124 | value = value.messages |
---|
[8971] | 125 | if name == 'phone' and value is not None: |
---|
| 126 | # Append hash '#' to phone numbers to circumvent |
---|
| 127 | # unwanted excel automatic |
---|
[8947] | 128 | value = str('%s#' % value) |
---|
[8493] | 129 | return super( |
---|
| 130 | StudentsExporter, self).mangle_value( |
---|
| 131 | value, name, context=context) |
---|
| 132 | |
---|
[7944] | 133 | def export_all(self, site, filepath=None): |
---|
| 134 | """Export students into filepath as CSV data. |
---|
| 135 | |
---|
| 136 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 137 | """ |
---|
[8414] | 138 | return self.export(get_students(site), filepath) |
---|
[7994] | 139 | |
---|
[8411] | 140 | def export_student(self, student, filepath=None): |
---|
| 141 | return self.export([student], filepath=filepath) |
---|
| 142 | |
---|
[8414] | 143 | |
---|
| 144 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[7994] | 145 | """Exporter for StudentStudyCourses. |
---|
| 146 | """ |
---|
| 147 | grok.name('studentstudycourses') |
---|
| 148 | |
---|
| 149 | #: Fieldnames considered by this exporter |
---|
[8493] | 150 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
[7994] | 151 | |
---|
| 152 | #: The title under which this exporter will be displayed |
---|
| 153 | title = _(u'Student Study Courses') |
---|
| 154 | |
---|
| 155 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 156 | """Treat location values special. |
---|
[7994] | 157 | """ |
---|
| 158 | if name == 'certificate' and value is not None: |
---|
| 159 | # XXX: hopefully cert codes are unique site-wide |
---|
| 160 | value = value.code |
---|
[8493] | 161 | if name == 'student_id' and context is not None: |
---|
[8736] | 162 | student = context.student |
---|
[8493] | 163 | value = getattr(student, name, None) |
---|
[7994] | 164 | return super( |
---|
| 165 | StudentStudyCourseExporter, self).mangle_value( |
---|
| 166 | value, name, context=context) |
---|
| 167 | |
---|
| 168 | def export_all(self, site, filepath=None): |
---|
| 169 | """Export study courses into filepath as CSV data. |
---|
| 170 | |
---|
| 171 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 172 | """ |
---|
[8414] | 173 | return self.export(get_studycourses(get_students(site)), filepath) |
---|
[8015] | 174 | |
---|
[8411] | 175 | def export_student(self, student, filepath=None): |
---|
[8414] | 176 | """Export studycourse of a single student object. |
---|
| 177 | """ |
---|
| 178 | return self.export(get_studycourses([student]), filepath) |
---|
[8411] | 179 | |
---|
| 180 | |
---|
[8414] | 181 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8015] | 182 | """Exporter for StudentStudyLevels. |
---|
| 183 | """ |
---|
| 184 | grok.name('studentstudylevels') |
---|
| 185 | |
---|
| 186 | #: Fieldnames considered by this exporter |
---|
[8493] | 187 | fields = tuple(sorted(iface_names( |
---|
[9253] | 188 | IStudentStudyLevel) + ['level'])) + ( |
---|
| 189 | 'student_id', 'number_of_tickets','certcode') |
---|
[8015] | 190 | |
---|
| 191 | #: The title under which this exporter will be displayed |
---|
| 192 | title = _(u'Student Study Levels') |
---|
| 193 | |
---|
| 194 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 195 | """Treat location values special. |
---|
[8015] | 196 | """ |
---|
[8493] | 197 | if name == 'student_id' and context is not None: |
---|
[8736] | 198 | student = context.student |
---|
[8493] | 199 | value = getattr(student, name, None) |
---|
[8015] | 200 | return super( |
---|
| 201 | StudentStudyLevelExporter, self).mangle_value( |
---|
| 202 | value, name, context=context) |
---|
| 203 | |
---|
| 204 | def export_all(self, site, filepath=None): |
---|
| 205 | """Export study levels into filepath as CSV data. |
---|
| 206 | |
---|
| 207 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 208 | """ |
---|
[8414] | 209 | return self.export(get_levels(get_students(site)), filepath) |
---|
[8342] | 210 | |
---|
[8411] | 211 | def export_student(self, student, filepath=None): |
---|
[8414] | 212 | return self.export(get_levels([student]), filepath) |
---|
[8411] | 213 | |
---|
[8414] | 214 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8342] | 215 | """Exporter for CourseTickets. |
---|
| 216 | """ |
---|
| 217 | grok.name('coursetickets') |
---|
| 218 | |
---|
| 219 | #: Fieldnames considered by this exporter |
---|
[8493] | 220 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
[9420] | 221 | ['level', 'code'])) + ('student_id', 'certcode') |
---|
[8342] | 222 | |
---|
| 223 | #: The title under which this exporter will be displayed |
---|
| 224 | title = _(u'Course Tickets') |
---|
| 225 | |
---|
| 226 | def mangle_value(self, value, name, context=None): |
---|
| 227 | """Treat location values special. |
---|
| 228 | """ |
---|
| 229 | if context is not None: |
---|
[8736] | 230 | student = context.student |
---|
[8493] | 231 | if name == 'student_id' and student is not None: |
---|
[8342] | 232 | value = getattr(student, name, None) |
---|
| 233 | if name == 'level': |
---|
[8393] | 234 | value = getattr(context, 'getLevel', lambda: None)() |
---|
[8342] | 235 | return super( |
---|
| 236 | CourseTicketExporter, self).mangle_value( |
---|
| 237 | value, name, context=context) |
---|
| 238 | |
---|
| 239 | def export_all(self, site, filepath=None): |
---|
| 240 | """Export course tickets into filepath as CSV data. |
---|
| 241 | |
---|
| 242 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 243 | """ |
---|
[8414] | 244 | return self.export(get_tickets(get_students(site)), filepath) |
---|
[8342] | 245 | |
---|
[8411] | 246 | def export_student(self, student, filepath=None): |
---|
[8414] | 247 | return self.export(get_tickets([student]), filepath) |
---|
[8411] | 248 | |
---|
[8414] | 249 | |
---|
| 250 | class PaymentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8371] | 251 | """Exporter for OnlinePayment instances. |
---|
| 252 | """ |
---|
| 253 | grok.name('studentpayments') |
---|
| 254 | |
---|
| 255 | #: Fieldnames considered by this exporter |
---|
| 256 | fields = tuple( |
---|
[8493] | 257 | sorted(iface_names( |
---|
[9258] | 258 | IStudentOnlinePayment, exclude_attribs=False))) + ( |
---|
[9278] | 259 | 'student_id','student_state','current_session') |
---|
[8371] | 260 | |
---|
| 261 | #: The title under which this exporter will be displayed |
---|
[8576] | 262 | title = _(u'Student Payments') |
---|
[8371] | 263 | |
---|
| 264 | def mangle_value(self, value, name, context=None): |
---|
| 265 | """Treat location values special. |
---|
| 266 | """ |
---|
| 267 | if context is not None: |
---|
[8736] | 268 | student = context.student |
---|
[8493] | 269 | if name in ['student_id'] and student is not None: |
---|
[8371] | 270 | value = getattr(student, name, None) |
---|
| 271 | return super( |
---|
| 272 | PaymentsExporter, self).mangle_value( |
---|
| 273 | value, name, context=context) |
---|
| 274 | |
---|
| 275 | def export_all(self, site, filepath=None): |
---|
| 276 | """Export payments into filepath as CSV data. |
---|
| 277 | |
---|
| 278 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 279 | """ |
---|
[8414] | 280 | return self.export(get_payments(get_students(site)), filepath) |
---|
[8411] | 281 | |
---|
| 282 | def export_student(self, student, filepath=None): |
---|
[8414] | 283 | return self.export(get_payments([student]), filepath) |
---|
[9427] | 284 | |
---|
| 285 | class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 286 | """Exporter for BedTicket instances. |
---|
| 287 | """ |
---|
| 288 | grok.name('bedtickets') |
---|
| 289 | |
---|
| 290 | #: Fieldnames considered by this exporter |
---|
| 291 | fields = tuple( |
---|
| 292 | sorted(iface_names( |
---|
| 293 | IBedTicket, exclude_attribs=False))) + ( |
---|
| 294 | 'student_id', 'actual_bed_type') |
---|
| 295 | |
---|
| 296 | #: The title under which this exporter will be displayed |
---|
| 297 | title = _(u'Bed Tickets') |
---|
| 298 | |
---|
| 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 | |
---|
| 314 | def export_all(self, site, filepath=None): |
---|
| 315 | """Export payments into filepath as CSV data. |
---|
| 316 | |
---|
| 317 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 318 | """ |
---|
| 319 | return self.export(get_bedtickets(get_students(site)), filepath) |
---|
| 320 | |
---|
| 321 | def export_student(self, student, filepath=None): |
---|
| 322 | return self.export(get_bedtickets([student]), filepath) |
---|
[9574] | 323 | |
---|
| 324 | class StudentPaymentsOverviewExporter(StudentsExporter): |
---|
| 325 | """Exporter for students with payment overview. |
---|
| 326 | """ |
---|
| 327 | grok.name('paymentsoverview') |
---|
| 328 | |
---|
| 329 | curr_year = datetime.now().year |
---|
| 330 | year_range = range(curr_year - 9, curr_year + 1) |
---|
| 331 | year_range_tuple = tuple([str(year) for year in year_range]) |
---|
| 332 | |
---|
| 333 | #: Fieldnames considered by this exporter |
---|
| 334 | fields = ('student_id', ) + ( |
---|
| 335 | 'state', 'certcode', 'faccode', 'depcode', 'is_postgrad', |
---|
| 336 | 'current_level', 'current_session', |
---|
| 337 | ) + year_range_tuple |
---|
| 338 | |
---|
| 339 | #: The title under which this exporter will be displayed |
---|
| 340 | title = _(u'Student Payments Overview') |
---|
| 341 | |
---|
| 342 | def mangle_value(self, value, name, context=None): |
---|
| 343 | if name in self.year_range_tuple and context is not None: |
---|
| 344 | value = '' |
---|
| 345 | for ticket in context['payments'].values(): |
---|
| 346 | if ticket.p_state == 'paid' and \ |
---|
| 347 | ticket.p_category == 'schoolfee' and \ |
---|
| 348 | ticket.p_session == int(name): |
---|
| 349 | value = ticket.amount_auth |
---|
| 350 | break |
---|
| 351 | return super( |
---|
| 352 | StudentsExporter, self).mangle_value( |
---|
| 353 | value, name, context=context) |
---|