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

Last change on this file since 8342 was 8342, checked in by uli, 12 years ago

Add exporter for course tickets.

  • Property svn:keywords set to Id
File size: 9.2 KB
Line 
1## $Id: export.py 8342 2012-05-04 11:52:58Z 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##
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 ICSVExporter
24from waeup.kofa.interfaces import MessageFactory as _
25from waeup.kofa.students.interfaces import (
26    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket)
27from waeup.kofa.utils.batching import ExporterBase
28from waeup.kofa.utils.helpers import iface_names
29
30class StudentsExporter(grok.GlobalUtility, ExporterBase):
31    """Exporter for Students.
32    """
33    grok.implements(ICSVExporter)
34    grok.name('students')
35
36    #: Fieldnames considered by this exporter
37    fields = tuple(sorted(iface_names(IStudent, omit=['loggerInfo'])))
38
39    #: The title under which this exporter will be displayed
40    title = _(u'Students')
41
42    def mangle_value(self, value, name, context=None):
43        """Add the hash symbol at the end of date_of_birth.
44
45        This is to avoid annoying automatic date transformation
46        by Excel or Calc.
47        """
48        if name == 'date_of_birth':
49            value = str(value) + '#'
50        return super(
51            StudentsExporter, self).mangle_value(
52            value, name, context=context)
53
54    def export(self, students, filepath=None):
55        """Export `students`, an iterable, as CSV file.
56
57        If `filepath` is ``None``, a raw string with CSV data is returned.
58        """
59        writer, outfile = self.get_csv_writer(filepath)
60        for student in students:
61            self.write_item(student, writer)
62        return self.close_outfile(filepath, outfile)
63
64    def export_all(self, site, filepath=None):
65        """Export students into filepath as CSV data.
66
67        If `filepath` is ``None``, a raw string with CSV data is returned.
68        """
69        catalog = queryUtility(
70            ICatalog, context=site, name='students_catalog', default=None)
71        if catalog is None:
72            return self.export([], filepath)
73        students = catalog.searchResults(
74            student_id=(None, None))
75        return self.export(students, filepath)
76
77class StudentStudyCourseExporter(grok.GlobalUtility, ExporterBase):
78    """Exporter for StudentStudyCourses.
79    """
80    grok.implements(ICSVExporter)
81    grok.name('studentstudycourses')
82
83    #: Fieldnames considered by this exporter
84    fields = tuple(sorted(iface_names(IStudentStudyCourse)))
85
86    #: The title under which this exporter will be displayed
87    title = _(u'Student Study Courses')
88
89    def mangle_value(self, value, name, context=None):
90        """Add the hash symbol at the end of date_of_birth.
91
92        This is to avoid annoying automatic date transformation
93        by Excel or Calc.
94        """
95        if name == 'certificate' and value is not None:
96            # XXX: hopefully cert codes are unique site-wide
97            value = value.code
98        return super(
99            StudentStudyCourseExporter, self).mangle_value(
100            value, name, context=context)
101
102
103    def export(self, study_courses, filepath=None):
104        """Export `study_courses`, an iterable, as CSV file.
105
106        If `filepath` is ``None``, a raw string with CSV data is returned.
107        """
108        writer, outfile = self.get_csv_writer(filepath)
109        for study_course in study_courses:
110            self.write_item(study_course, writer)
111        return self.close_outfile(filepath, outfile)
112
113    def export_all(self, site, filepath=None):
114        """Export study courses into filepath as CSV data.
115
116        If `filepath` is ``None``, a raw string with CSV data is returned.
117        """
118        catalog = queryUtility(
119            ICatalog, context=site, name='students_catalog', default=None)
120        if catalog is None:
121            return self.export([], filepath)
122        students = catalog.searchResults(
123            student_id=(None, None))
124        study_courses = [x.get('studycourse', None) for x in students
125                         if x is not None]
126        return self.export(study_courses, filepath)
127
128class StudentStudyLevelExporter(grok.GlobalUtility, ExporterBase):
129    """Exporter for StudentStudyLevels.
130    """
131    grok.implements(ICSVExporter)
132    grok.name('studentstudylevels')
133
134    #: Fieldnames considered by this exporter
135    fields = tuple(sorted(iface_names(IStudentStudyLevel) + [
136        'reg_number', 'matric_number', 'level']))
137
138    #: The title under which this exporter will be displayed
139    title = _(u'Student Study Levels')
140
141    def mangle_value(self, value, name, context=None):
142        """Add the hash symbol at the end of date_of_birth.
143
144        This is to avoid annoying automatic date transformation
145        by Excel or Calc.
146        """
147
148        if name == 'reg_number' and context is not None:
149            value = getattr(
150                getattr(getattr(context, '__parent__', None),
151                        '__parent__', None), 'reg_number', None)
152        if name == 'matric_number' and context is not None:
153            value = getattr(
154                getattr(getattr(context, '__parent__', None),
155                        '__parent__', None), 'matric_number', None)
156        return super(
157            StudentStudyLevelExporter, self).mangle_value(
158            value, name, context=context)
159
160    def export(self, study_levels, filepath=None):
161        """Export `study_levels`, an iterable, as CSV file.
162
163        If `filepath` is ``None``, a raw string with CSV data is returned.
164        """
165        writer, outfile = self.get_csv_writer(filepath)
166        for study_level in study_levels:
167            self.write_item(study_level, writer)
168        return self.close_outfile(filepath, outfile)
169
170    def export_all(self, site, filepath=None):
171        """Export study levels into filepath as CSV data.
172
173        If `filepath` is ``None``, a raw string with CSV data is returned.
174        """
175        catalog = queryUtility(
176            ICatalog, context=site, name='students_catalog', default=None)
177        if catalog is None:
178            return self.export([], filepath)
179        students = catalog.searchResults(
180            student_id=(None, None))
181        levels = []
182        study_courses = [x.get('studycourse', None) for x in students
183                         if x is not None]
184        for course in study_courses:
185            for level in course.values():
186                levels.append(level)
187        return self.export(levels, filepath)
188
189class CourseTicketExporter(grok.GlobalUtility, ExporterBase):
190    """Exporter for CourseTickets.
191    """
192    grok.implements(ICSVExporter)
193    grok.name('coursetickets')
194
195    #: Fieldnames considered by this exporter
196    fields = tuple(sorted(iface_names(ICourseTicket) + [
197        'reg_number', 'matric_number', 'level', 'code', 'title', 'credits',
198        'passmark', 'semester', 'fcode', 'dcode']))
199
200    #: The title under which this exporter will be displayed
201    title = _(u'Course Tickets')
202
203    def mangle_value(self, value, name, context=None):
204        """Treat location values special.
205        """
206        if context is not None:
207            student = context.getStudent()
208            if name in ['reg_number', 'matric_number'] and student is not None:
209                value = getattr(student, name, None)
210            if name == 'level':
211                value = context.getLevel()
212        return super(
213            CourseTicketExporter, self).mangle_value(
214            value, name, context=context)
215
216    def export(self, course_tickets, filepath=None):
217        """Export `course_tickets`, an iterable, as CSV file.
218
219        If `filepath` is ``None``, a raw string with CSV data is returned.
220        """
221        writer, outfile = self.get_csv_writer(filepath)
222        for ticket in course_tickets:
223            self.write_item(ticket, writer)
224        return self.close_outfile(filepath, outfile)
225
226    def export_all(self, site, filepath=None):
227        """Export course tickets into filepath as CSV data.
228
229        If `filepath` is ``None``, a raw string with CSV data is returned.
230        """
231        catalog = queryUtility(
232            ICatalog, context=site, name='students_catalog', default=None)
233        if catalog is None:
234            return self.export([], filepath)
235        students = catalog.searchResults(
236            student_id=(None, None))
237        levels = []
238        study_courses = [x.get('studycourse', None) for x in students
239                         if x is not None]
240        for course in study_courses:
241            for level in course.values():
242                levels.append(level)
243
244        tickets = []
245        for level in levels:
246            for ticket in level.values():
247                tickets.append(ticket)
248        return self.export(tickets, filepath)
249
Note: See TracBrowser for help on using the repository browser.