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

Last change on this file since 9840 was 9834, checked in by Henrik Bettermann, 12 years ago

There are more student data exporters.

  • Property svn:keywords set to Id
File size: 12.8 KB
Line 
1## $Id: export.py 9834 2013-01-06 08:22:30Z 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##
18"""Exporters for student related stuff.
19"""
20import grok
21from datetime import datetime
22from waeup.kofa.interfaces import MessageFactory as _
23from waeup.kofa.students.catalog import StudentsQuery
24from waeup.kofa.students.interfaces import (
25    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
26    IStudentOnlinePayment, ICSVStudentExporter, IBedTicket)
27from waeup.kofa.students.vocabularies import study_levels
28from waeup.kofa.utils.batching import ExporterBase
29from waeup.kofa.utils.helpers import iface_names
30
31#: A tuple containing all exporter names referring to students or
32#: subobjects thereof.
33EXPORTER_NAMES = ('students', 'studentstudycourses',
34        'studentstudylevels', 'coursetickets',
35        'studentpayments', 'bedtickets', 'paymentsoverview',
36        'studylevelsoverview')
37
38def get_students(site, stud_filter=StudentsQuery()):
39    """Get all students registered in catalog in `site`.
40    """
41    return stud_filter.query()
42
43def get_studycourses(students):
44    """Get studycourses of `students`.
45    """
46    return [x.get('studycourse', None) for x in students
47            if x is not None]
48
49def get_levels(students):
50    """Get all studylevels of `students`.
51    """
52    levels = []
53    for course in get_studycourses(students):
54        for level in course.values():
55            levels.append(level)
56    return levels
57
58def get_tickets(students):
59    """Get all course tickets of `students`.
60    """
61    tickets = []
62    for level in get_levels(students):
63        for ticket in level.values():
64            tickets.append(ticket)
65    return tickets
66
67def get_payments(students):
68    """Get all payments of `students`.
69    """
70    payments = []
71    for student in students:
72        for payment in student.get('payments', {}).values():
73            payments.append(payment)
74    return payments
75
76def get_bedtickets(students):
77    """Get all bedtickets of `students`.
78    """
79    tickets = []
80    for student in students:
81        for ticket in student.get('accommodation', {}).values():
82            tickets.append(ticket)
83    return tickets
84
85class StudentExporterBase(ExporterBase):
86    """Exporter for students or related objects.
87
88    This is a baseclass.
89    """
90    grok.baseclass()
91    grok.implements(ICSVStudentExporter)
92    grok.provides(ICSVStudentExporter)
93
94    def filter_func(self, x):
95        return x
96
97    def get_filtered(self, site, **kw):
98        """Get students filtered by keywords.
99
100        The keys must be valid students catalog index names. Returns a
101        simple empty list or a catalog result set with `Student`
102        objects.
103
104        .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog`
105
106        """
107        # Pass only given keywords to StudentsQuery. This way we avoid
108        # trouble with `None` value ambivalences and queries are also
109        # faster (normally less indexes to ask). Drawback is, that
110        # developers must look into catalog to see what keywords are
111        # valid.
112        query = StudentsQuery(**kw)
113        return query.query()
114
115    def export(self, values, filepath=None):
116        """Export `values`, an iterable, as CSV file.
117
118        If `filepath` is ``None``, a raw string with CSV data is returned.
119        """
120        writer, outfile = self.get_csv_writer(filepath)
121        for value in values:
122            self.write_item(value, writer)
123        return self.close_outfile(filepath, outfile)
124
125    def export_all(self, site, filepath=None):
126        """Export students into filepath as CSV data.
127
128        If `filepath` is ``None``, a raw string with CSV data is returned.
129        """
130        return self.export(self.filter_func(get_students(site)), filepath)
131
132    def export_student(self, student, filepath=None):
133        return self.export(self.filter_func([student]), filepath=filepath)
134
135    def export_filtered(self, site, filepath=None, **kw):
136        """Export items denoted by `kw`.
137
138        If `filepath` is ``None``, a raw string with CSV data should
139        be returned.
140        """
141        data = self.get_filtered(site, **kw)
142        return self.export(self.filter_func(data), filepath=filepath)
143
144
145class StudentsExporter(grok.GlobalUtility, StudentExporterBase):
146    """Exporter for Students.
147    """
148    grok.name('students')
149
150    #: Fieldnames considered by this exporter
151    fields = tuple(sorted(iface_names(
152        IStudent, omit=['loggerInfo']))) + (
153        'password', 'state', 'history', 'certcode', 'is_postgrad',
154        'current_level', 'current_session')
155
156    #: The title under which this exporter will be displayed
157    title = _(u'Students')
158
159    def mangle_value(self, value, name, context=None):
160        if name == 'history':
161            value = value.messages
162        if name == 'phone' and value is not None:
163            # Append hash '#' to phone numbers to circumvent
164            # unwanted excel automatic
165            value = str('%s#' % value)
166        return super(
167            StudentsExporter, self).mangle_value(
168            value, name, context=context)
169
170
171class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
172    """Exporter for StudentStudyCourses.
173    """
174    grok.name('studentstudycourses')
175
176    #: Fieldnames considered by this exporter
177    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
178
179    #: The title under which this exporter will be displayed
180    title = _(u'Student Study Courses')
181
182    def filter_func(self, x):
183        return get_studycourses(x)
184
185    def mangle_value(self, value, name, context=None):
186        """Treat location values special.
187        """
188        if name == 'certificate' and value is not None:
189            # XXX: hopefully cert codes are unique site-wide
190            value = value.code
191        if name == 'student_id' and context is not None:
192            student = context.student
193            value = getattr(student, name, None)
194        return super(
195            StudentStudyCourseExporter, self).mangle_value(
196            value, name, context=context)
197
198
199class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
200    """Exporter for StudentStudyLevels.
201    """
202    grok.name('studentstudylevels')
203
204    #: Fieldnames considered by this exporter
205    fields = tuple(sorted(iface_names(
206        IStudentStudyLevel) + ['level'])) + (
207        'student_id', 'number_of_tickets','certcode')
208
209    #: The title under which this exporter will be displayed
210    title = _(u'Student Study Levels')
211
212    def filter_func(self, x):
213        return get_levels(x)
214
215    def mangle_value(self, value, name, context=None):
216        """Treat location values special.
217        """
218        if name == 'student_id' and context is not None:
219            student = context.student
220            value = getattr(student, name, None)
221        return super(
222            StudentStudyLevelExporter, self).mangle_value(
223            value, name, context=context)
224
225class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
226    """Exporter for CourseTickets.
227    """
228    grok.name('coursetickets')
229
230    #: Fieldnames considered by this exporter
231    fields = tuple(sorted(iface_names(ICourseTicket) +
232        ['level', 'code'])) + ('student_id', 'certcode')
233
234    #: The title under which this exporter will be displayed
235    title = _(u'Course Tickets')
236
237    def filter_func(self, x):
238        return get_tickets(x)
239
240    def mangle_value(self, value, name, context=None):
241        """Treat location values special.
242        """
243        if context is not None:
244            student = context.student
245            if name == 'student_id' and student is not None:
246                value = getattr(student, name, None)
247            if name == 'level':
248                value = getattr(context, 'getLevel', lambda: None)()
249        return super(
250            CourseTicketExporter, self).mangle_value(
251            value, name, context=context)
252
253
254class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase):
255    """Exporter for OnlinePayment instances.
256    """
257    grok.name('studentpayments')
258
259    #: Fieldnames considered by this exporter
260    fields = tuple(
261        sorted(iface_names(
262            IStudentOnlinePayment, exclude_attribs=False))) + (
263                'student_id','student_state','current_session')
264
265    #: The title under which this exporter will be displayed
266    title = _(u'Student Payments')
267
268    def filter_func(self, x):
269        return get_payments(x)
270
271    def mangle_value(self, value, name, context=None):
272        """Treat location values special.
273        """
274        if context is not None:
275            student = context.student
276            if name in ['student_id'] and student is not None:
277                value = getattr(student, name, None)
278        return super(
279            StudentPaymentsExporter, self).mangle_value(
280            value, name, context=context)
281
282class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase):
283    """Exporter for BedTicket instances.
284    """
285    grok.name('bedtickets')
286
287    #: Fieldnames considered by this exporter
288    fields = tuple(
289        sorted(iface_names(
290            IBedTicket, exclude_attribs=False))) + (
291                'student_id', 'actual_bed_type')
292
293    #: The title under which this exporter will be displayed
294    title = _(u'Bed Tickets')
295
296    def filter_func(self, x):
297        return get_bedtickets(x)
298
299    def mangle_value(self, value, name, context=None):
300        """Treat location values and others special.
301        """
302        if context is not None:
303            student = context.student
304            if name in ['student_id'] and student is not None:
305                value = getattr(student, name, None)
306        if name == 'bed' and value is not None:
307            value = getattr(value, 'bed_id', None)
308        if name == 'actual_bed_type':
309            value = getattr(getattr(context, 'bed', None), 'bed_type')
310        return super(
311            BedTicketsExporter, self).mangle_value(
312            value, name, context=context)
313
314class StudentPaymentsOverviewExporter(StudentsExporter):
315    """Exporter for students with payment overview.
316    """
317    grok.name('paymentsoverview')
318
319    curr_year = datetime.now().year
320    year_range = range(curr_year - 9, curr_year + 1)
321    year_range_tuple = tuple([str(year) for year in year_range])
322
323    #: Fieldnames considered by this exporter
324    fields = ('student_id', ) + (
325        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
326        'current_level', 'current_session', 'current_mode',
327        ) + year_range_tuple
328
329    #: The title under which this exporter will be displayed
330    title = _(u'Student Payments Overview')
331
332    def mangle_value(self, value, name, context=None):
333        if name in self.year_range_tuple and context is not None:
334            value = ''
335            for ticket in context['payments'].values():
336                if ticket.p_state == 'paid' and \
337                    ticket.p_category == 'schoolfee' and \
338                    ticket.p_session == int(name):
339                    value = ticket.amount_auth
340                    break
341        return super(
342            StudentsExporter, self).mangle_value(
343            value, name, context=context)
344
345class StudentStudyLevelsOverviewExporter(StudentsExporter):
346    """Exporter for students with study level overview.
347    """
348    grok.name('studylevelsoverview')
349
350    avail_levels = tuple([str(x) for x in study_levels(None)])
351
352    #: Fieldnames considered by this exporter
353    fields = ('student_id', ) + (
354        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
355        'entry_session', 'current_level', 'current_session',
356        ) + avail_levels
357
358    #: The title under which this exporter will be displayed
359    title = _(u'Student Study Levels Overview')
360
361    def mangle_value(self, value, name, context=None):
362        if name in self.avail_levels and context is not None:
363            value = ''
364            for level in context['studycourse'].values():
365                if level.level == int(name):
366                    #value = '%s|%s|%s|%s' % (
367                    #    level.level_session,
368                    #    len(level),
369                    #    level.validated_by,
370                    #    level.level_verdict)
371                    value = '%s' % level.level_session
372                    break
373        return super(
374            StudentsExporter, self).mangle_value(
375            value, name, context=context)
Note: See TracBrowser for help on using the repository browser.