Changeset 7732


Ignore:
Timestamp:
29 Feb 2012, 18:34:38 (13 years ago)
Author:
uli
Message:
  • Add exporter for department.
  • Reflect changes in ICSVExporter interface.
  • Use utility names that could also be used in export filenames.
File:
1 edited

Legend:

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

    r7728 r7732  
    1010    """
    1111    grok.implements(ICSVExporter)
    12     grok.name('Faculty Exporter')
     12    grok.name('faculties')
    1313
    1414    #: Fieldnames considered by this exporter
     
    6666        return
    6767
    68     def export(self, faculty, filepath=None):
    69         """Export single `faculty` as CSV file.
     68    def export(self, faculties, filepath=None):
     69        """Export `faculties`, an iterable, as CSV file.
    7070
    7171        If `filepath` is ``None``, a raw string with CSV data is returned.
    7272        """
    7373        writer, outfile = self.get_csv_writer(filepath)
    74         self.write_item(faculty, writer)
     74        for faculty in faculties:
     75            self.write_item(faculty, writer)
    7576        return self.close_outfile(filepath, outfile)
    7677
     
    8283        writer, outfile = self.get_csv_writer(filepath)
    8384        faculties = site.get('faculties', {})
     85        return self.export(faculties.values(), filepath)
    8486        for faculty in faculties.values():
    8587            self.write_item(faculty, writer)
    8688        return self.close_outfile(filepath, outfile)
     89
     90class DepartmentExporter(FacultyExporter, grok.GlobalUtility):
     91    """Exporter for departments.
     92    """
     93    grok.implements(ICSVExporter)
     94    grok.name('departments')
     95
     96    #: Fieldnames considered by this exporter
     97    fields = ('code', 'faculty', 'title', 'title_prefix')
     98
     99    def mangle_value(self, value, name, context=None):
     100        """Hook for mangling values in derived classes
     101        """
     102        if name == 'faculty':
     103            value = getattr(
     104                getattr(context, '__parent__', None),
     105                'code', None)
     106        return super(DepartmentExporter, self).mangle_value(
     107            value, name, context)
     108
     109    def export_all(self, site, filepath=None):
     110        """Export faculties in facultycontainer into filepath as CSV data.
     111
     112        If `filepath` is ``None``, a raw string with CSV data is returned.
     113        """
     114        writer, outfile = self.get_csv_writer(filepath)
     115        faculties = site.get('faculties', {})
     116        for faculty in faculties.values():
     117            for department in faculty.values():
     118                self.write_item(department, writer)
     119        return self.close_outfile(filepath, outfile)
Note: See TracChangeset for help on using the changeset viewer.