## $Id: export.py 8057 2012-04-06 21:56:22Z henrik $
##
## 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 zope.catalog.interfaces import ICatalog
from zope.component import queryUtility
from waeup.kofa.applicants.interfaces import IApplicantBaseData
from waeup.kofa.interfaces import ICSVExporter
from waeup.kofa.interfaces import MessageFactory as _
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',
              'application_category', 'description',
              'startdate', 'enddate', 'strict_deadline')

    #: The title under which this exporter will be displayed
    title = _(u'Basic Applicants Containers')

    def mangle_value(self, value, name, context=None):
        return super(
            ApplicantsContainerExporter, self).mangle_value(
            value, name, context=context)

    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)

class ApplicantsExporter(grok.GlobalUtility, ExporterBase):
    """Exporter for Applicants.
    """
    grok.implements(ICSVExporter)
    grok.name('applicants')

    #: Fieldnames considered by this exporter
    fields = tuple(sorted(IApplicantBaseData.names()))

    #: The title under which this exporter will be displayed
    title = _(u'Applicants')

    def mangle_value(self, value, name, context=None):
        if name in (
            'course1', 'course2', 'course_admitted') and value is not None:
            value = value.code
        #elif name == 'school_grades':
        #    value = [eval(entry.to_string()) for entry in value]
        elif name == 'history':
            value = value.messages
        return super(
            ApplicantsExporter, self).mangle_value(
            value, name, context=context)

    def export(self, applicants, filepath=None):
        """Export `applicants`, 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 applicant in applicants:
            self.write_item(applicant, writer)
        return self.close_outfile(filepath, outfile)

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

        If `filepath` is ``None``, a raw string with CSV data is returned.
        """
        catalog = queryUtility(
            ICatalog, context=site, name='applicants_catalog', default=None)
        if catalog is None:
            return self.export([], filepath)
        applicants = catalog.searchResults(
            # reg_num might not be set and then would not be found.
            # We therefore search for applicant_id.
            applicant_id=(None, None))
        return self.export(applicants, filepath)
