source: main/waeup.kofa/branches/0.2/src/waeup/kofa/applicants/export.py @ 11348

Last change on this file since 11348 was 10654, checked in by Henrik Bettermann, 11 years ago

The the container keyword should be the container ode and not the container object itself.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1## $Id: export.py 10654 2013-09-26 08:17:05Z henrik $
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        elif name == 'phone' and value is not None:
86            # Append hash '#' to phone numbers to circumvent
87            # unwanted excel automatic
88            value = str('%s#' % value)
89        return super(
90            ApplicantsExporter, self).mangle_value(
91            value, name, context=context)
92
93    def export(self, applicants, filepath=None):
94        """Export `applicants`, an iterable, as CSV file.
95
96        If `filepath` is ``None``, a raw string with CSV data is returned.
97        """
98        writer, outfile = self.get_csv_writer(filepath)
99        for applicant in applicants:
100            self.write_item(applicant, writer)
101        return self.close_outfile(filepath, outfile)
102
103    def export_all(self, site, filepath=None):
104        """Export applicants into filepath as CSV data.
105
106        If `filepath` is ``None``, a raw string with CSV data is returned.
107        """
108        catalog = queryUtility(
109            ICatalog, context=site, name='applicants_catalog', default=None)
110        if catalog is None:
111            return self.export([], filepath)
112        applicants = catalog.searchResults(
113            # reg_num might not be set and then would not be found.
114            # We therefore search for applicant_id.
115            applicant_id=(None, None))
116        return self.export(applicants, filepath)
117
118    def export_filtered(self, site, filepath=None, **kw):
119        """Export applicants in container.
120
121        If `filepath` is ``None``, a raw string with CSV data should
122        be returned.
123        """
124        container = grok.getSite()['applicants'][kw['container']]
125        return self.export(container.values(), filepath=filepath)
Note: See TracBrowser for help on using the repository browser.