source: main/waeup.kofa/branches/0.2/src/waeup/kofa/students/export.py @ 17931

Last change on this file since 17931 was 10650, checked in by Henrik Bettermann, 11 years ago

Convert longtitle() into property attribute.

  • Property svn:keywords set to Id
File size: 17.4 KB
Line 
1## $Id: export.py 10650 2013-09-25 06:46:53Z 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_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
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
116
117class StudentExporterBase(ExporterBase):
118    """Exporter for students or related objects.
119
120    This is a baseclass.
121    """
122    grok.baseclass()
123    grok.implements(ICSVStudentExporter)
124    grok.provides(ICSVStudentExporter)
125
126    def filter_func(self, x, **kw):
127        return x
128
129    def get_filtered(self, site, **kw):
130        """Get students from a catalog filtered by keywords.
131
132        students_catalog is the default catalog. The keys must be valid
133        catalog index names.
134        Returns a simple empty list, a list with `Student`
135        objects or a catalog result set with `Student`
136        objects.
137
138        .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog`
139
140        """
141        # Pass only given keywords to create FilteredCatalogQuery objects.
142        # This way we avoid
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.
147        if kw.get('catalog', None) == 'coursetickets':
148            coursetickets = CourseTicketsQuery(**kw).query()
149            students = []
150            for ticket in coursetickets:
151                students.append(ticket.student)
152            return list(set(students))
153        query = StudentsQuery(**kw)
154        return query.query()
155
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
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.
170        """
171        return self.export(self.filter_func(get_students(site)), filepath)
172
173    def export_student(self, student, filepath=None):
174        return self.export(self.filter_func([student]), filepath=filepath)
175
176    def export_filtered(self, site, filepath=None, **kw):
177        """Export items denoted by `kw`.
178
179        If `filepath` is ``None``, a raw string with CSV data should
180        be returned.
181        """
182        data = self.get_filtered(site, **kw)
183        return self.export(self.filter_func(data, **kw), filepath=filepath)
184
185
186class StudentsExporter(grok.GlobalUtility, StudentExporterBase):
187    """Exporter for Students.
188    """
189    grok.name('students')
190
191    #: Fieldnames considered by this exporter
192    fields = tuple(sorted(iface_names(IStudent))) + (
193        'password', 'state', 'history', 'certcode', 'is_postgrad',
194        'current_level', 'current_session')
195
196    #: The title under which this exporter will be displayed
197    title = _(u'Students')
198
199    def mangle_value(self, value, name, context=None):
200        if name == 'history':
201            value = value.messages
202        if name == 'phone' and value is not None:
203            # Append hash '#' to phone numbers to circumvent
204            # unwanted excel automatic
205            value = str('%s#' % value)
206        return super(
207            StudentsExporter, self).mangle_value(
208            value, name, context=context)
209
210
211class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
212    """Exporter for StudentStudyCourses.
213    """
214    grok.name('studentstudycourses')
215
216    #: Fieldnames considered by this exporter
217    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
218
219    #: The title under which this exporter will be displayed
220    title = _(u'Student Study Courses')
221
222    def filter_func(self, x, **kw):
223        return get_studycourses(x)
224
225    def mangle_value(self, value, name, context=None):
226        """Treat location values special.
227        """
228        if name == 'certificate' and value is not None:
229            # XXX: hopefully cert codes are unique site-wide
230            value = value.code
231        if name == 'student_id' and context is not None:
232            student = context.student
233            value = getattr(student, name, None)
234        return super(
235            StudentStudyCourseExporter, self).mangle_value(
236            value, name, context=context)
237
238
239class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
240    """Exporter for StudentStudyLevels.
241    """
242    grok.name('studentstudylevels')
243
244    #: Fieldnames considered by this exporter
245    fields = tuple(sorted(iface_names(
246        IStudentStudyLevel) + ['level'])) + (
247        'student_id', 'number_of_tickets','certcode')
248
249    #: The title under which this exporter will be displayed
250    title = _(u'Student Study Levels')
251
252    def filter_func(self, x, **kw):
253        return get_levels(x)
254
255    def mangle_value(self, value, name, context=None):
256        """Treat location values special.
257        """
258        if name == 'student_id' and context is not None:
259            student = context.student
260            value = getattr(student, name, None)
261        return super(
262            StudentStudyLevelExporter, self).mangle_value(
263            value, name, context=context)
264
265class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
266    """Exporter for CourseTickets.
267    """
268    grok.name('coursetickets')
269
270    #: Fieldnames considered by this exporter
271    fields = tuple(sorted(iface_names(ICourseTicket) +
272        ['level', 'code', 'level_session'])) + ('student_id', 'certcode')
273
274    #: The title under which this exporter will be displayed
275    title = _(u'Course Tickets')
276
277    def filter_func(self, x, **kw):
278        return get_tickets(x, **kw)
279
280    def mangle_value(self, value, name, context=None):
281        """Treat location values special.
282        """
283        if context is not None:
284            student = context.student
285            if name == 'student_id' and student is not None:
286                value = getattr(student, name, None)
287            if name == 'level':
288                value = getattr(context, 'level', lambda: None)
289            if name == 'level_session':
290                value = getattr(context, 'level_session', lambda: None)
291        return super(
292            CourseTicketExporter, self).mangle_value(
293            value, name, context=context)
294
295
296class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase):
297    """Exporter for OnlinePayment instances.
298    """
299    grok.name('studentpayments')
300
301    #: Fieldnames considered by this exporter
302    fields = tuple(
303        sorted(iface_names(
304            IStudentOnlinePayment, exclude_attribs=False,
305            omit=['display_item']))) + (
306            'student_id','state','current_session')
307
308    #: The title under which this exporter will be displayed
309    title = _(u'Student Payments')
310
311    def filter_func(self, x, **kw):
312        return get_payments(x)
313
314    def mangle_value(self, value, name, context=None):
315        """Treat location values special.
316        """
317        if context is not None:
318            student = context.student
319            if name in ['student_id','state',
320                        'current_session'] and student is not None:
321                value = getattr(student, name, None)
322        return super(
323            StudentPaymentsExporter, self).mangle_value(
324            value, name, context=context)
325
326class DataForBursaryExporter(StudentPaymentsExporter):
327    """Exporter for OnlinePayment instances.
328    """
329    grok.name('bursary')
330
331    def filter_func(self, x, **kw):
332        return get_paid_payments(x)
333
334    #: Fieldnames considered by this exporter
335    fields = tuple(
336        sorted(iface_names(
337            IStudentOnlinePayment, exclude_attribs=False,
338            omit=['display_item']))) + (
339            'student_id','matric_number',
340            'firstname', 'middlename', 'lastname',
341            'state','current_session',
342            'entry_session', 'entry_mode',
343            'faccode', 'depcode')
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
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:
359                value = getattr(student, name, None)
360        return super(
361            StudentPaymentsExporter, self).mangle_value(
362            value, name, context=context)
363
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(
372            IBedTicket, exclude_attribs=False,
373            omit=['display_coordinates']))) + (
374            'student_id', 'actual_bed_type')
375
376    #: The title under which this exporter will be displayed
377    title = _(u'Bed Tickets')
378
379    def filter_func(self, x, **kw):
380        return get_bedtickets(x)
381
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
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
407    fields = ('student_id', 'matric_number', 'display_fullname',
408        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
409        'current_level', 'current_session', 'current_mode',
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(
426            value, name, context=context)
427
428class StudentStudyLevelsOverviewExporter(StudentsExporter):
429    """Exporter for students with study level overview.
430    """
431    grok.name('studylevelsoverview')
432
433    avail_levels = tuple([str(x) for x in study_levels(None)])
434
435    #: Fieldnames considered by this exporter
436    fields = ('student_id', ) + (
437        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
438        'entry_session', 'current_level', 'current_session',
439        ) + avail_levels
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):
445        if name in self.avail_levels and context is not None:
446            value = ''
447            for level in context['studycourse'].values():
448                if level.level == int(name):
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
455                    break
456        return super(
457            StudentsExporter, self).mangle_value(
458            value, name, context=context)
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',
467              'student_id','matric_number',
468              'certificate', 'faculty', 'department', 'passport_path')
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
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:])
488        return super(
489            ComboCardDataExporter, self).mangle_value(
490            value, name, context=context)
Note: See TracBrowser for help on using the repository browser.