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

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

Export passport file path.

  • Property svn:keywords set to Id
File size: 15.2 KB
Line 
1## $Id: export.py 9937 2013-02-07 09:51:37Z 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')
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.
66    """
67    tickets = []
68    code = kw.get('code', None)
69    if code is None:
70        for level in get_levels(students):
71            for ticket in level.values():
72                tickets.append(ticket)
73    else:
74        for level in get_levels(students):
75            for ticket in level.values():
76                if ticket.code == code:
77                    tickets.append(ticket)
78    return tickets
79
80def get_payments(students):
81    """Get all payments of `students`.
82    """
83    payments = []
84    for student in students:
85        for payment in student.get('payments', {}).values():
86            payments.append(payment)
87    return payments
88
89def get_bedtickets(students):
90    """Get all bedtickets of `students`.
91    """
92    tickets = []
93    for student in students:
94        for ticket in student.get('accommodation', {}).values():
95            tickets.append(ticket)
96    return tickets
97
98class StudentExporterBase(ExporterBase):
99    """Exporter for students or related objects.
100
101    This is a baseclass.
102    """
103    grok.baseclass()
104    grok.implements(ICSVStudentExporter)
105    grok.provides(ICSVStudentExporter)
106
107    def filter_func(self, x, **kw):
108        return x
109
110    def get_filtered(self, site, **kw):
111        """Get students from a catalog filtered by keywords.
112
113        students_catalog is the default catalog. The keys must be valid
114        catalog index names.
115        Returns a simple empty list, a list with `Student`
116        objects or a catalog result set with `Student`
117        objects.
118
119        .. seealso:: `waeup.kofa.students.catalog.StudentsCatalog`
120
121        """
122        # Pass only given keywords to create FilteredCatalogQuery objects.
123        # This way we avoid
124        # trouble with `None` value ambivalences and queries are also
125        # faster (normally less indexes to ask). Drawback is, that
126        # developers must look into catalog to see what keywords are
127        # valid.
128        if kw.get('catalog', None) == 'coursetickets':
129            coursetickets = CourseTicketsQuery(**kw).query()
130            students = []
131            for ticket in coursetickets:
132                students.append(ticket.student)
133            return list(set(students))
134        query = StudentsQuery(**kw)
135        return query.query()
136
137    def export(self, values, filepath=None):
138        """Export `values`, an iterable, as CSV file.
139
140        If `filepath` is ``None``, a raw string with CSV data is returned.
141        """
142        writer, outfile = self.get_csv_writer(filepath)
143        for value in values:
144            self.write_item(value, writer)
145        return self.close_outfile(filepath, outfile)
146
147    def export_all(self, site, filepath=None):
148        """Export students into filepath as CSV data.
149
150        If `filepath` is ``None``, a raw string with CSV data is returned.
151        """
152        return self.export(self.filter_func(get_students(site)), filepath)
153
154    def export_student(self, student, filepath=None):
155        return self.export(self.filter_func([student]), filepath=filepath)
156
157    def export_filtered(self, site, filepath=None, **kw):
158        """Export items denoted by `kw`.
159
160        If `filepath` is ``None``, a raw string with CSV data should
161        be returned.
162        """
163        data = self.get_filtered(site, **kw)
164        return self.export(self.filter_func(data, **kw), filepath=filepath)
165
166
167class StudentsExporter(grok.GlobalUtility, StudentExporterBase):
168    """Exporter for Students.
169    """
170    grok.name('students')
171
172    #: Fieldnames considered by this exporter
173    fields = tuple(sorted(iface_names(IStudent))) + (
174        'password', 'state', 'history', 'certcode', 'is_postgrad',
175        'current_level', 'current_session')
176
177    #: The title under which this exporter will be displayed
178    title = _(u'Students')
179
180    def mangle_value(self, value, name, context=None):
181        if name == 'history':
182            value = value.messages
183        if name == 'phone' and value is not None:
184            # Append hash '#' to phone numbers to circumvent
185            # unwanted excel automatic
186            value = str('%s#' % value)
187        return super(
188            StudentsExporter, self).mangle_value(
189            value, name, context=context)
190
191
192class StudentStudyCourseExporter(grok.GlobalUtility, StudentExporterBase):
193    """Exporter for StudentStudyCourses.
194    """
195    grok.name('studentstudycourses')
196
197    #: Fieldnames considered by this exporter
198    fields = tuple(sorted(iface_names(IStudentStudyCourse))) + ('student_id',)
199
200    #: The title under which this exporter will be displayed
201    title = _(u'Student Study Courses')
202
203    def filter_func(self, x, **kw):
204        return get_studycourses(x)
205
206    def mangle_value(self, value, name, context=None):
207        """Treat location values special.
208        """
209        if name == 'certificate' and value is not None:
210            # XXX: hopefully cert codes are unique site-wide
211            value = value.code
212        if name == 'student_id' and context is not None:
213            student = context.student
214            value = getattr(student, name, None)
215        return super(
216            StudentStudyCourseExporter, self).mangle_value(
217            value, name, context=context)
218
219
220class StudentStudyLevelExporter(grok.GlobalUtility, StudentExporterBase):
221    """Exporter for StudentStudyLevels.
222    """
223    grok.name('studentstudylevels')
224
225    #: Fieldnames considered by this exporter
226    fields = tuple(sorted(iface_names(
227        IStudentStudyLevel) + ['level'])) + (
228        'student_id', 'number_of_tickets','certcode')
229
230    #: The title under which this exporter will be displayed
231    title = _(u'Student Study Levels')
232
233    def filter_func(self, x, **kw):
234        return get_levels(x)
235
236    def mangle_value(self, value, name, context=None):
237        """Treat location values special.
238        """
239        if name == 'student_id' and context is not None:
240            student = context.student
241            value = getattr(student, name, None)
242        return super(
243            StudentStudyLevelExporter, self).mangle_value(
244            value, name, context=context)
245
246class CourseTicketExporter(grok.GlobalUtility, StudentExporterBase):
247    """Exporter for CourseTickets.
248    """
249    grok.name('coursetickets')
250
251    #: Fieldnames considered by this exporter
252    fields = tuple(sorted(iface_names(ICourseTicket) +
253        ['level', 'code', 'level_session'])) + ('student_id', 'certcode')
254
255    #: The title under which this exporter will be displayed
256    title = _(u'Course Tickets')
257
258    def filter_func(self, x, **kw):
259        return get_tickets(x, **kw)
260
261    def mangle_value(self, value, name, context=None):
262        """Treat location values special.
263        """
264        if context is not None:
265            student = context.student
266            if name == 'student_id' and student is not None:
267                value = getattr(student, name, None)
268            if name == 'level':
269                value = getattr(context, 'level', lambda: None)
270            if name == 'level_session':
271                value = getattr(context, 'level_session', lambda: None)
272        return super(
273            CourseTicketExporter, self).mangle_value(
274            value, name, context=context)
275
276
277class StudentPaymentsExporter(grok.GlobalUtility, StudentExporterBase):
278    """Exporter for OnlinePayment instances.
279    """
280    grok.name('studentpayments')
281
282    #: Fieldnames considered by this exporter
283    fields = tuple(
284        sorted(iface_names(
285            IStudentOnlinePayment, exclude_attribs=False))) + (
286                'student_id','student_state','current_session')
287
288    #: The title under which this exporter will be displayed
289    title = _(u'Student Payments')
290
291    def filter_func(self, x, **kw):
292        return get_payments(x)
293
294    def mangle_value(self, value, name, context=None):
295        """Treat location values special.
296        """
297        if context is not None:
298            student = context.student
299            if name in ['student_id'] and student is not None:
300                value = getattr(student, name, None)
301        return super(
302            StudentPaymentsExporter, self).mangle_value(
303            value, name, context=context)
304
305class BedTicketsExporter(grok.GlobalUtility, StudentExporterBase):
306    """Exporter for BedTicket instances.
307    """
308    grok.name('bedtickets')
309
310    #: Fieldnames considered by this exporter
311    fields = tuple(
312        sorted(iface_names(
313            IBedTicket, exclude_attribs=False))) + (
314                'student_id', 'actual_bed_type')
315
316    #: The title under which this exporter will be displayed
317    title = _(u'Bed Tickets')
318
319    def filter_func(self, x, **kw):
320        return get_bedtickets(x)
321
322    def mangle_value(self, value, name, context=None):
323        """Treat location values and others special.
324        """
325        if context is not None:
326            student = context.student
327            if name in ['student_id'] and student is not None:
328                value = getattr(student, name, None)
329        if name == 'bed' and value is not None:
330            value = getattr(value, 'bed_id', None)
331        if name == 'actual_bed_type':
332            value = getattr(getattr(context, 'bed', None), 'bed_type')
333        return super(
334            BedTicketsExporter, self).mangle_value(
335            value, name, context=context)
336
337class StudentPaymentsOverviewExporter(StudentsExporter):
338    """Exporter for students with payment overview.
339    """
340    grok.name('paymentsoverview')
341
342    curr_year = datetime.now().year
343    year_range = range(curr_year - 9, curr_year + 1)
344    year_range_tuple = tuple([str(year) for year in year_range])
345
346    #: Fieldnames considered by this exporter
347    fields = ('student_id', ) + (
348        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
349        'current_level', 'current_session', 'current_mode',
350        ) + year_range_tuple
351
352    #: The title under which this exporter will be displayed
353    title = _(u'Student Payments Overview')
354
355    def mangle_value(self, value, name, context=None):
356        if name in self.year_range_tuple and context is not None:
357            value = ''
358            for ticket in context['payments'].values():
359                if ticket.p_state == 'paid' and \
360                    ticket.p_category == 'schoolfee' and \
361                    ticket.p_session == int(name):
362                    value = ticket.amount_auth
363                    break
364        return super(
365            StudentsExporter, self).mangle_value(
366            value, name, context=context)
367
368class StudentStudyLevelsOverviewExporter(StudentsExporter):
369    """Exporter for students with study level overview.
370    """
371    grok.name('studylevelsoverview')
372
373    avail_levels = tuple([str(x) for x in study_levels(None)])
374
375    #: Fieldnames considered by this exporter
376    fields = ('student_id', ) + (
377        'state', 'certcode', 'faccode', 'depcode', 'is_postgrad',
378        'entry_session', 'current_level', 'current_session',
379        ) + avail_levels
380
381    #: The title under which this exporter will be displayed
382    title = _(u'Student Study Levels Overview')
383
384    def mangle_value(self, value, name, context=None):
385        if name in self.avail_levels and context is not None:
386            value = ''
387            for level in context['studycourse'].values():
388                if level.level == int(name):
389                    #value = '%s|%s|%s|%s' % (
390                    #    level.level_session,
391                    #    len(level),
392                    #    level.validated_by,
393                    #    level.level_verdict)
394                    value = '%s' % level.level_session
395                    break
396        return super(
397            StudentsExporter, self).mangle_value(
398            value, name, context=context)
399
400class ComboCardDataExporter(grok.GlobalUtility, StudentExporterBase):
401    """Exporter for Interswitch Combo Card Data.
402    """
403    grok.name('combocard')
404
405    #: Fieldnames considered by this exporter
406    fields = ('display_fullname',
407              'student_id','matric_number',
408              'certificate', 'faculty', 'department', 'passport_path')
409
410    #: The title under which this exporter will be displayed
411    title = _(u'Combo Card Data')
412
413    def mangle_value(self, value, name, context=None):
414        certificate = context['studycourse'].certificate
415        if name == 'certificate' and certificate is not None:
416            value = certificate.title
417        if name == 'department' and certificate is not None:
418            value = certificate.__parent__.__parent__.longtitle()
419        if name == 'faculty' and certificate is not None:
420            value = certificate.__parent__.__parent__.__parent__.longtitle()
421        if name == 'passport_path' and certificate is not None:
422            file_id = IFileStoreNameChooser(context).chooseName(attr='passport.jpg')
423            os_path = getUtility(IExtFileStore)._pathFromFileID(file_id)
424            if not os.path.exists(os_path):
425                value = None
426            else:
427                value = '/'.join(os_path.split('/')[-4:])
428        return super(
429            ComboCardDataExporter, self).mangle_value(
430            value, name, context=context)
Note: See TracBrowser for help on using the repository browser.