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

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

Add more columns to bursary export file.

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