Ignore:
Timestamp:
4 May 2012, 11:52:58 (13 years ago)
Author:
uli
Message:

Add exporter for course tickets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/export.py

    r8160 r8342  
    2424from waeup.kofa.interfaces import MessageFactory as _
    2525from waeup.kofa.students.interfaces import (
    26     IStudent, IStudentStudyCourse, IStudentStudyLevel,)
     26    IStudent, IStudentStudyCourse, IStudentStudyLevel, ICourseTicket)
    2727from waeup.kofa.utils.batching import ExporterBase
    2828from waeup.kofa.utils.helpers import iface_names
     
    186186                levels.append(level)
    187187        return self.export(levels, filepath)
     188
     189class CourseTicketExporter(grok.GlobalUtility, ExporterBase):
     190    """Exporter for CourseTickets.
     191    """
     192    grok.implements(ICSVExporter)
     193    grok.name('coursetickets')
     194
     195    #: Fieldnames considered by this exporter
     196    fields = tuple(sorted(iface_names(ICourseTicket) + [
     197        'reg_number', 'matric_number', 'level', 'code', 'title', 'credits',
     198        'passmark', 'semester', 'fcode', 'dcode']))
     199
     200    #: The title under which this exporter will be displayed
     201    title = _(u'Course Tickets')
     202
     203    def mangle_value(self, value, name, context=None):
     204        """Treat location values special.
     205        """
     206        if context is not None:
     207            student = context.getStudent()
     208            if name in ['reg_number', 'matric_number'] and student is not None:
     209                value = getattr(student, name, None)
     210            if name == 'level':
     211                value = context.getLevel()
     212        return super(
     213            CourseTicketExporter, self).mangle_value(
     214            value, name, context=context)
     215
     216    def export(self, course_tickets, filepath=None):
     217        """Export `course_tickets`, an iterable, as CSV file.
     218
     219        If `filepath` is ``None``, a raw string with CSV data is returned.
     220        """
     221        writer, outfile = self.get_csv_writer(filepath)
     222        for ticket in course_tickets:
     223            self.write_item(ticket, writer)
     224        return self.close_outfile(filepath, outfile)
     225
     226    def export_all(self, site, filepath=None):
     227        """Export course tickets into filepath as CSV data.
     228
     229        If `filepath` is ``None``, a raw string with CSV data is returned.
     230        """
     231        catalog = queryUtility(
     232            ICatalog, context=site, name='students_catalog', default=None)
     233        if catalog is None:
     234            return self.export([], filepath)
     235        students = catalog.searchResults(
     236            student_id=(None, None))
     237        levels = []
     238        study_courses = [x.get('studycourse', None) for x in students
     239                         if x is not None]
     240        for course in study_courses:
     241            for level in course.values():
     242                levels.append(level)
     243
     244        tickets = []
     245        for level in levels:
     246            for ticket in level.values():
     247                tickets.append(ticket)
     248        return self.export(tickets, filepath)
     249
Note: See TracChangeset for help on using the changeset viewer.