source: main/waeup.kofa/branches/uli-stud-utils-cleanup/src/waeup/kofa/students/export.py @ 13606

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

Make all datetimes timezone aware. Otherwise we can't compare.

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