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

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

DataForBursaryExporter?: Export only paid payment tickets.

Adjust tests.

  • Property svn:keywords set to Id
File size: 17.4 KB
RevLine 
[8057]1## $Id: export.py 10296 2013-06-12 10:16:01Z 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"""
[9937]20import os
[7944]21import grok
[9574]22from datetime import datetime
[9937]23from zope.component import getUtility
24from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
[7944]25from waeup.kofa.interfaces import MessageFactory as _
[9843]26from waeup.kofa.students.catalog import StudentsQuery, CourseTicketsQuery
[8015]27from waeup.kofa.students.interfaces import (
[8371]28    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
[9427]29    IStudentOnlinePayment, ICSVStudentExporter, IBedTicket)
[9787]30from waeup.kofa.students.vocabularies import study_levels
[7944]31from waeup.kofa.utils.batching import ExporterBase
32from waeup.kofa.utils.helpers import iface_names
33
[8400]34#: A tuple containing all exporter names referring to students or
35#: subobjects thereof.
[9834]36EXPORTER_NAMES = ('students', 'studentstudycourses',
37        'studentstudylevels', 'coursetickets',
38        'studentpayments', 'bedtickets', 'paymentsoverview',
[10233]39        'studylevelsoverview', 'combocard', 'bursary')
[8400]40
[9787]41def get_students(site, stud_filter=StudentsQuery()):
[8414]42    """Get all students registered in catalog in `site`.
[7944]43    """
[9787]44    return stud_filter.query()
[8414]45
46def get_studycourses(students):
47    """Get studycourses of `students`.
48    """
49    return [x.get('studycourse', None) for x in students
50            if x is not None]
51
52def get_levels(students):
53    """Get all studylevels of `students`.
54    """
55    levels = []
56    for course in get_studycourses(students):
57        for level in course.values():
58            levels.append(level)
59    return levels
60
[9844]61def get_tickets(students, **kw):
62    """Get course tickets of `students`.
63
[10017]64    If code is passed through, filter course tickets
65    which belong to this course code and meets level
66    and level_session.
[8414]67    """
68    tickets = []
[9844]69    code = kw.get('code', None)
[10017]70    level = kw.get('level', None)
71    level_session = kw.get('level_session', None)
[9844]72    if code is None:
[10017]73        for level_obj in get_levels(students):
74            for ticket in level_obj.values():
[9844]75                tickets.append(ticket)
76    else:
[10017]77        for level_obj in get_levels(students):
78            for ticket in level_obj.values():
79                if ticket.code != code:
80                    continue
81                if level is not None and int(level) != level_obj.level:
82                    continue
83                if level_session is not None and \
84                    int(level_session) != level_obj.level_session:
85                    continue
86                tickets.append(ticket)
[8414]87    return tickets
88
89def get_payments(students):
90    """Get all payments of `students`.
91    """
92    payments = []
93    for student in students:
94        for payment in student.get('payments', {}).values():
95            payments.append(payment)
96    return payments
97
[10296]98def get_paid_payments(students):
99    """Get all paid payments of `students`.
100    """
101    payments = []
102    for student in students:
103        for payment in student.get('payments', {}).values():
104            if payment.p_state == 'paid':
105                payments.append(payment)
106    return payments
107
[9427]108def get_bedtickets(students):
109    """Get all bedtickets of `students`.
110    """
111    tickets = []
112    for student in students:
113        for ticket in student.get('accommodation', {}).values():
114            tickets.append(ticket)
115    return tickets
[8414]116
117class StudentExporterBase(ExporterBase):
118    """Exporter for students or related objects.
119
120    This is a baseclass.
121    """
122    grok.baseclass()
[8411]123    grok.implements(ICSVStudentExporter)
124    grok.provides(ICSVStudentExporter)
[8414]125
[9844]126    def filter_func(self, x, **kw):
[9802]127        return x
[9797]128
[9801]129    def get_filtered(self, site, **kw):
[9843]130        """Get students from a catalog filtered by keywords.
[9801]131
[9843]132        students_catalog is the default catalog. The keys must be valid
[9933]133        catalog index names.
[9843]134        Returns a simple empty list, a list with `Student`
135        objects or a catalog result set with `Student`
[9801]136        objects.
137
138        .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog`
139
140        """
[9843]141        # Pass only given keywords to create FilteredCatalogQuery objects.
142        # This way we avoid
[9801]143        # trouble with `None` value ambivalences and queries are also
144        # faster (normally less indexes to ask). Drawback is, that
145        # developers must look into catalog to see what keywords are
146        # valid.
[9845]147        if kw.get('catalog', None) == 'coursetickets':
[9843]148            coursetickets = CourseTicketsQuery(**kw).query()
149            students = []
150            for ticket in coursetickets:
151                students.append(ticket.student)
152            return list(set(students))
[9801]153        query = StudentsQuery(**kw)
[9797]154        return query.query()
155
[8414]156    def export(self, values, filepath=None):
157        """Export `values`, an iterable, as CSV file.
158
159        If `filepath` is ``None``, a raw string with CSV data is returned.
160        """
161        writer, outfile = self.get_csv_writer(filepath)
162        for value in values:
163            self.write_item(value, writer)
164        return self.close_outfile(filepath, outfile)
165
[9797]166    def export_all(self, site, filepath=None):
167        """Export students into filepath as CSV data.
168
169        If `filepath` is ``None``, a raw string with CSV data is returned.
[9763]170        """
[9802]171        return self.export(self.filter_func(get_students(site)), filepath)
[8414]172
[9797]173    def export_student(self, student, filepath=None):
174        return self.export(self.filter_func([student]), filepath=filepath)
[9734]175
[9802]176    def export_filtered(self, site, filepath=None, **kw):
177        """Export items denoted by `kw`.
[9797]178
[9802]179        If `filepath` is ``None``, a raw string with CSV data should
180        be returned.
181        """
182        data = self.get_filtered(site, **kw)
[9844]183        return self.export(self.filter_func(data, **kw), filepath=filepath)
[9802]184
185
[8414]186class StudentsExporter(grok.GlobalUtility, StudentExporterBase):
187    """Exporter for Students.
188    """
[7944]189    grok.name('students')
190
191    #: Fieldnames considered by this exporter
[9936]192    fields = tuple(sorted(iface_names(IStudent))) + (
[9253]193        'password', 'state', 'history', 'certcode', 'is_postgrad',
194        'current_level', 'current_session')
[7944]195
196    #: The title under which this exporter will be displayed
197    title = _(u'Students')
198
[8493]199    def mangle_value(self, value, name, context=None):
200        if name == 'history':
201            value = value.messages
[8971]202        if name == 'phone' and value is not None:
203            # Append hash '#' to phone numbers to circumvent
204            # unwanted excel automatic
[8947]205            value = str('%s#' % value)
[8493]206        return super(
207            StudentsExporter, self).mangle_value(
208            value, name, context=context)
209
[7944]210
[8414]211class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
[7994]212    """Exporter for StudentStudyCourses.
213    """
214    grok.name('studentstudycourses')
215
216    #: Fieldnames considered by this exporter
[8493]217    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
[7994]218
219    #: The title under which this exporter will be displayed
220    title = _(u'Student Study Courses')
221
[9844]222    def filter_func(self, x, **kw):
[9797]223        return get_studycourses(x)
224
[7994]225    def mangle_value(self, value, name, context=None):
[8493]226        """Treat location values special.
[7994]227        """
228        if name == 'certificate' and value is not None:
229            # XXX: hopefully cert codes are unique site-wide
230            value = value.code
[8493]231        if name == 'student_id' and context is not None:
[8736]232            student = context.student
[8493]233            value = getattr(student, name, None)
[7994]234        return super(
235            StudentStudyCourseExporter, self).mangle_value(
236            value, name, context=context)
237
238
[8414]239class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
[8015]240    """Exporter for StudentStudyLevels.
241    """
242    grok.name('studentstudylevels')
243
244    #: Fieldnames considered by this exporter
[8493]245    fields = tuple(sorted(iface_names(
[9253]246        IStudentStudyLevel) + ['level'])) + (
247        'student_id', 'number_of_tickets','certcode')
[8015]248
249    #: The title under which this exporter will be displayed
250    title = _(u'Student Study Levels')
251
[9844]252    def filter_func(self, x, **kw):
[9802]253        return get_levels(x)
254
[8015]255    def mangle_value(self, value, name, context=None):
[8493]256        """Treat location values special.
[8015]257        """
[8493]258        if name == 'student_id' and context is not None:
[8736]259            student = context.student
[8493]260            value = getattr(student, name, None)
[8015]261        return super(
262            StudentStudyLevelExporter, self).mangle_value(
263            value, name, context=context)
264
[8414]265class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
[8342]266    """Exporter for CourseTickets.
267    """
268    grok.name('coursetickets')
269
270    #: Fieldnames considered by this exporter
[8493]271    fields = tuple(sorted(iface_names(ICourseTicket) +
[9859]272        ['level', 'code', 'level_session'])) + ('student_id', 'certcode')
[8342]273
274    #: The title under which this exporter will be displayed
275    title = _(u'Course Tickets')
276
[9844]277    def filter_func(self, x, **kw):
278        return get_tickets(x, **kw)
[9802]279
[8342]280    def mangle_value(self, value, name, context=None):
281        """Treat location values special.
282        """
283        if context is not None:
[8736]284            student = context.student
[8493]285            if name == 'student_id' and student is not None:
[8342]286                value = getattr(student, name, None)
287            if name == 'level':
[9925]288                value = getattr(context, 'level', lambda: None)
[9859]289            if name == 'level_session':
[9925]290                value = getattr(context, 'level_session', lambda: None)
[8342]291        return super(
292            CourseTicketExporter, self).mangle_value(
293            value, name, context=context)
294
295
[9832]296class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase):
[8371]297    """Exporter for OnlinePayment instances.
298    """
299    grok.name('studentpayments')
300
301    #: Fieldnames considered by this exporter
302    fields = tuple(
[8493]303        sorted(iface_names(
[9984]304            IStudentOnlinePayment, exclude_attribs=False,
305            omit=['display_item']))) + (
[10232]306            'student_id','state','current_session')
[8371]307
308    #: The title under which this exporter will be displayed
[8576]309    title = _(u'Student Payments')
[8371]310
[9844]311    def filter_func(self, x, **kw):
[9802]312        return get_payments(x)
313
[8371]314    def mangle_value(self, value, name, context=None):
315        """Treat location values special.
316        """
317        if context is not None:
[8736]318            student = context.student
[10232]319            if name in ['student_id','state',
320                        'current_session'] and student is not None:
[8371]321                value = getattr(student, name, None)
322        return super(
[9832]323            StudentPaymentsExporter, self).mangle_value(
[8371]324            value, name, context=context)
325
[10233]326class DataForBursaryExporter(StudentPaymentsExporter):
327    """Exporter for OnlinePayment instances.
328    """
329    grok.name('bursary')
330
[10296]331    def filter_func(self, x, **kw):
332        return get_paid_payments(x)
333
[10233]334    #: Fieldnames considered by this exporter
335    fields = tuple(
336        sorted(iface_names(
337            IStudentOnlinePayment, exclude_attribs=False,
338            omit=['display_item']))) + (
[10236]339            'student_id','matric_number',
340            'firstname', 'middlename', 'lastname',
341            'state','current_session',
342            'entry_session', 'entry_mode',
343            'faccode', 'depcode')
[10233]344
345    #: The title under which this exporter will be displayed
346    title = _(u'Payment Data for Bursary')
347
348    def mangle_value(self, value, name, context=None):
349        """Treat location values special.
350        """
351        if context is not None:
352            student = context.student
[10236]353            if name in [
354                'student_id','matric_number',
355                'firstname', 'middlename', 'lastname',
356                'state','current_session',
357                'entry_session', 'entry_mode',
358                'faccode', 'depcode'] and student is not None:
[10233]359                value = getattr(student, name, None)
360        return super(
361            StudentPaymentsExporter, self).mangle_value(
362            value, name, context=context)
363
[9427]364class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase):
365    """Exporter for BedTicket instances.
366    """
367    grok.name('bedtickets')
368
369    #: Fieldnames considered by this exporter
370    fields = tuple(
371        sorted(iface_names(
[9984]372            IBedTicket, exclude_attribs=False,
373            omit=['display_coordinates']))) + (
374            'student_id', 'actual_bed_type')
[9427]375
376    #: The title under which this exporter will be displayed
377    title = _(u'Bed Tickets')
378
[9844]379    def filter_func(self, x, **kw):
[9802]380        return get_bedtickets(x)
381
[9427]382    def mangle_value(self, value, name, context=None):
383        """Treat location values and others special.
384        """
385        if context is not None:
386            student = context.student
387            if name in ['student_id'] and student is not None:
388                value = getattr(student, name, None)
389        if name == 'bed' and value is not None:
390            value = getattr(value, 'bed_id', None)
391        if name == 'actual_bed_type':
392            value = getattr(getattr(context, 'bed', None), 'bed_type')
393        return super(
394            BedTicketsExporter, self).mangle_value(
395            value, name, context=context)
396
[9574]397class StudentPaymentsOverviewExporter(StudentsExporter):
398    """Exporter for students with payment overview.
399    """
400    grok.name('paymentsoverview')
401
402    curr_year = datetime.now().year
403    year_range = range(curr_year - 9, curr_year + 1)
404    year_range_tuple = tuple([str(year) for year in year_range])
405
406    #: Fieldnames considered by this exporter
[9983]407    fields = ('student_id', 'matric_number', 'display_fullname',
[9574]408        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
[9807]409        'current_level', 'current_session', 'current_mode',
[9574]410        ) + year_range_tuple
411
412    #: The title under which this exporter will be displayed
413    title = _(u'Student Payments Overview')
414
415    def mangle_value(self, value, name, context=None):
416        if name in self.year_range_tuple and context is not None:
417            value = ''
418            for ticket in context['payments'].values():
419                if ticket.p_state == 'paid' and \
420                    ticket.p_category == 'schoolfee' and \
421                    ticket.p_session == int(name):
422                    value = ticket.amount_auth
423                    break
424        return super(
425            StudentsExporter, self).mangle_value(
[9734]426            value, name, context=context)
[9744]427
428class StudentStudyLevelsOverviewExporter(StudentsExporter):
429    """Exporter for students with study level overview.
430    """
431    grok.name('studylevelsoverview')
432
[9787]433    avail_levels = tuple([str(x) for x in study_levels(None)])
[9744]434
435    #: Fieldnames considered by this exporter
436    fields = ('student_id', ) + (
437        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
[9761]438        'entry_session', 'current_level', 'current_session',
[9787]439        ) + avail_levels
[9744]440
441    #: The title under which this exporter will be displayed
442    title = _(u'Student Study Levels Overview')
443
444    def mangle_value(self, value, name, context=None):
[9787]445        if name in self.avail_levels and context is not None:
[9744]446            value = ''
447            for level in context['studycourse'].values():
448                if level.level == int(name):
[9761]449                    #value = '%s|%s|%s|%s' % (
450                    #    level.level_session,
451                    #    len(level),
452                    #    level.validated_by,
453                    #    level.level_verdict)
454                    value = '%s' % level.level_session
[9744]455                    break
456        return super(
457            StudentsExporter, self).mangle_value(
458            value, name, context=context)
[9936]459
460class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase):
461    """Exporter for Interswitch Combo Card Data.
462    """
463    grok.name('combocard')
464
465    #: Fieldnames considered by this exporter
466    fields = ('display_fullname',
[9937]467              'student_id','matric_number',
468              'certificate', 'faculty', 'department', 'passport_path')
[9936]469
470    #: The title under which this exporter will be displayed
471    title = _(u'Combo Card Data')
472
473    def mangle_value(self, value, name, context=None):
474        certificate = context['studycourse'].certificate
475        if name == 'certificate' and certificate is not None:
476            value = certificate.title
477        if name == 'department' and certificate is not None:
478            value = certificate.__parent__.__parent__.longtitle()
479        if name == 'faculty' and certificate is not None:
480            value = certificate.__parent__.__parent__.__parent__.longtitle()
[9937]481        if name == 'passport_path' and certificate is not None:
482            file_id = IFileStoreNameChooser(context).chooseName(attr='passport.jpg')
483            os_path = getUtility(IExtFileStore)._pathFromFileID(file_id)
484            if not os.path.exists(os_path):
485                value = None
486            else:
487                value = '/'.join(os_path.split('/')[-4:])
[9936]488        return super(
489            ComboCardDataExporter, self).mangle_value(
490            value, name, context=context)
Note: See TracBrowser for help on using the repository browser.