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

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

Export current_mode.

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