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

Last change on this file since 11737 was 11730, checked in by Henrik Bettermann, 10 years ago

Filter payment ticket data exports by specifying the payment_date period.

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