## $Id$
##
## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""Exporters for applicant-related stuff.
"""
import grok
from waeup.kofa.interfaces import ICSVExporter
from waeup.kofa.utils.batching import ExporterBase

class ApplicantsContainerExporter(grok.GlobalUtility, ExporterBase):
    """Exporter for ApplicantsContainers.
    """
    grok.implements(ICSVExporter)
    grok.name('applicantscontainers')

    #: Fieldnames considered by this exporter
    fields = ('code', 'title', 'prefix', 'entry_level', 'year',
              'provider', 'application_category', 'description',
              'description_dict', 'startdate', 'enddate', 'strict_deadline')

    def export(self, containers, filepath=None):
        """Export `containers`, an iterable, as CSV file.

        If `filepath` is ``None``, a raw string with CSV data is returned.
        """
        writer, outfile = self.get_csv_writer(filepath)
        for container in containers:
            self.write_item(container, writer)
        return self.close_outfile(filepath, outfile)

    def export_all(self, site, filepath=None):
        """Export applicantscontainer into filepath as CSV data.

        If `filepath` is ``None``, a raw string with CSV data is returned.
        """
        writer, outfile = self.get_csv_writer(filepath)
        containers = site.get('applicants', {})
        return self.export(containers.values(), filepath)
