[7865] | 1 | ## $Id: export.py 13216 2015-08-24 05:56:39Z 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 | """ |
---|
| 20 | import grok |
---|
[7914] | 21 | from zope.catalog.interfaces import ICatalog |
---|
| 22 | from zope.component import queryUtility |
---|
[8724] | 23 | from waeup.kofa.applicants.interfaces import ( |
---|
| 24 | IApplicantBaseData, IApplicantsContainer) |
---|
[7865] | 25 | from waeup.kofa.interfaces import ICSVExporter |
---|
[7906] | 26 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7865] | 27 | from waeup.kofa.utils.batching import ExporterBase |
---|
[8724] | 28 | from waeup.kofa.utils.helpers import iface_names |
---|
[7865] | 29 | |
---|
| 30 | class ApplicantsContainerExporter(grok.GlobalUtility, ExporterBase): |
---|
[12859] | 31 | """The Applicants Container Exporter exports container data. It does not |
---|
| 32 | export applicants (application records) inside the container. |
---|
[7865] | 33 | """ |
---|
| 34 | grok.implements(ICSVExporter) |
---|
| 35 | grok.name('applicantscontainers') |
---|
| 36 | |
---|
[8724] | 37 | fields = tuple(sorted(iface_names(IApplicantsContainer))) |
---|
[12180] | 38 | title = _(u'Applicants Containers') |
---|
[7906] | 39 | |
---|
| 40 | def mangle_value(self, value, name, context=None): |
---|
| 41 | return super( |
---|
| 42 | ApplicantsContainerExporter, self).mangle_value( |
---|
| 43 | value, name, context=context) |
---|
| 44 | |
---|
[7865] | 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) |
---|
[7914] | 63 | |
---|
[12859] | 64 | |
---|
[12079] | 65 | class ApplicantExporter(grok.GlobalUtility, ExporterBase): |
---|
[12861] | 66 | """The Applicant Exporter exports application records (= applicants) |
---|
[12862] | 67 | stored in the database. In contrast to the exporters in the academic |
---|
[12859] | 68 | section this exporter does not iterate over the items of containers |
---|
[12861] | 69 | but searches the :class:`ApplicantsCatalog` instead. |
---|
| 70 | |
---|
| 71 | The exporter exports all applicants if started in the Data Center |
---|
| 72 | which means in the context of the `DataCenter` object. The exporter can also |
---|
| 73 | be started 'locally' which means in the context of an `ApplicantsContainer` |
---|
| 74 | container. Then the :meth:`export_filtered()` instead of the |
---|
| 75 | :meth:`export_all()` method is applied which searches for applicants |
---|
| 76 | in the respective container. |
---|
[7914] | 77 | """ |
---|
| 78 | grok.implements(ICSVExporter) |
---|
| 79 | grok.name('applicants') |
---|
| 80 | |
---|
[13080] | 81 | fields = tuple(sorted(iface_names(IApplicantBaseData))) + ( |
---|
| 82 | 'password', 'state', 'history', 'container_code', 'application_number', |
---|
| 83 | 'display_fullname', 'application_date') |
---|
[7914] | 84 | title = _(u'Applicants') |
---|
| 85 | |
---|
[7924] | 86 | def mangle_value(self, value, name, context=None): |
---|
[12865] | 87 | """The mangler determines the codes of the atributes `course1`, |
---|
[12859] | 88 | `course2` and `course_admitted`. It furthermore prepares the |
---|
| 89 | history messages and adds a hash symbol at the end of the phone number |
---|
| 90 | to avoid annoying automatic number transformation by Excel or Calc. |
---|
| 91 | """ |
---|
[7924] | 92 | if name in ( |
---|
| 93 | 'course1', 'course2', 'course_admitted') and value is not None: |
---|
| 94 | value = value.code |
---|
[8052] | 95 | #elif name == 'school_grades': |
---|
| 96 | # value = [eval(entry.to_string()) for entry in value] |
---|
| 97 | elif name == 'history': |
---|
| 98 | value = value.messages |
---|
[9117] | 99 | elif name == 'phone' and value is not None: |
---|
| 100 | # Append hash '#' to phone numbers to circumvent |
---|
| 101 | # unwanted excel automatic |
---|
| 102 | value = str('%s#' % value) |
---|
[13216] | 103 | elif name == 'container_code': |
---|
| 104 | value = value.strip('+') |
---|
[7914] | 105 | return super( |
---|
[12079] | 106 | ApplicantExporter, self).mangle_value( |
---|
[7914] | 107 | value, name, context=context) |
---|
| 108 | |
---|
| 109 | def export(self, applicants, filepath=None): |
---|
| 110 | """Export `applicants`, an iterable, as CSV file. |
---|
| 111 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 112 | """ |
---|
| 113 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 114 | for applicant in applicants: |
---|
| 115 | self.write_item(applicant, writer) |
---|
| 116 | return self.close_outfile(filepath, outfile) |
---|
| 117 | |
---|
| 118 | def export_all(self, site, filepath=None): |
---|
[12861] | 119 | """Export all applicants into filepath as CSV data. |
---|
[7914] | 120 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
[13216] | 121 | Only used records are being exported. |
---|
[7914] | 122 | """ |
---|
| 123 | catalog = queryUtility( |
---|
| 124 | ICatalog, context=site, name='applicants_catalog', default=None) |
---|
| 125 | if catalog is None: |
---|
| 126 | return self.export([], filepath) |
---|
| 127 | applicants = catalog.searchResults( |
---|
[7924] | 128 | # reg_num might not be set and then would not be found. |
---|
| 129 | # We therefore search for applicant_id. |
---|
| 130 | applicant_id=(None, None)) |
---|
[13216] | 131 | used = [value for value in applicants if value.container_code.endswith('+')] |
---|
| 132 | return self.export(used, filepath=filepath) |
---|
[10653] | 133 | |
---|
| 134 | def export_filtered(self, site, filepath=None, **kw): |
---|
[12861] | 135 | """Export filtered applicants in container denoted by keywords (`kw`). |
---|
[10653] | 136 | If `filepath` is ``None``, a raw string with CSV data should |
---|
[13216] | 137 | be returned. Only used records are being exported. |
---|
[10653] | 138 | """ |
---|
[10654] | 139 | container = grok.getSite()['applicants'][kw['container']] |
---|
[13216] | 140 | container_values = container.values() |
---|
| 141 | used = [value for value in container_values if value.container_code.endswith('+')] |
---|
| 142 | return self.export(used, filepath=filepath) |
---|