Ignore:
Timestamp:
19 Mar 2012, 01:57:34 (13 years ago)
Author:
uli
Message:

Add applicants exporter (first draft).

File:
1 edited

Legend:

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

    r7906 r7914  
    1919"""
    2020import grok
     21from zope.catalog.interfaces import ICatalog
     22from zope.component import queryUtility
     23from waeup.kofa.applicants.interfaces import IApplicantBaseData
    2124from waeup.kofa.interfaces import ICSVExporter
    2225from waeup.kofa.interfaces import MessageFactory as _
     
    6265        containers = site.get('applicants', {})
    6366        return self.export(containers.values(), filepath)
     67
     68class ApplicantsExporter(grok.GlobalUtility, ExporterBase):
     69    """Exporter for Applicants.
     70    """
     71    grok.implements(ICSVExporter)
     72    grok.name('applicants')
     73
     74    #: Fieldnames considered by this exporter
     75    fields = tuple(IApplicantBaseData.names())
     76
     77    #: The title under which this exporter will be displayed
     78    title = _(u'Applicants')
     79
     80    def NOmangle_value(self, value, name, context=None):
     81        if name == 'provider' and isinstance(value, tuple):
     82            value = value[0]
     83        return super(
     84            ApplicantsContainerExporter, self).mangle_value(
     85            value, name, context=context)
     86
     87    def export(self, applicants, filepath=None):
     88        """Export `applicants`, an iterable, as CSV file.
     89
     90        If `filepath` is ``None``, a raw string with CSV data is returned.
     91        """
     92        writer, outfile = self.get_csv_writer(filepath)
     93        for applicant in applicants:
     94            self.write_item(applicant, writer)
     95        return self.close_outfile(filepath, outfile)
     96
     97    def export_all(self, site, filepath=None):
     98        """Export applicants into filepath as CSV data.
     99
     100        If `filepath` is ``None``, a raw string with CSV data is returned.
     101        """
     102        catalog = queryUtility(
     103            ICatalog, context=site, name='applicants_catalog', default=None)
     104        if catalog is None:
     105            return self.export([], filepath)
     106        applicants = catalog.searchResults(
     107            reg_number=(None, None))
     108        return self.export(applicants, filepath)
Note: See TracChangeset for help on using the changeset viewer.