[7865] | 1 | ## $Id$ |
---|
| 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 | """ |
---|
| 20 | import grok |
---|
[7914] | 21 | from zope.catalog.interfaces import ICatalog |
---|
| 22 | from zope.component import queryUtility |
---|
| 23 | from waeup.kofa.applicants.interfaces import IApplicantBaseData |
---|
[7865] | 24 | from waeup.kofa.interfaces import ICSVExporter |
---|
[7906] | 25 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7865] | 26 | from waeup.kofa.utils.batching import ExporterBase |
---|
| 27 | |
---|
| 28 | class ApplicantsContainerExporter(grok.GlobalUtility, ExporterBase): |
---|
| 29 | """Exporter for ApplicantsContainers. |
---|
| 30 | """ |
---|
| 31 | grok.implements(ICSVExporter) |
---|
| 32 | grok.name('applicantscontainers') |
---|
| 33 | |
---|
| 34 | #: Fieldnames considered by this exporter |
---|
| 35 | fields = ('code', 'title', 'prefix', 'entry_level', 'year', |
---|
| 36 | 'provider', 'application_category', 'description', |
---|
[7903] | 37 | 'startdate', 'enddate', 'strict_deadline') |
---|
[7865] | 38 | |
---|
[7906] | 39 | #: The title under which this exporter will be displayed |
---|
| 40 | title = _(u'Basic Applicants Containers') |
---|
| 41 | |
---|
| 42 | def mangle_value(self, value, name, context=None): |
---|
| 43 | if name == 'provider' and isinstance(value, tuple): |
---|
| 44 | value = value[0] |
---|
| 45 | return super( |
---|
| 46 | ApplicantsContainerExporter, self).mangle_value( |
---|
| 47 | value, name, context=context) |
---|
| 48 | |
---|
[7865] | 49 | def export(self, containers, filepath=None): |
---|
| 50 | """Export `containers`, an iterable, as CSV file. |
---|
| 51 | |
---|
| 52 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 53 | """ |
---|
| 54 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 55 | for container in containers: |
---|
| 56 | self.write_item(container, writer) |
---|
| 57 | return self.close_outfile(filepath, outfile) |
---|
| 58 | |
---|
| 59 | def export_all(self, site, filepath=None): |
---|
| 60 | """Export applicantscontainer into filepath as CSV data. |
---|
| 61 | |
---|
| 62 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 63 | """ |
---|
| 64 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 65 | containers = site.get('applicants', {}) |
---|
| 66 | return self.export(containers.values(), filepath) |
---|
[7914] | 67 | |
---|
| 68 | class ApplicantsExporter(grok.GlobalUtility, ExporterBase): |
---|
| 69 | """Exporter for Applicants. |
---|
| 70 | """ |
---|
| 71 | grok.implements(ICSVExporter) |
---|
| 72 | grok.name('applicants') |
---|
| 73 | |
---|
| 74 | #: Fieldnames considered by this exporter |
---|
| 75 | fields = tuple(IApplicantBaseData.names()) |
---|
| 76 | |
---|
| 77 | #: The title under which this exporter will be displayed |
---|
| 78 | title = _(u'Applicants') |
---|
| 79 | |
---|
| 80 | def NOmangle_value(self, value, name, context=None): |
---|
| 81 | if name == 'provider' and isinstance(value, tuple): |
---|
| 82 | value = value[0] |
---|
| 83 | return super( |
---|
| 84 | ApplicantsContainerExporter, self).mangle_value( |
---|
| 85 | value, name, context=context) |
---|
| 86 | |
---|
| 87 | def export(self, applicants, filepath=None): |
---|
| 88 | """Export `applicants`, an iterable, as CSV file. |
---|
| 89 | |
---|
| 90 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 91 | """ |
---|
| 92 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 93 | for applicant in applicants: |
---|
| 94 | self.write_item(applicant, writer) |
---|
| 95 | return self.close_outfile(filepath, outfile) |
---|
| 96 | |
---|
| 97 | def export_all(self, site, filepath=None): |
---|
| 98 | """Export applicants into filepath as CSV data. |
---|
| 99 | |
---|
| 100 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 101 | """ |
---|
| 102 | catalog = queryUtility( |
---|
| 103 | ICatalog, context=site, name='applicants_catalog', default=None) |
---|
| 104 | if catalog is None: |
---|
| 105 | return self.export([], filepath) |
---|
| 106 | applicants = catalog.searchResults( |
---|
| 107 | reg_number=(None, None)) |
---|
| 108 | return self.export(applicants, filepath) |
---|