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

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

Add level_session column to export.

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