source: main/waeup.kofa/branches/uli-zc-async/src/waeup/kofa/applicants/export.py @ 9819

Last change on this file since 9819 was 9211, checked in by uli, 12 years ago

Rollback r9209. Looks like multiple merges from trunk confuse svn when merging back into trunk.

  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1## $Id: export.py 9211 2012-09-21 08:19:35Z uli $
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Exporters for applicant-related stuff.
19"""
20import grok
21from zope.catalog.interfaces import ICatalog
22from zope.component import queryUtility
23from waeup.kofa.applicants.interfaces import (
24    IApplicantBaseData, IApplicantsContainer)
25from waeup.kofa.interfaces import ICSVExporter
26from waeup.kofa.interfaces import MessageFactory as _
27from waeup.kofa.utils.batching import ExporterBase
28from waeup.kofa.utils.helpers import iface_names
29
30class ApplicantsContainerExporter(grok.GlobalUtility, ExporterBase):
31    """Exporter for ApplicantsContainers.
32    """
33    grok.implements(ICSVExporter)
34    grok.name('applicantscontainers')
35
36    #: Fieldnames considered by this exporter
37    fields = tuple(sorted(iface_names(IApplicantsContainer)))
38    #: The title under which this exporter will be displayed
39    title = _(u'Basic Applicants Containers')
40
41    def mangle_value(self, value, name, context=None):
42        return super(
43            ApplicantsContainerExporter, self).mangle_value(
44            value, name, context=context)
45
46    def export(self, containers, filepath=None):
47        """Export `containers`, an iterable, as CSV file.
48
49        If `filepath` is ``None``, a raw string with CSV data is returned.
50        """
51        writer, outfile = self.get_csv_writer(filepath)
52        for container in containers:
53            self.write_item(container, writer)
54        return self.close_outfile(filepath, outfile)
55
56    def export_all(self, site, filepath=None):
57        """Export applicantscontainer into filepath as CSV data.
58
59        If `filepath` is ``None``, a raw string with CSV data is returned.
60        """
61        writer, outfile = self.get_csv_writer(filepath)
62        containers = site.get('applicants', {})
63        return self.export(containers.values(), filepath)
64
65class ApplicantsExporter(grok.GlobalUtility, ExporterBase):
66    """Exporter for Applicants.
67    """
68    grok.implements(ICSVExporter)
69    grok.name('applicants')
70
71    #: Fieldnames considered by this exporter
72    fields = tuple(sorted(IApplicantBaseData.names())) + ('container_code',)
73
74    #: The title under which this exporter will be displayed
75    title = _(u'Applicants')
76
77    def mangle_value(self, value, name, context=None):
78        if name in (
79            'course1', 'course2', 'course_admitted') and value is not None:
80            value = value.code
81        #elif name == 'school_grades':
82        #    value = [eval(entry.to_string()) for entry in value]
83        elif name == 'history':
84            value = value.messages
85        return super(
86            ApplicantsExporter, self).mangle_value(
87            value, name, context=context)
88
89    def export(self, applicants, filepath=None):
90        """Export `applicants`, an iterable, as CSV file.
91
92        If `filepath` is ``None``, a raw string with CSV data is returned.
93        """
94        writer, outfile = self.get_csv_writer(filepath)
95        for applicant in applicants:
96            self.write_item(applicant, writer)
97        return self.close_outfile(filepath, outfile)
98
99    def export_all(self, site, filepath=None):
100        """Export applicants into filepath as CSV data.
101
102        If `filepath` is ``None``, a raw string with CSV data is returned.
103        """
104        catalog = queryUtility(
105            ICatalog, context=site, name='applicants_catalog', default=None)
106        if catalog is None:
107            return self.export([], filepath)
108        applicants = catalog.searchResults(
109            # reg_num might not be set and then would not be found.
110            # We therefore search for applicant_id.
111            applicant_id=(None, None))
112        return self.export(applicants, filepath)
Note: See TracBrowser for help on using the repository browser.