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