Changeset 7740


Ignore:
Timestamp:
1 Mar 2012, 09:23:16 (13 years ago)
Author:
uli
Message:

Add course exporter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/university/export.py

    r7732 r7740  
    118118                self.write_item(department, writer)
    119119        return self.close_outfile(filepath, outfile)
     120
     121
     122class CourseExporter(FacultyExporter, grok.GlobalUtility):
     123    """Exporter for courses.
     124    """
     125    grok.implements(ICSVExporter)
     126    grok.name('courses')
     127
     128    #: Fieldnames considered by this exporter
     129    fields = ('code', 'faculty', 'department', 'title', 'credits',
     130              'passmark', 'semester')
     131
     132    def mangle_value(self, value, name, context=None):
     133        """Hook for mangling values in derived classes
     134        """
     135        if name == 'faculty':
     136            try:
     137                value = context.__parent__.__parent__.__parent__.code
     138            except AttributeError:
     139                value = None
     140        elif name == 'department':
     141            value = getattr(
     142                getattr(context, '__parent__', None),
     143                'code', None)
     144        return super(CourseExporter, self).mangle_value(
     145            value, name, context)
     146
     147    def export_all(self, site, filepath=None):
     148        """Export faculties in facultycontainer into filepath as CSV data.
     149
     150        If `filepath` is ``None``, a raw string with CSV data is returned.
     151        """
     152        writer, outfile = self.get_csv_writer(filepath)
     153        faculties = site.get('faculties', {})
     154        for faculty in faculties.values():
     155            for department in faculty.values():
     156                for course in department.courses.values():
     157                    self.write_item(course, writer)
     158        return self.close_outfile(filepath, outfile)
Note: See TracChangeset for help on using the changeset viewer.