[7865] | 1 | ## $Id: export.py 8381 2012-05-07 10:37:27Z 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 | """ |
---|
| 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 |
---|
[8311] | 35 | fields = ('code', 'title', 'prefix', 'year', |
---|
[8008] | 36 | '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 | return super( |
---|
| 44 | ApplicantsContainerExporter, self).mangle_value( |
---|
| 45 | value, name, context=context) |
---|
| 46 | |
---|
[7865] | 47 | def export(self, containers, filepath=None): |
---|
| 48 | """Export `containers`, an iterable, as CSV file. |
---|
| 49 | |
---|
| 50 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 51 | """ |
---|
| 52 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 53 | for container in containers: |
---|
| 54 | self.write_item(container, writer) |
---|
| 55 | return self.close_outfile(filepath, outfile) |
---|
| 56 | |
---|
| 57 | def export_all(self, site, filepath=None): |
---|
| 58 | """Export applicantscontainer into filepath as CSV data. |
---|
| 59 | |
---|
| 60 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 61 | """ |
---|
| 62 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 63 | containers = site.get('applicants', {}) |
---|
| 64 | return self.export(containers.values(), filepath) |
---|
[7914] | 65 | |
---|
| 66 | class ApplicantsExporter(grok.GlobalUtility, ExporterBase): |
---|
| 67 | """Exporter for Applicants. |
---|
| 68 | """ |
---|
| 69 | grok.implements(ICSVExporter) |
---|
| 70 | grok.name('applicants') |
---|
| 71 | |
---|
| 72 | #: Fieldnames considered by this exporter |
---|
[8377] | 73 | fields = tuple(sorted(IApplicantBaseData.names())) + ('container_code',) |
---|
[7914] | 74 | |
---|
| 75 | #: The title under which this exporter will be displayed |
---|
| 76 | title = _(u'Applicants') |
---|
| 77 | |
---|
[7924] | 78 | def mangle_value(self, value, name, context=None): |
---|
| 79 | if name in ( |
---|
| 80 | 'course1', 'course2', 'course_admitted') and value is not None: |
---|
| 81 | value = value.code |
---|
[8052] | 82 | #elif name == 'school_grades': |
---|
| 83 | # value = [eval(entry.to_string()) for entry in value] |
---|
| 84 | elif name == 'history': |
---|
| 85 | value = value.messages |
---|
[7914] | 86 | return super( |
---|
[7924] | 87 | ApplicantsExporter, self).mangle_value( |
---|
[7914] | 88 | value, name, context=context) |
---|
| 89 | |
---|
| 90 | def export(self, applicants, filepath=None): |
---|
| 91 | """Export `applicants`, an iterable, as CSV file. |
---|
| 92 | |
---|
| 93 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 94 | """ |
---|
| 95 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 96 | for applicant in applicants: |
---|
| 97 | self.write_item(applicant, writer) |
---|
| 98 | return self.close_outfile(filepath, outfile) |
---|
| 99 | |
---|
| 100 | def export_all(self, site, filepath=None): |
---|
| 101 | """Export applicants into filepath as CSV data. |
---|
| 102 | |
---|
| 103 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 104 | """ |
---|
| 105 | catalog = queryUtility( |
---|
| 106 | ICatalog, context=site, name='applicants_catalog', default=None) |
---|
| 107 | if catalog is None: |
---|
| 108 | return self.export([], filepath) |
---|
| 109 | applicants = catalog.searchResults( |
---|
[7924] | 110 | # reg_num might not be set and then would not be found. |
---|
| 111 | # We therefore search for applicant_id. |
---|
| 112 | applicant_id=(None, None)) |
---|
[7914] | 113 | return self.export(applicants, filepath) |
---|