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

Last change on this file since 16432 was 16431, checked in by Henrik Bettermann, 4 years ago

Add payment option (p_option) field to payment tickets and add
select box on online payment add form pages. Disable this feature
in the base package.

  • Property svn:keywords set to Id
File size: 37.5 KB
RevLine 
[8057]1## $Id: export.py 16431 2021-03-25 09:45:54Z 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
[13156]22from datetime import datetime, timedelta
[9937]23from zope.component import getUtility
[11757]24from waeup.kofa.interfaces import (
25    IExtFileStore, IFileStoreNameChooser, IKofaUtils)
[7944]26from waeup.kofa.interfaces import MessageFactory as _
[15873]27from waeup.kofa.university.interfaces import ICertificateCourse, ICourse
[9843]28from waeup.kofa.students.catalog import StudentsQuery, CourseTicketsQuery
[8015]29from waeup.kofa.students.interfaces import (
[8371]30    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket,
[9427]31    IStudentOnlinePayment, ICSVStudentExporter, IBedTicket)
[9787]32from waeup.kofa.students.vocabularies import study_levels
[7944]33from waeup.kofa.utils.batching import ExporterBase
[11757]34from waeup.kofa.utils.helpers import iface_names, to_timezone
[7944]35
[8400]36
[9787]37def get_students(site, stud_filter=StudentsQuery()):
[8414]38    """Get all students registered in catalog in `site`.
[7944]39    """
[9787]40    return stud_filter.query()
[8414]41
42def get_studycourses(students):
43    """Get studycourses of `students`.
44    """
45    return [x.get('studycourse', None) for x in students
46            if x is not None]
47
[15918]48def get_levels(students, **kw):
[8414]49    """Get all studylevels of `students`.
50    """
51    levels = []
[15918]52    level_session = kw.get('level_session', None)
[8414]53    for course in get_studycourses(students):
54        for level in course.values():
[15918]55            if level_session not in ('all', None) and \
56                int(level_session) != level.level_session:
57                continue
[8414]58            levels.append(level)
59    return levels
60
[9844]61def get_tickets(students, **kw):
62    """Get course tickets of `students`.
[10017]63    If code is passed through, filter course tickets
[14984]64    which belong to this course code and meet level=level
65    and level_session=level_session.
[15546]66    If not, but ct_level, ct_session and ct_semester
[14984]67    are passed through, filter course tickets
[15546]68    which meet level==ct_level, level_session==ct_session
69    and semester==ct_semester.
[8414]70    """
71    tickets = []
[9844]72    code = kw.get('code', None)
[10017]73    level = kw.get('level', None)
[14984]74    session = kw.get('session', None)
75    ct_level = kw.get('ct_level', None)
76    ct_session = kw.get('ct_session', None)
[15546]77    ct_semester = kw.get('ct_semester', None)
[9844]78    if code is None:
[15918]79        for level_obj in get_levels(students, **kw):
[10017]80            for ticket in level_obj.values():
[14984]81                if ct_level not in ('all', None):
82                    if level_obj.level in (10, 999, None)  \
83                        and int(ct_level) != level_obj.level:
84                        continue
85                    if level_obj.level not in range(
86                        int(ct_level), int(ct_level)+100, 10):
87                        continue
88                if ct_session not in ('all', None) and \
89                    int(ct_session) != level_obj.level_session:
90                    continue
[15546]91                if ct_semester not in ('all', None) and \
92                    int(ct_semester) != ticket.semester:
93                    continue
[9844]94                tickets.append(ticket)
95    else:
[15918]96        for level_obj in get_levels(students, **kw):
[10017]97            for ticket in level_obj.values():
98                if ticket.code != code:
99                    continue
[14984]100                if level not in ('all', None):
[11483]101                    if level_obj.level in (10, 999, None)  \
102                        and int(level) != level_obj.level:
103                        continue
[14984]104                    if level_obj.level not in range(
105                        int(level), int(level)+100, 10):
[11483]106                        continue
[14984]107                if session not in ('all', None) and \
108                    int(session) != level_obj.level_session:
[10017]109                    continue
110                tickets.append(ticket)
[8414]111    return tickets
112
[13894]113def get_tickets_for_lecturer(students, **kw):
114    """Get course tickets of `students`.
115    Filter course tickets which belong to this course code and
[16412]116    which are editable by lecturers. The latter requirement was disabled
117    on 10/03/21.
[13894]118    """
119    tickets = []
120    code = kw.get('code', None)
121    level_session = kw.get('session', None)
122    level = kw.get('level', None)
[15918]123    for level_obj in get_levels(students, **kw):
[13894]124        for ticket in level_obj.values():
125            if ticket.code != code:
126                continue
[16412]127            # Disabled on 10/03/21
128            #if not ticket.editable_by_lecturer:
129            #    continue
[14984]130            if level not in ('all', None):
[13894]131                if level_obj.level in (10, 999, None)  \
132                    and int(level) != level_obj.level:
133                    continue
[14984]134                if level_obj.level not in range(int(level), int(level)+100, 10):
[13894]135                    continue
[14984]136            if level_session not in ('all', None) and \
[13894]137                int(level_session) != level_obj.level_session:
138                continue
139            tickets.append(ticket)
140    return tickets
141
[15873]142def get_outstanding(students, **kw):
143    """Get students with outstanding certificate courses.
144    """
145    students_wo = []
146    for student in students:
147        certificate = getattr(
148            student.get('studycourse', None), 'certificate', None)
149        if certificate:
150            allticketcodes = []
151            failedticketcodes = '' # taken but failed
152            nottakenticketcodes = '' # registered but not taken
153            missedticketcodes = '' # not registered
154            for level in student['studycourse'].values():
155                failedticketcodes += level.passed_params[4]
156                nottakenticketcodes += level.passed_params[5]
157                for ticket in level.values():
158                    allticketcodes.append(ticket.code)
159            for certcourse in certificate.values():
160                if certcourse.getCourseCode() not in allticketcodes:
161                    missedticketcodes += '%s ' % certcourse.__name__
162            student_wo = (student, missedticketcodes,
163                          failedticketcodes, nottakenticketcodes)
164            students_wo.append(student_wo)
165    return students_wo
166
[14761]167def get_payments(students, p_states=None, **kw):
[15924]168    """Get all payment tickets of `students` within given payment_date period.
[10296]169    """
[11730]170    date_format = '%d/%m/%Y'
[10296]171    payments = []
[15043]172    p_start = kw.get('payments_start')
173    p_end = kw.get('payments_end')
[15042]174    paycat = kw.get('paycat')
[15055]175    paysession = kw.get('paysession')
[15042]176    for student in students:
177        for payment in student.get('payments', {}).values():
[15043]178            if p_start and p_end:
[15042]179                if not payment.payment_date:
180                    continue
[15043]181                payments_start = datetime.strptime(p_start, date_format)
182                payments_end = datetime.strptime(p_end, date_format)
[15042]183                tz = getUtility(IKofaUtils).tzinfo
184                payments_start = tz.localize(payments_start)
185                payments_end = tz.localize(payments_end) + timedelta(days=1)
186                payment_date = to_timezone(payment.payment_date, tz)
187                if payment_date < payments_start or payment_date > payments_end:
188                    continue
189            if p_states and not payment.p_state in p_states:
190                continue
191            if paycat not in ('all', None) and payment.p_category != paycat:
192                continue
[15297]193            if paysession not in ('all', None) \
194                and payment.p_session != int(paysession):
[15055]195                continue
[15042]196            payments.append(payment)
[10296]197    return payments
198
[9427]199def get_bedtickets(students):
[15924]200    """Get all bed tickets of `students`.
[9427]201    """
202    tickets = []
203    for student in students:
204        for ticket in student.get('accommodation', {}).values():
205            tickets.append(ticket)
206    return tickets
[8414]207
208class StudentExporterBase(ExporterBase):
209    """Exporter for students or related objects.
210    This is a baseclass.
211    """
212    grok.baseclass()
[8411]213    grok.implements(ICSVStudentExporter)
214    grok.provides(ICSVStudentExporter)
[8414]215
[9844]216    def filter_func(self, x, **kw):
[9802]217        return x
[9797]218
[9801]219    def get_filtered(self, site, **kw):
[9843]220        """Get students from a catalog filtered by keywords.
221        students_catalog is the default catalog. The keys must be valid
[9933]222        catalog index names.
[9843]223        Returns a simple empty list, a list with `Student`
224        objects or a catalog result set with `Student`
[9801]225        objects.
226
227        .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog`
228
229        """
[9843]230        # Pass only given keywords to create FilteredCatalogQuery objects.
231        # This way we avoid
[9801]232        # trouble with `None` value ambivalences and queries are also
233        # faster (normally less indexes to ask). Drawback is, that
234        # developers must look into catalog to see what keywords are
235        # valid.
[9845]236        if kw.get('catalog', None) == 'coursetickets':
[9843]237            coursetickets = CourseTicketsQuery(**kw).query()
238            students = []
239            for ticket in coursetickets:
240                students.append(ticket.student)
241            return list(set(students))
[15042]242        # Payments can be filtered by payment date and payment category.
243        # These parameters are not keys of the catalog and must thus be
244        # removed from kw.
[11730]245        try:
246            del kw['payments_start']
247            del kw['payments_end']
[15042]248            del kw['paycat']
[15055]249            del kw['paysession']
[11730]250        except KeyError:
251            pass
[15055]252        # Coursetickets can be filtered by level and session.
[15042]253        # These parameters are not keys of the catalog and must thus be
254        # removed from kw.
[14984]255        try:
256            del kw['ct_level']
257            del kw['ct_session']
[15546]258            del kw['ct_semester']
[14984]259        except KeyError:
260            pass
[15918]261        # Studylevels can be filtered by level_session.
262        # This parameter is not a key of the catalog and must thus be
263        # removed from kw.
264        try:
265            del kw['level_session']
266        except KeyError:
267            pass
[9801]268        query = StudentsQuery(**kw)
[9797]269        return query.query()
270
[12518]271    def get_selected(self, site, selected):
272        """Get set of selected students.
273        Returns a simple empty list or a list with `Student`
274        objects.
275        """
276        students = []
277        students_container = site.get('students', {})
278        for id in selected:
279            student = students_container.get(id, None)
[14443]280            if student is None:
281                # try matric number
282                result = list(StudentsQuery(matric_number=id).query())
283                if result:
284                    student = result[0]
285                else:
286                    continue
287            students.append(student)
[12518]288        return students
289
[8414]290    def export(self, values, filepath=None):
291        """Export `values`, an iterable, as CSV file.
292        If `filepath` is ``None``, a raw string with CSV data is returned.
293        """
294        writer, outfile = self.get_csv_writer(filepath)
295        for value in values:
296            self.write_item(value, writer)
297        return self.close_outfile(filepath, outfile)
298
[9797]299    def export_all(self, site, filepath=None):
300        """Export students into filepath as CSV data.
301        If `filepath` is ``None``, a raw string with CSV data is returned.
[9763]302        """
[9802]303        return self.export(self.filter_func(get_students(site)), filepath)
[8414]304
[9797]305    def export_student(self, student, filepath=None):
306        return self.export(self.filter_func([student]), filepath=filepath)
[9734]307
[9802]308    def export_filtered(self, site, filepath=None, **kw):
309        """Export items denoted by `kw`.
310        If `filepath` is ``None``, a raw string with CSV data should
311        be returned.
312        """
313        data = self.get_filtered(site, **kw)
[9844]314        return self.export(self.filter_func(data, **kw), filepath=filepath)
[9802]315
[12518]316    def export_selected(self,site, filepath=None, **kw):
317        """Export data for selected set of students.
318        """
319        selected = kw.get('selected', [])
320        data = self.get_selected(site, selected)
321        return self.export(self.filter_func(data, **kw), filepath=filepath)
[9802]322
[12518]323
[12079]324class StudentExporter(grok.GlobalUtility, StudentExporterBase):
[12861]325    """The Student Exporter first filters the set of students by searching the
326    students catalog. Then it exports student base data of this set of students.
[8414]327    """
[7944]328    grok.name('students')
329
[9936]330    fields = tuple(sorted(iface_names(IStudent))) + (
[9253]331        'password', 'state', 'history', 'certcode', 'is_postgrad',
[16186]332        'current_level', 'current_session', 'entry_session')
[15920]333    title = _(u'Students (Data Backup)')
[7944]334
[8493]335    def mangle_value(self, value, name, context=None):
[12861]336        """The mangler prepares the history messages and adds a hash symbol at
337        the end of the phone number to avoid annoying automatic number
338        transformation by Excel or Calc."""
[8493]339        if name == 'history':
340            value = value.messages
[14957]341        if 'phone' in name and value is not None:
[8971]342            # Append hash '#' to phone numbers to circumvent
343            # unwanted excel automatic
[8947]344            value = str('%s#' % value)
[8493]345        return super(
[12079]346            StudentExporter, self).mangle_value(
[8493]347            value, name, context=context)
348
[15966]349class TrimmedDataExporter(grok.GlobalUtility, StudentExporterBase):
350    """The Student Trimmed Data Exporter first filters the set of students
351    by searching the students catalog. Then it exports a trimmed data set
352    of this set of students.
353    """
354    grok.name('trimmed')
[7944]355
[15966]356    fields = (
357        'student_id',
358        'matric_number',
359        'reg_number',
360        'firstname',
361        'middlename',
362        'lastname',
363        'sex',
364        'email',
365        'phone',
366        'nationality',
367        'date_of_birth',
368        'state',
369        'current_mode',
370        'certcode',
371        'faccode',
372        'depcode',
373        'current_level',
374        'current_session',
[16186]375        'current_verdict',
376        'entry_session')
[15966]377    title = _(u'Students (Trimmed Data)')
378
379    def mangle_value(self, value, name, context=None):
380        """The mangler prepares the history messages and adds a hash symbol at
381        the end of the phone number to avoid annoying automatic number
382        transformation by Excel or Calc."""
383        if 'phone' in name and value is not None:
384            # Append hash '#' to phone numbers to circumvent
385            # unwanted excel automatic
386            value = str('%s#' % value)
387        return super(
388            TrimmedDataExporter, self).mangle_value(
389            value, name, context=context)
390
[8414]391class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
[12861]392    """The Student Study Course Exporter first filters the set of students
393    by searching the students catalog. Then it exports the data of the current
394    study course container of each student from this set. It does
395    not export their content.
[7994]396    """
397    grok.name('studentstudycourses')
398
[8493]399    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
[15920]400    title = _(u'Student Study Courses (Data Backup)')
[7994]401
[9844]402    def filter_func(self, x, **kw):
[9797]403        return get_studycourses(x)
404
[7994]405    def mangle_value(self, value, name, context=None):
[12861]406        """The mangler determines the certificate code and the student id.
[7994]407        """
408        if name == 'certificate' and value is not None:
409            # XXX: hopefully cert codes are unique site-wide
410            value = value.code
[8493]411        if name == 'student_id' and context is not None:
[8736]412            student = context.student
[8493]413            value = getattr(student, name, None)
[7994]414        return super(
415            StudentStudyCourseExporter, self).mangle_value(
416            value, name, context=context)
417
418
[8414]419class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
[12861]420    """The Student Study Level Exporter first filters the set of students
421    by searching the students catalog. Then it exports the data of the student's
[12862]422    study level container data but not their content (course tickets).
[12861]423    The exporter iterates over all objects in the students' ``studycourse``
424    containers.
[8015]425    """
426    grok.name('studentstudylevels')
427
[8493]428    fields = tuple(sorted(iface_names(
[12873]429        IStudentStudyLevel))) + (
[9253]430        'student_id', 'number_of_tickets','certcode')
[15920]431    title = _(u'Student Study Levels (Data Backup)')
[8015]432
[9844]433    def filter_func(self, x, **kw):
[15918]434        return get_levels(x, **kw)
[9802]435
[8015]436    def mangle_value(self, value, name, context=None):
[12861]437        """The mangler determines the student id, nothing else.
[8015]438        """
[8493]439        if name == 'student_id' and context is not None:
[8736]440            student = context.student
[8493]441            value = getattr(student, name, None)
[8015]442        return super(
443            StudentStudyLevelExporter, self).mangle_value(
444            value, name, context=context)
445
[8414]446class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
[13144]447    """The Course Ticket Exporter exports course tickets. Usually,
[12861]448    the exporter first filters the set of students by searching the
449    students catalog. Then it collects and iterates over all ``studylevel``
450    containers of the filtered student set and finally
451    iterates over all items inside these containers.
452
453    If the course code is passed through, the exporter uses a different
454    catalog. It searches for students in the course tickets catalog and
455    exports those course tickets which belong to the given course code and
[13263]456    also meet level and session passed through at the same time.
[12862]457    This happens if the exporter is called at course level in the academic
[12861]458    section.
[8342]459    """
460    grok.name('coursetickets')
461
[8493]462    fields = tuple(sorted(iface_names(ICourseTicket) +
[11484]463        ['level', 'code', 'level_session'])) + ('student_id',
464        'certcode', 'display_fullname')
[15920]465    title = _(u'Course Tickets (Data Backup)')
[8342]466
[9844]467    def filter_func(self, x, **kw):
468        return get_tickets(x, **kw)
[9802]469
[8342]470    def mangle_value(self, value, name, context=None):
[12861]471        """The mangler determines the student's id and fullname.
[8342]472        """
473        if context is not None:
[8736]474            student = context.student
[11484]475            if name in ('student_id', 'display_fullname') and student is not None:
[8342]476                value = getattr(student, name, None)
477        return super(
478            CourseTicketExporter, self).mangle_value(
479            value, name, context=context)
480
[15924]481class StudentPaymentExporter(grok.GlobalUtility, StudentExporterBase):
482    """The Student Payment Exporter first filters the set of students
483    by searching the students catalog. Then it exports student payment
484    tickets by iterating over the items of the student's ``payments``
485    container. If the payment period is given, only tickets, which were
486    paid in payment period, are considered for export.
487    """
488    grok.name('studentpayments')
489
490    fields = tuple(
491        sorted(iface_names(
492            IStudentOnlinePayment, exclude_attribs=False,
[16431]493            omit=['display_item', 'certificate', 'student', 'p_option']))) + (
[15924]494            'student_id','state','current_session')
495    title = _(u'Student Payments (Data Backup)')
496
497    def filter_func(self, x, **kw):
498        return get_payments(x, **kw)
499
500    def mangle_value(self, value, name, context=None):
501        """The mangler determines the student's id, registration
502        state and current session.
503        """
504        if context is not None:
505            student = context.student
[16351]506            if name in ['student_id','state','entry_session',
[15924]507                        'current_session'] and student is not None:
508                value = getattr(student, name, None)
509        return super(
510            StudentPaymentExporter, self).mangle_value(
511            value, name, context=context)
512
[15979]513class StudentTrimmedPaymentExporter(grok.GlobalUtility, StudentExporterBase):
514    """The Student Trimmed Payment Exporter is a slightly customized version
515    of the StudentPaymentExporter requested by Uniben.
516    """
517    grok.name('trimmedpayments')
518
519    fields = tuple(
520        sorted(iface_names(
521            IStudentOnlinePayment, exclude_attribs=False,
[16431]522            omit=['display_item', 'certificate', 'student', 'ac', 'p_option']))) + (
[15979]523            'student_id','faccode', 'depcode', 'state','current_session')
524    title = _(u'Student Payments (Sorted Data)')
525
526    def filter_func(self, x, **kw):
527        payments = get_payments(x, **kw)
528        return sorted([payment for payment in payments],
[15996]529            key=lambda payment: str(payment.p_category) + str(payment.student.faccode)
[15979]530                + str(payment.student.depcode) + str(payment.p_item))
531
532    def mangle_value(self, value, name, context=None):
533        """The mangler determines the student's id, registration
534        state and current session.
535        """
536        if context is not None:
537            student = context.student
538            if name in ['student_id','state', 'faccode', 'depcode',
539                        'current_session'] and student is not None:
540                value = getattr(student, name, None)
541        return super(
542            StudentTrimmedPaymentExporter, self).mangle_value(
543            value, name, context=context)
544
[13894]545class DataForLecturerExporter(grok.GlobalUtility, StudentExporterBase):
[15049]546    """The Data for Lecturer Exporter searches for students in the course
547    tickets catalog and exports those course tickets which belong to the
548    given course code, meet level and session passed through at the
[16412]549    same time, and which are editable by lecturers (disabled on 10/03/21).
550    This exporter can only be called at course level in the academic section.
[13766]551    """
552    grok.name('lecturer')
[8342]553
[13894]554    fields = ('matric_number', 'student_id', 'display_fullname',
[13766]555              'level', 'code', 'level_session', 'score')
556
557    title = _(u'Data for Lecturer')
558
[13894]559    def filter_func(self, x, **kw):
[16006]560        tickets = get_tickets_for_lecturer(x, **kw)
561        return sorted([ticket for ticket in tickets],
562            key=lambda ticket: str(ticket.fcode) + str(ticket.dcode)
563                + str(ticket.student.matric_number))
[13894]564
[13766]565    def mangle_value(self, value, name, context=None):
566        """The mangler determines the student's id and fullname.
567        """
568        if context is not None:
569            student = context.student
[13885]570            if name in ('matric_number',
571                        'reg_number',
572                        'student_id',
573                        'display_fullname',) and student is not None:
[13766]574                value = getattr(student, name, None)
575        return super(
[13894]576            DataForLecturerExporter, self).mangle_value(
[13766]577            value, name, context=context)
578
[15924]579class OutstandingCoursesExporter(grok.GlobalUtility, StudentExporterBase):
[15873]580    """The Student Outstanding Courses Exporter first filters the set of
581    students by searching the students catalog. Then it exports students with
582    lists of outstanding courses, i.e. courses which the student has
583    missed (not registered at all), failed (registered but not passed)
584    or nottaken (registered but not taken).
585    """
[15920]586    grok.name('outstandingcourses')
[15873]587
[15963]588    fields = ('student_id', 'matric_number', 'certcode', 'display_fullname',
589              'missed', 'failed', 'nottaken')
[15920]590    title = _(u'Outstanding Courses')
[15873]591
592    def filter_func(self, x, **kw):
593        return get_outstanding(x, **kw)
594
595    def mangle_value(self, value, name, context=None):
596        """The mangler determines the student's id, fullname and certcode,
597        and it collects the lists of outstanding courses.
598        """
599        if context is not None:
[15963]600            if name in ('student_id', 'matric_number',
601                        'display_fullname', 'certcode'):
[15873]602                value = getattr(context[0], name, None)
603            elif name == 'missed':
604                value = context[1]
605            elif name == 'failed':
606                value = context[2]
607            elif name == 'nottaken':
608                value = context[3]
609        return super(
[15924]610            OutstandingCoursesExporter, self).mangle_value(
[15873]611            value, name, context=context)
612
[15924]613class UnpaidPaymentsExporter(StudentPaymentExporter):
614    """The Unpaid Payments Exporter works just like the
615    Student Payment (singular intended) Exporter  but it exports only
616    unpaid tickets. This exporter is designed for finding and finally
617    purging outdated payment tickets.
[8371]618    """
[15920]619    grok.name('unpaidpayments')
[12971]620
[15920]621    title = _(u'Unpaid Payment Tickets')
[12971]622
623    def filter_func(self, x, **kw):
[14761]624        return get_payments(x, p_states=('unpaid',) , **kw)
[12971]625
[12865]626class DataForBursaryExporter(StudentPaymentExporter):
[15278]627    """The Data for Bursary Exporter works just like the Student Payment
628    Exporter but it exports much more information about the student. It combines
[12862]629    payment and student data in one table in order to spare postprocessing of
630    two seperate export files. The exporter is primarily used by bursary
[14761]631    officers who have exclusively access to this exporter. The exporter
[15792]632    exports ``paid``, ``waived`` and ``scholarship`` payment tickets.
[10233]633    """
634    grok.name('bursary')
635
[10296]636    def filter_func(self, x, **kw):
[15792]637        return get_payments(x, p_states=('paid', 'waived', 'scholarship'), **kw)
[10296]638
[10233]639    fields = tuple(
640        sorted(iface_names(
641            IStudentOnlinePayment, exclude_attribs=False,
[16431]642            omit=['display_item', 'certificate', 'student', 'p_option']))) + (
[11702]643            'student_id','matric_number','reg_number',
[16329]644            'firstname', 'middlename', 'lastname','sex',
[10236]645            'state','current_session',
646            'entry_session', 'entry_mode',
[13943]647            'faccode', 'depcode','certcode')
[10233]648    title = _(u'Payment Data for Bursary')
649
650    def mangle_value(self, value, name, context=None):
[12862]651        """The mangler fetches the student data.
[10233]652        """
653        if context is not None:
654            student = context.student
[10236]655            if name in [
[11702]656                'student_id','matric_number', 'reg_number',
[16333]657                'firstname', 'middlename', 'lastname','sex',
[11702]658                'state', 'current_session',
[10236]659                'entry_session', 'entry_mode',
[11702]660                'faccode', 'depcode', 'certcode'] and student is not None:
[10233]661                value = getattr(student, name, None)
662        return super(
[12865]663            StudentPaymentExporter, self).mangle_value(
[10233]664            value, name, context=context)
665
[15277]666class AccommodationPaymentsExporter(DataForBursaryExporter):
[15278]667    """The Accommodation Payments Exporter works like the Data for Bursary
[15792]668    Exporter above. The exporter exports ``paid``, ``waived`` and ``scholarship``
669    payment tickets with category ``bed_allocation`` or ``hostel_maintenance``.
[15278]670    The exporter is primarily used by accommodation officers who have
671    exclusively access to this exporter.
[15277]672    """
673    grok.name('accommodationpayments')
674
675    def filter_func(self, x, **kw):
676        kw['paycat'] = 'bed_allocation'
[15792]677        payments = get_payments(x, p_states=(
678          'paid', 'waived', 'scholarship'), **kw)
[15277]679        kw['paycat'] = 'hostel_maintenance'
[15792]680        payments += get_payments(x, p_states=(
681          'paid', 'waived', 'scholarship'), **kw)
[15277]682        return payments
683
684    title = _(u'Accommodation Payments')
685
[12865]686class BedTicketExporter(grok.GlobalUtility, StudentExporterBase):
687    """The Bed Ticket Exporter first filters the set of students
[12862]688    by searching the students catalog. Then it exports bed
689    tickets by iterating over the items of the student's ``accommodation``
690    container.
[9427]691    """
692    grok.name('bedtickets')
693
694    fields = tuple(
695        sorted(iface_names(
[9984]696            IBedTicket, exclude_attribs=False,
[13314]697            omit=['display_coordinates', 'maint_payment_made']))) + (
[9984]698            'student_id', 'actual_bed_type')
[15920]699    title = _(u'Bed Tickets (Data Backup)')
[9427]700
[9844]701    def filter_func(self, x, **kw):
[9802]702        return get_bedtickets(x)
703
[9427]704    def mangle_value(self, value, name, context=None):
[12862]705        """The mangler determines the student id and the type of the bed
706        which has been booked in the ticket.
[9427]707        """
708        if context is not None:
709            student = context.student
710            if name in ['student_id'] and student is not None:
711                value = getattr(student, name, None)
712        if name == 'bed' and value is not None:
713            value = getattr(value, 'bed_id', None)
714        if name == 'actual_bed_type':
[14395]715            value = getattr(getattr(context, 'bed', None), 'bed_type', None)
[9427]716        return super(
[12865]717            BedTicketExporter, self).mangle_value(
[9427]718            value, name, context=context)
719
[15047]720class SchoolFeePaymentsOverviewExporter(StudentExporter):
721    """The School Fee Payments Overview Exporter first filters the set of students
[12862]722    by searching the students catalog. Then it exports some student base data
723    together with the total school fee amount paid in each year over a
724    predefined year range (current year - 9, ... , current year + 1).
[9574]725    """
[15047]726    grok.name('sfpaymentsoverview')
[9574]727
728    curr_year = datetime.now().year
[14596]729    year_range = range(curr_year - 11, curr_year + 1)
[9574]730    year_range_tuple = tuple([str(year) for year in year_range])
[9983]731    fields = ('student_id', 'matric_number', 'display_fullname',
[9574]732        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
[13641]733        'current_level', 'current_session', 'current_mode',
734        'entry_session', 'reg_number'
[9574]735        ) + year_range_tuple
[15920]736    title = _(u'School Fee Payments Overview')
[9574]737
738    def mangle_value(self, value, name, context=None):
[12862]739        """The mangler summarizes the school fee amounts made per year. It
740        iterates over all paid school fee payment tickets and
741        adds together the amounts paid in a year. Waived payments
[15792]742        are marked ``waived`` and scholarship payments marked `scholarship`.
[12862]743        """
[9574]744        if name in self.year_range_tuple and context is not None:
[11661]745            value = 0
[9574]746            for ticket in context['payments'].values():
[12568]747                if ticket.p_category == 'schoolfee' and \
[9574]748                    ticket.p_session == int(name):
[12568]749                    if ticket.p_state == 'waived':
750                        value = 'waived'
751                        break
[15792]752                    if ticket.p_state == 'scholarship':
753                        value = 'scholarship'
754                        break
[12568]755                    if ticket.p_state == 'paid':
756                        try:
757                            value += ticket.amount_auth
758                        except TypeError:
759                            pass
[11661]760            if value == 0:
761                value = ''
[14367]762            elif isinstance(value, float):
763                value = round(value, 2)
[9574]764        return super(
[12079]765            StudentExporter, self).mangle_value(
[9734]766            value, name, context=context)
[9744]767
[15051]768class SessionPaymentsOverviewExporter(StudentExporter):
769    """The Session Payments Overview Exporter first filters the set of students
770    by searching the students catalog. Then it exports some student base data
[15060]771    together with the total amount paid in predefined payment categories
772    over the previous three session (referring to current academic session).
773    Sample output:
774
775    header: ``...schoolfee13,schoolfee14,schoolfee15,gown13,gown14,gown15...``
776
777    data: ``...2000.0,,3000.0,,,1000.0,...``
778
[15062]779    This csv data string means that the student paid 2000.0 school fee in 2013
780    and 3000.0 in 2015. S/He furthermore paid 1000.0 for gown rental in 2015.
[15051]781    """
782    grok.name('sessionpaymentsoverview')
783
784    paycats = ('schoolfee', 'clearance', 'gown', 'transcript')
[15060]785    regular_fields = ('student_id', 'matric_number', 'display_fullname',
[15051]786        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
787        'current_level', 'current_session', 'current_mode',
788        'entry_session', 'reg_number'
[15060]789        )
[15051]790    title = _(u'Session Payments Overview')
791
[15060]792    @property
793    def paycatyears(self):
794        cas = grok.getSite()['configuration'].current_academic_session
795        paycatyears = []
796        if cas:
[15918]797            year_range = range(cas-2, cas+1)
[15060]798            year_range_tuple = tuple([str(year)[2:] for year in year_range])
799            paycatyears = [
800                cat+year for cat in self.paycats for year in year_range_tuple]
801        return paycatyears
802
803    @property
804    def fields(self):
805        return self.regular_fields + tuple(self.paycatyears)
806
[15051]807    def mangle_value(self, value, name, context=None):
808        """
809        """
[15060]810        amounts = dict()
811        for catyear in self.paycatyears:
812            amounts[catyear] = 0.0
813        if name[:-2] in self.paycats and context is not None:
[15051]814            for ticket in context['payments'].values():
[15060]815                if ticket.p_category == name[:-2]:
[15051]816                    if ticket.p_state in ('waived', 'paid'):
[15060]817                        if str(ticket.p_session)[2:] == name[-2:]:
818                            amounts[name] += ticket.amount_auth
819            if amounts[name] == 0.0:
820                value = ''
821            elif isinstance(amounts[name], float):
822                value = round(amounts[name], 2)
[15051]823        return super(
824            StudentExporter, self).mangle_value(
825            value, name, context=context)
826
[15921]827class StudyLevelsOverviewExporter(StudentExporter):
[12862]828    """The Student Study Levels Overview Exporter first filters the set of
829    students by searching the students catalog. Then it exports some student
830    base data together with the session key of registered levels.
831    Sample output:
832
833    header: ``...100,110,120,200,210,220,300...``
834
835    data: ``...2010,,,2011,2012,,2013...``
836
837    This csv data string means that level 100 was registered in session
838    2010/2011, level 200 in session 2011/2012, level 210 (200 on 1st probation)
839    in session 2012/2013 and level 300 in session 2013/2014.
[9744]840    """
841    grok.name('studylevelsoverview')
842
[9787]843    avail_levels = tuple([str(x) for x in study_levels(None)])
[9744]844
845    fields = ('student_id', ) + (
846        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
[9761]847        'entry_session', 'current_level', 'current_session',
[9787]848        ) + avail_levels
[15920]849    title = _(u'Study Levels Overview')
[9744]850
851    def mangle_value(self, value, name, context=None):
[12862]852        """The mangler checks if a given level has been registered. It returns
853        the ``level_session`` attribute of the student study level object
854        if the named level exists.
855        """
[9787]856        if name in self.avail_levels and context is not None:
[9744]857            value = ''
858            for level in context['studycourse'].values():
859                if level.level == int(name):
[9761]860                    value = '%s' % level.level_session
[9744]861                    break
862        return super(
[12079]863            StudentExporter, self).mangle_value(
[9744]864            value, name, context=context)
[9936]865
866class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase):
[12862]867    """Like all other exporters the Combo Card Data Exporter first filters the
868    set of students by searching the students catalog. Then it exports some
869    student base data which are neccessary to print for the Interswitch combo
870    card (identity card for students). The output contains a ``passport_path``
871    column which contains the filesystem path of the passport image file.
872    If no path is given, no passport image file exists.
[9936]873    """
874    grok.name('combocard')
875
876    fields = ('display_fullname',
[9937]877              'student_id','matric_number',
878              'certificate', 'faculty', 'department', 'passport_path')
[9936]879    title = _(u'Combo Card Data')
880
881    def mangle_value(self, value, name, context=None):
[12862]882        """The mangler determines the titles of faculty, department
883        and certificate. It also computes the path of passport image file
884        stored in the filesystem.
885        """
[9936]886        certificate = context['studycourse'].certificate
887        if name == 'certificate' and certificate is not None:
888            value = certificate.title
889        if name == 'department' and certificate is not None:
[10650]890            value = certificate.__parent__.__parent__.longtitle
[9936]891        if name == 'faculty' and certificate is not None:
[10650]892            value = certificate.__parent__.__parent__.__parent__.longtitle
[9937]893        if name == 'passport_path' and certificate is not None:
[12862]894            file_id = IFileStoreNameChooser(context).chooseName(
895                attr='passport.jpg')
[9937]896            os_path = getUtility(IExtFileStore)._pathFromFileID(file_id)
897            if not os.path.exists(os_path):
898                value = None
899            else:
900                value = '/'.join(os_path.split('/')[-4:])
[9936]901        return super(
902            ComboCardDataExporter, self).mangle_value(
[15920]903            value, name, context=context)
904
[15921]905class TranscriptDataExporter(StudentExporter):
906    """The Transcript Data Exporter first filters the set of
[15920]907    students by searching the students catalog. Then it exports student data
908    along with their transcript data.
909    """
910    grok.name('transcriptdata')
911
912    fields = ('student_id', ) + (
913        'state', 'certcode', 'faccode', 'depcode',
914        'entry_session', 'current_level', 'current_session',
915        'transcript_data')
916    title = _(u'Transcript Data')
917
918    def mangle_value(self, value, name, context=None):
[15924]919        """The mangler determines and formats the transcript data.
[15920]920        """
921        if name == 'transcript_data':
922            value = {}
923            td = context['studycourse'].getTranscriptData()[0]
924            for level in td:
925                tickets_1 = ','.join(i.code for i in level['tickets_1'])
926                tickets_2 = ','.join(i.code for i in level['tickets_2'])
927                tickets_3 = ','.join(i.code for i in level['tickets_3'])
928                value = "Level %s; 1st: %s; 2nd: %s; 3rd: %s; sgpa: %s" % (
929                    level['level_key'], tickets_1, tickets_2,
930                    tickets_3, level['sgpa'],
931                    )
932        return super(
[15921]933            TranscriptDataExporter, self).mangle_value(
[9936]934            value, name, context=context)
Note: See TracBrowser for help on using the repository browser.