source: main/waeup.kofa/trunk/src/waeup/kofa/students/export.py @ 8412

Last change on this file since 8412 was 8411, checked in by uli, 13 years ago

Dirty fix for problem with export of student subobjects.

  • Property svn:keywords set to Id
File size: 12.4 KB
RevLine 
[8057]1## $Id: export.py 8411 2012-05-10 16:38:15Z uli $
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"""
20import grok
21from zope.catalog.interfaces import ICatalog
22from zope.component import queryUtility
23from waeup.kofa.interfaces import MessageFactory as _
[8015]24from waeup.kofa.students.interfaces import (
[8371]25    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
[8411]26    IStudentOnlinePayment, ICSVStudentExporter)
[7944]27from waeup.kofa.utils.batching import ExporterBase
28from waeup.kofa.utils.helpers import iface_names
29
[8400]30#: A tuple containing all exporter names referring to students or
31#: subobjects thereof.
32EXPORTER_NAMES = ('students', 'studentstudycourses', 'studentstudylevels',
33                  'coursetickets', 'studentpayments')
34
[7944]35class StudentsExporter(grok.GlobalUtility, ExporterBase):
[8160]36    """Exporter for Students.
[7944]37    """
[8411]38    grok.implements(ICSVStudentExporter)
39    grok.provides(ICSVStudentExporter)
[7944]40    grok.name('students')
41
42    #: Fieldnames considered by this exporter
43    fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo'])))
44
45    #: The title under which this exporter will be displayed
46    title = _(u'Students')
47
48    def export(self, students, filepath=None):
49        """Export `students`, an iterable, as CSV file.
50
51        If `filepath` is ``None``, a raw string with CSV data is returned.
52        """
53        writer, outfile = self.get_csv_writer(filepath)
54        for student in students:
55            self.write_item(student, writer)
56        return self.close_outfile(filepath, outfile)
57
58    def export_all(self, site, filepath=None):
59        """Export students into filepath as CSV data.
60
61        If `filepath` is ``None``, a raw string with CSV data is returned.
62        """
63        catalog = queryUtility(
64            ICatalog, context=site, name='students_catalog', default=None)
65        if catalog is None:
66            return self.export([], filepath)
67        students = catalog.searchResults(
68            student_id=(None, None))
69        return self.export(students, filepath)
[7994]70
[8411]71    def export_student(self, student, filepath=None):
72        return self.export([student], filepath=filepath)
73
[7994]74class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase):
75    """Exporter for StudentStudyCourses.
76    """
[8411]77    grok.implements(ICSVStudentExporter)
78    grok.provides(ICSVStudentExporter)
[7994]79    grok.name('studentstudycourses')
80
81    #: Fieldnames considered by this exporter
82    fields = tuple(sorted(iface_names(IStudentStudyCourse)))
83
84    #: The title under which this exporter will be displayed
85    title = _(u'Student Study Courses')
86
87    def mangle_value(self, value, name, context=None):
88        """Add the hash symbol at the end of date_of_birth.
89
90        This is to avoid annoying automatic date transformation
91        by Excel or Calc.
92        """
93        if name == 'certificate' and value is not None:
94            # XXX: hopefully cert codes are unique site-wide
95            value = value.code
96        return super(
97            StudentStudyCourseExporter, self).mangle_value(
98            value, name, context=context)
99
100
101    def export(self, study_courses, filepath=None):
102        """Export `study_courses`, an iterable, as CSV file.
103
104        If `filepath` is ``None``, a raw string with CSV data is returned.
105        """
106        writer, outfile = self.get_csv_writer(filepath)
107        for study_course in study_courses:
108            self.write_item(study_course, writer)
109        return self.close_outfile(filepath, outfile)
110
111    def export_all(self, site, filepath=None):
112        """Export study courses into filepath as CSV data.
113
114        If `filepath` is ``None``, a raw string with CSV data is returned.
115        """
116        catalog = queryUtility(
117            ICatalog, context=site, name='students_catalog', default=None)
118        if catalog is None:
119            return self.export([], filepath)
120        students = catalog.searchResults(
121            student_id=(None, None))
122        study_courses = [x.get('studycourse', None) for x in students
123                         if x is not None]
124        return self.export(study_courses, filepath)
[8015]125
[8411]126    def export_student(self, student, filepath=None):
127        if 'studycourse' in student.keys():
128            return self.export([student['studycourse']], filepath=filepath)
129        return self.export([], filepath=filepath)
130
131
[8015]132class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase):
133    """Exporter for StudentStudyLevels.
134    """
[8411]135    grok.implements(ICSVStudentExporter)
136    grok.provides(ICSVStudentExporter)
[8015]137    grok.name('studentstudylevels')
138
139    #: Fieldnames considered by this exporter
140    fields = tuple(sorted(iface_names(IStudentStudyLevel) + [
141        'reg_number', 'matric_number', 'level']))
142
143    #: The title under which this exporter will be displayed
144    title = _(u'Student Study Levels')
145
146    def mangle_value(self, value, name, context=None):
147        """Add the hash symbol at the end of date_of_birth.
148
149        This is to avoid annoying automatic date transformation
150        by Excel or Calc.
151        """
152
153        if name == 'reg_number' and context is not None:
154            value = getattr(
155                getattr(getattr(context, '__parent__', None),
156                        '__parent__', None), 'reg_number', None)
157        if name == 'matric_number' and context is not None:
158            value = getattr(
159                getattr(getattr(context, '__parent__', None),
160                        '__parent__', None), 'matric_number', None)
161        return super(
162            StudentStudyLevelExporter, self).mangle_value(
163            value, name, context=context)
164
165    def export(self, study_levels, filepath=None):
166        """Export `study_levels`, an iterable, as CSV file.
167
168        If `filepath` is ``None``, a raw string with CSV data is returned.
169        """
170        writer, outfile = self.get_csv_writer(filepath)
171        for study_level in study_levels:
172            self.write_item(study_level, writer)
173        return self.close_outfile(filepath, outfile)
174
175    def export_all(self, site, filepath=None):
176        """Export study levels into filepath as CSV data.
177
178        If `filepath` is ``None``, a raw string with CSV data is returned.
179        """
180        catalog = queryUtility(
181            ICatalog, context=site, name='students_catalog', default=None)
182        if catalog is None:
183            return self.export([], filepath)
184        students = catalog.searchResults(
185            student_id=(None, None))
186        levels = []
187        study_courses = [x.get('studycourse', None) for x in students
188                         if x is not None]
189        for course in study_courses:
190            for level in course.values():
191                levels.append(level)
192        return self.export(levels, filepath)
[8342]193
[8411]194    def export_student(self, student, filepath=None):
195        if 'studycourse' in student.keys():
196            return self.export(student['studycourse'].values(),
197                               filepath=filepath)
198        return self.export([], filepath=filepath)
199
[8342]200class CourseTicketExporter(grok.GlobalUtility, ExporterBase):
201    """Exporter for CourseTickets.
202    """
[8411]203    grok.implements(ICSVStudentExporter)
204    grok.provides(ICSVStudentExporter)
[8342]205    grok.name('coursetickets')
206
207    #: Fieldnames considered by this exporter
208    fields = tuple(sorted(iface_names(ICourseTicket) + [
209        'reg_number', 'matric_number', 'level', 'code', 'title', 'credits',
210        'passmark', 'semester', 'fcode', 'dcode']))
211
212    #: The title under which this exporter will be displayed
213    title = _(u'Course Tickets')
214
215    def mangle_value(self, value, name, context=None):
216        """Treat location values special.
217        """
218        if context is not None:
219            student = context.getStudent()
220            if name in ['reg_number', 'matric_number'] and student is not None:
221                value = getattr(student, name, None)
222            if name == 'level':
[8393]223                value = getattr(context, 'getLevel', lambda: None)()
[8342]224        return super(
225            CourseTicketExporter, self).mangle_value(
226            value, name, context=context)
227
228    def export(self, course_tickets, filepath=None):
229        """Export `course_tickets`, an iterable, as CSV file.
230
231        If `filepath` is ``None``, a raw string with CSV data is returned.
232        """
233        writer, outfile = self.get_csv_writer(filepath)
234        for ticket in course_tickets:
235            self.write_item(ticket, writer)
236        return self.close_outfile(filepath, outfile)
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        """
243        catalog = queryUtility(
244            ICatalog, context=site, name='students_catalog', default=None)
245        if catalog is None:
246            return self.export([], filepath)
247        students = catalog.searchResults(
248            student_id=(None, None))
249        levels = []
250        study_courses = [x.get('studycourse', None) for x in students
251                         if x is not None]
252        for course in study_courses:
253            for level in course.values():
254                levels.append(level)
255
256        tickets = []
257        for level in levels:
258            for ticket in level.values():
259                tickets.append(ticket)
260        return self.export(tickets, filepath)
261
[8411]262    def export_student(self, student, filepath=None):
263        if 'studycourse' not in student.keys():
264            return self.export([], filepath=filepath)
265        levels = student['studycourse'].values()
266        tickets = []
267        for level in levels:
268            for ticket in level.values():
269                tickets.append(ticket)
270        return self.export(tickets, filepath=filepath)
271
[8371]272class PaymentsExporter(grok.GlobalUtility, ExporterBase):
273    """Exporter for OnlinePayment instances.
274    """
[8411]275    grok.implements(ICSVStudentExporter)
276    grok.provides(ICSVStudentExporter)
[8371]277    grok.name('studentpayments')
278
279    #: Fieldnames considered by this exporter
280    fields = tuple(
281        sorted(['reg_number', 'matric_number'] +
282               iface_names(IStudentOnlinePayment, exclude_attribs=False)))
283
284    #: The title under which this exporter will be displayed
285    title = _(u'Course Tickets')
286
287    def mangle_value(self, value, name, context=None):
288        """Treat location values special.
289        """
290        if context is not None:
291            student = context.getStudent()
292            if name in ['reg_number', 'matric_number'] and student is not None:
293                value = getattr(student, name, None)
294        return super(
295            PaymentsExporter, self).mangle_value(
296            value, name, context=context)
297
298    def export(self, payments, filepath=None):
299        """Export `payments`, an iterable, as CSV file.
300
301        If `filepath` is ``None``, a raw string with CSV data is returned.
302        """
303        writer, outfile = self.get_csv_writer(filepath)
304        for payment in payments:
305            self.write_item(payment, writer)
306        return self.close_outfile(filepath, outfile)
307
308    def export_all(self, site, filepath=None):
309        """Export payments into filepath as CSV data.
310
311        If `filepath` is ``None``, a raw string with CSV data is returned.
312        """
313        catalog = queryUtility(
314            ICatalog, context=site, name='students_catalog', default=None)
315        if catalog is None:
316            return self.export([], filepath)
317        students = catalog.searchResults(
318            student_id=(None, None))
319        payments = []
320        for student in students:
321            if not 'payments' in student.keys():
322                continue
323            for payment in student['payments'].values():
324                payments.append(payment)
325        return self.export(payments, filepath)
[8411]326
327    def export_student(self, student, filepath=None):
328        if not 'payments' in student.keys():
329            return self.export([], filepath=filepath)
330        return self.export(student['payments'].values(), filepath=filepath)
Note: See TracBrowser for help on using the repository browser.