source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/export.py @ 12859

Last change on this file since 12859 was 12859, checked in by Henrik Bettermann, 10 years ago

Add more exporter documentation.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1## $Id: export.py 12859 2015-04-16 18:49:08Z 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    """The Applicants Container Exporter exports container data. It does not
32    export applicants (application records) inside the container.
33    """
34    grok.implements(ICSVExporter)
35    grok.name('applicantscontainers')
36
37    fields = tuple(sorted(iface_names(IApplicantsContainer)))
38    title = _(u'Applicants Containers')
39
40    def mangle_value(self, value, name, context=None):
41        return super(
42            ApplicantsContainerExporter, self).mangle_value(
43            value, name, context=context)
44
45    def export(self, containers, filepath=None):
46        """Export `containers`, an iterable, as CSV file.
47
48        If `filepath` is ``None``, a raw string with CSV data is returned.
49        """
50        writer, outfile = self.get_csv_writer(filepath)
51        for container in containers:
52            self.write_item(container, writer)
53        return self.close_outfile(filepath, outfile)
54
55    def export_all(self, site, filepath=None):
56        """Export applicantscontainer into filepath as CSV data.
57
58        If `filepath` is ``None``, a raw string with CSV data is returned.
59        """
60        writer, outfile = self.get_csv_writer(filepath)
61        containers = site.get('applicants', {})
62        return self.export(containers.values(), filepath)
63
64
65class ApplicantExporter(grok.GlobalUtility, ExporterBase):
66    """The Applicant Exporter exports all application records (= applicants)
67    stored in the database. In contrast to the exporters in the academics
68    section this exporter does not iterate over the items of containers
69    but searches the Applicants Catalog instead.
70    """
71    grok.implements(ICSVExporter)
72    grok.name('applicants')
73
74    fields = tuple(sorted(IApplicantBaseData.names())) + ('container_code',)
75    title = _(u'Applicants')
76
77    def mangle_value(self, value, name, context=None):
78        """The mangler determines the the codes of the atributes `course1`,
79        `course2` and `course_admitted`. It furthermore prepares the
80        history messages and adds a hash symbol at the end of the phone number
81        to avoid annoying automatic number transformation by Excel or Calc.
82        """
83        if name in (
84            'course1', 'course2', 'course_admitted') and value is not None:
85            value = value.code
86        #elif name == 'school_grades':
87        #    value = [eval(entry.to_string()) for entry in value]
88        elif name == 'history':
89            value = value.messages
90        elif name == 'phone' and value is not None:
91            # Append hash '#' to phone numbers to circumvent
92            # unwanted excel automatic
93            value = str('%s#' % value)
94        return super(
95            ApplicantExporter, self).mangle_value(
96            value, name, context=context)
97
98    def export(self, applicants, filepath=None):
99        """Export `applicants`, an iterable, as CSV file.
100
101        If `filepath` is ``None``, a raw string with CSV data is returned.
102        """
103        writer, outfile = self.get_csv_writer(filepath)
104        for applicant in applicants:
105            self.write_item(applicant, writer)
106        return self.close_outfile(filepath, outfile)
107
108    def export_all(self, site, filepath=None):
109        """Export applicants into filepath as CSV data.
110
111        If `filepath` is ``None``, a raw string with CSV data is returned.
112        """
113        catalog = queryUtility(
114            ICatalog, context=site, name='applicants_catalog', default=None)
115        if catalog is None:
116            return self.export([], filepath)
117        applicants = catalog.searchResults(
118            # reg_num might not be set and then would not be found.
119            # We therefore search for applicant_id.
120            applicant_id=(None, None))
121        return self.export(applicants, filepath)
122
123    def export_filtered(self, site, filepath=None, **kw):
124        """Export applicants in container.
125
126        If `filepath` is ``None``, a raw string with CSV data should
127        be returned.
128        """
129        container = grok.getSite()['applicants'][kw['container']]
130        return self.export(container.values(), filepath=filepath)
Note: See TracBrowser for help on using the repository browser.