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

Last change on this file since 8903 was 8736, checked in by Henrik Bettermann, 13 years ago

Replace getStudent method by a 'student' attribute.

  • Property svn:keywords set to Id
File size: 9.1 KB
RevLine 
[8057]1## $Id: export.py 8736 2012-06-17 07:09:21Z 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"""
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
[8414]35def 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
45def 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
51def 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
60def 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
69def 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
79class 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
99class 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
115        return super(
116            StudentsExporter, self).mangle_value(
117            value, name, context=context)
118
[7944]119    def export_all(self, site, filepath=None):
120        """Export students into filepath as CSV data.
121
122        If `filepath` is ``None``, a raw string with CSV data is returned.
123        """
[8414]124        return self.export(get_students(site), filepath)
[7994]125
[8411]126    def export_student(self, student, filepath=None):
127        return self.export([student], filepath=filepath)
128
[8414]129
130class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
[7994]131    """Exporter for StudentStudyCourses.
132    """
133    grok.name('studentstudycourses')
134
135    #: Fieldnames considered by this exporter
[8493]136    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
[7994]137
138    #: The title under which this exporter will be displayed
139    title = _(u'Student Study Courses')
140
141    def mangle_value(self, value, name, context=None):
[8493]142        """Treat location values special.
[7994]143        """
144        if name == 'certificate' and value is not None:
145            # XXX: hopefully cert codes are unique site-wide
146            value = value.code
[8493]147        if name == 'student_id' and context is not None:
[8736]148            student = context.student
[8493]149            value = getattr(student, name, None)
[7994]150        return super(
151            StudentStudyCourseExporter, self).mangle_value(
152            value, name, context=context)
153
154    def export_all(self, site, filepath=None):
155        """Export study courses into filepath as CSV data.
156
157        If `filepath` is ``None``, a raw string with CSV data is returned.
158        """
[8414]159        return self.export(get_studycourses(get_students(site)), filepath)
[8015]160
[8411]161    def export_student(self, student, filepath=None):
[8414]162        """Export studycourse of a single student object.
163        """
164        return self.export(get_studycourses([student]), filepath)
[8411]165
166
[8414]167class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
[8015]168    """Exporter for StudentStudyLevels.
169    """
170    grok.name('studentstudylevels')
171
172    #: Fieldnames considered by this exporter
[8493]173    fields = tuple(sorted(iface_names(
174        IStudentStudyLevel) + ['level'])) + ('student_id',)
[8015]175
176    #: The title under which this exporter will be displayed
177    title = _(u'Student Study Levels')
178
179    def mangle_value(self, value, name, context=None):
[8493]180        """Treat location values special.
[8015]181        """
[8493]182        if name == 'student_id' and context is not None:
[8736]183            student = context.student
[8493]184            value = getattr(student, name, None)
[8015]185        return super(
186            StudentStudyLevelExporter, self).mangle_value(
187            value, name, context=context)
188
189    def export_all(self, site, filepath=None):
190        """Export study levels into filepath as CSV data.
191
192        If `filepath` is ``None``, a raw string with CSV data is returned.
193        """
[8414]194        return self.export(get_levels(get_students(site)), filepath)
[8342]195
[8411]196    def export_student(self, student, filepath=None):
[8414]197        return self.export(get_levels([student]), filepath)
[8411]198
[8414]199class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
[8342]200    """Exporter for CourseTickets.
201    """
202    grok.name('coursetickets')
203
204    #: Fieldnames considered by this exporter
[8493]205    fields = tuple(sorted(iface_names(ICourseTicket) +
206        ['level', 'code', 'title', 'credits',
207        'passmark', 'semester', 'fcode', 'dcode'])) + ('student_id',)
[8342]208
209    #: The title under which this exporter will be displayed
210    title = _(u'Course Tickets')
211
212    def mangle_value(self, value, name, context=None):
213        """Treat location values special.
214        """
215        if context is not None:
[8736]216            student = context.student
[8493]217            if name == 'student_id' and student is not None:
[8342]218                value = getattr(student, name, None)
219            if name == 'level':
[8393]220                value = getattr(context, 'getLevel', lambda: None)()
[8342]221        return super(
222            CourseTicketExporter, self).mangle_value(
223            value, name, context=context)
224
225    def export_all(self, site, filepath=None):
226        """Export course tickets into filepath as CSV data.
227
228        If `filepath` is ``None``, a raw string with CSV data is returned.
229        """
[8414]230        return self.export(get_tickets(get_students(site)), filepath)
[8342]231
[8411]232    def export_student(self, student, filepath=None):
[8414]233        return self.export(get_tickets([student]), filepath)
[8411]234
[8414]235
236class PaymentsExporter(grok.GlobalUtility, StudentExporterBase):
[8371]237    """Exporter for OnlinePayment instances.
238    """
239    grok.name('studentpayments')
240
241    #: Fieldnames considered by this exporter
242    fields = tuple(
[8493]243        sorted(iface_names(
244            IStudentOnlinePayment, exclude_attribs=False))) + ('student_id',)
[8371]245
246    #: The title under which this exporter will be displayed
[8576]247    title = _(u'Student Payments')
[8371]248
249    def mangle_value(self, value, name, context=None):
250        """Treat location values special.
251        """
252        if context is not None:
[8736]253            student = context.student
[8493]254            if name in ['student_id'] and student is not None:
[8371]255                value = getattr(student, name, None)
256        return super(
257            PaymentsExporter, self).mangle_value(
258            value, name, context=context)
259
260    def export_all(self, site, filepath=None):
261        """Export payments into filepath as CSV data.
262
263        If `filepath` is ``None``, a raw string with CSV data is returned.
264        """
[8414]265        return self.export(get_payments(get_students(site)), filepath)
[8411]266
267    def export_student(self, student, filepath=None):
[8414]268        return self.export(get_payments([student]), filepath)
Note: See TracBrowser for help on using the repository browser.