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