[8057] | 1 | ## $Id: export.py 8971 2012-07-10 21:51:28Z 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, |
---|
[8411] | 26 | IStudentOnlinePayment, ICSVStudentExporter) |
---|
[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 | |
---|
| 78 | |
---|
| 79 | class StudentExporterBase(ExporterBase): |
---|
| 80 | """Exporter for students or related objects. |
---|
| 81 | |
---|
| 82 | This is a baseclass. |
---|
| 83 | """ |
---|
| 84 | grok.baseclass() |
---|
[8411] | 85 | grok.implements(ICSVStudentExporter) |
---|
| 86 | grok.provides(ICSVStudentExporter) |
---|
[8414] | 87 | |
---|
| 88 | def export(self, values, filepath=None): |
---|
| 89 | """Export `values`, an iterable, as CSV file. |
---|
| 90 | |
---|
| 91 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 92 | """ |
---|
| 93 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 94 | for value in values: |
---|
| 95 | self.write_item(value, writer) |
---|
| 96 | return self.close_outfile(filepath, outfile) |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | class StudentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
| 100 | """Exporter for Students. |
---|
| 101 | """ |
---|
[7944] | 102 | grok.name('students') |
---|
| 103 | |
---|
| 104 | #: Fieldnames considered by this exporter |
---|
[8493] | 105 | fields = tuple(sorted(iface_names( |
---|
| 106 | IStudent, omit=['loggerInfo']))) + ( |
---|
| 107 | 'password', 'state', 'history', 'certcode') |
---|
[7944] | 108 | |
---|
| 109 | #: The title under which this exporter will be displayed |
---|
| 110 | title = _(u'Students') |
---|
| 111 | |
---|
[8493] | 112 | def mangle_value(self, value, name, context=None): |
---|
| 113 | if name == 'history': |
---|
| 114 | value = value.messages |
---|
[8971] | 115 | if name == 'phone' and value is not None: |
---|
| 116 | # Append hash '#' to phone numbers to circumvent |
---|
| 117 | # unwanted excel automatic |
---|
[8947] | 118 | value = str('%s#' % value) |
---|
[8493] | 119 | return super( |
---|
| 120 | StudentsExporter, self).mangle_value( |
---|
| 121 | value, name, context=context) |
---|
| 122 | |
---|
[7944] | 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. |
---|
| 127 | """ |
---|
[8414] | 128 | return self.export(get_students(site), filepath) |
---|
[7994] | 129 | |
---|
[8411] | 130 | def export_student(self, student, filepath=None): |
---|
| 131 | return self.export([student], filepath=filepath) |
---|
| 132 | |
---|
[8414] | 133 | |
---|
| 134 | class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[7994] | 135 | """Exporter for StudentStudyCourses. |
---|
| 136 | """ |
---|
| 137 | grok.name('studentstudycourses') |
---|
| 138 | |
---|
| 139 | #: Fieldnames considered by this exporter |
---|
[8493] | 140 | fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',) |
---|
[7994] | 141 | |
---|
| 142 | #: The title under which this exporter will be displayed |
---|
| 143 | title = _(u'Student Study Courses') |
---|
| 144 | |
---|
| 145 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 146 | """Treat location values special. |
---|
[7994] | 147 | """ |
---|
| 148 | if name == 'certificate' and value is not None: |
---|
| 149 | # XXX: hopefully cert codes are unique site-wide |
---|
| 150 | value = value.code |
---|
[8493] | 151 | if name == 'student_id' and context is not None: |
---|
[8736] | 152 | student = context.student |
---|
[8493] | 153 | value = getattr(student, name, None) |
---|
[7994] | 154 | return super( |
---|
| 155 | StudentStudyCourseExporter, self).mangle_value( |
---|
| 156 | value, name, context=context) |
---|
| 157 | |
---|
| 158 | def export_all(self, site, filepath=None): |
---|
| 159 | """Export study courses into filepath as CSV data. |
---|
| 160 | |
---|
| 161 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 162 | """ |
---|
[8414] | 163 | return self.export(get_studycourses(get_students(site)), filepath) |
---|
[8015] | 164 | |
---|
[8411] | 165 | def export_student(self, student, filepath=None): |
---|
[8414] | 166 | """Export studycourse of a single student object. |
---|
| 167 | """ |
---|
| 168 | return self.export(get_studycourses([student]), filepath) |
---|
[8411] | 169 | |
---|
| 170 | |
---|
[8414] | 171 | class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8015] | 172 | """Exporter for StudentStudyLevels. |
---|
| 173 | """ |
---|
| 174 | grok.name('studentstudylevels') |
---|
| 175 | |
---|
| 176 | #: Fieldnames considered by this exporter |
---|
[8493] | 177 | fields = tuple(sorted(iface_names( |
---|
| 178 | IStudentStudyLevel) + ['level'])) + ('student_id',) |
---|
[8015] | 179 | |
---|
| 180 | #: The title under which this exporter will be displayed |
---|
| 181 | title = _(u'Student Study Levels') |
---|
| 182 | |
---|
| 183 | def mangle_value(self, value, name, context=None): |
---|
[8493] | 184 | """Treat location values special. |
---|
[8015] | 185 | """ |
---|
[8493] | 186 | if name == 'student_id' and context is not None: |
---|
[8736] | 187 | student = context.student |
---|
[8493] | 188 | value = getattr(student, name, None) |
---|
[8015] | 189 | return super( |
---|
| 190 | StudentStudyLevelExporter, self).mangle_value( |
---|
| 191 | value, name, context=context) |
---|
| 192 | |
---|
| 193 | def export_all(self, site, filepath=None): |
---|
| 194 | """Export study levels into filepath as CSV data. |
---|
| 195 | |
---|
| 196 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 197 | """ |
---|
[8414] | 198 | return self.export(get_levels(get_students(site)), filepath) |
---|
[8342] | 199 | |
---|
[8411] | 200 | def export_student(self, student, filepath=None): |
---|
[8414] | 201 | return self.export(get_levels([student]), filepath) |
---|
[8411] | 202 | |
---|
[8414] | 203 | class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8342] | 204 | """Exporter for CourseTickets. |
---|
| 205 | """ |
---|
| 206 | grok.name('coursetickets') |
---|
| 207 | |
---|
| 208 | #: Fieldnames considered by this exporter |
---|
[8493] | 209 | fields = tuple(sorted(iface_names(ICourseTicket) + |
---|
| 210 | ['level', 'code', 'title', 'credits', |
---|
| 211 | 'passmark', 'semester', 'fcode', 'dcode'])) + ('student_id',) |
---|
[8342] | 212 | |
---|
| 213 | #: The title under which this exporter will be displayed |
---|
| 214 | title = _(u'Course Tickets') |
---|
| 215 | |
---|
| 216 | def mangle_value(self, value, name, context=None): |
---|
| 217 | """Treat location values special. |
---|
| 218 | """ |
---|
| 219 | if context is not None: |
---|
[8736] | 220 | student = context.student |
---|
[8493] | 221 | if name == 'student_id' and student is not None: |
---|
[8342] | 222 | value = getattr(student, name, None) |
---|
| 223 | if name == 'level': |
---|
[8393] | 224 | value = getattr(context, 'getLevel', lambda: None)() |
---|
[8342] | 225 | return super( |
---|
| 226 | CourseTicketExporter, self).mangle_value( |
---|
| 227 | value, name, context=context) |
---|
| 228 | |
---|
| 229 | def export_all(self, site, filepath=None): |
---|
| 230 | """Export course tickets into filepath as CSV data. |
---|
| 231 | |
---|
| 232 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 233 | """ |
---|
[8414] | 234 | return self.export(get_tickets(get_students(site)), filepath) |
---|
[8342] | 235 | |
---|
[8411] | 236 | def export_student(self, student, filepath=None): |
---|
[8414] | 237 | return self.export(get_tickets([student]), filepath) |
---|
[8411] | 238 | |
---|
[8414] | 239 | |
---|
| 240 | class PaymentsExporter(grok.GlobalUtility, StudentExporterBase): |
---|
[8371] | 241 | """Exporter for OnlinePayment instances. |
---|
| 242 | """ |
---|
| 243 | grok.name('studentpayments') |
---|
| 244 | |
---|
| 245 | #: Fieldnames considered by this exporter |
---|
| 246 | fields = tuple( |
---|
[8493] | 247 | sorted(iface_names( |
---|
| 248 | IStudentOnlinePayment, exclude_attribs=False))) + ('student_id',) |
---|
[8371] | 249 | |
---|
| 250 | #: The title under which this exporter will be displayed |
---|
[8576] | 251 | title = _(u'Student Payments') |
---|
[8371] | 252 | |
---|
| 253 | def mangle_value(self, value, name, context=None): |
---|
| 254 | """Treat location values special. |
---|
| 255 | """ |
---|
| 256 | if context is not None: |
---|
[8736] | 257 | student = context.student |
---|
[8493] | 258 | if name in ['student_id'] and student is not None: |
---|
[8371] | 259 | value = getattr(student, name, None) |
---|
| 260 | return super( |
---|
| 261 | PaymentsExporter, self).mangle_value( |
---|
| 262 | value, name, context=context) |
---|
| 263 | |
---|
| 264 | def export_all(self, site, filepath=None): |
---|
| 265 | """Export payments into filepath as CSV data. |
---|
| 266 | |
---|
| 267 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 268 | """ |
---|
[8414] | 269 | return self.export(get_payments(get_students(site)), filepath) |
---|
[8411] | 270 | |
---|
| 271 | def export_student(self, student, filepath=None): |
---|
[8414] | 272 | return self.export(get_payments([student]), filepath) |
---|