[9261] | 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 access codes and access code batches. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
| 21 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
| 22 | from waeup.kofa.interfaces import ICSVExporter |
---|
| 23 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 24 | from waeup.kofa.utils.batching import ExporterBase |
---|
| 25 | from waeup.kofa.utils.helpers import iface_names |
---|
| 26 | from waeup.kofa.accesscodes.interfaces import IAccessCode, IAccessCodeBatch |
---|
| 27 | |
---|
| 28 | class AccessCodeBatchExporter(grok.GlobalUtility, ExporterBase): |
---|
[12859] | 29 | """The Access Code Batch Exporter exports container data. It does not |
---|
| 30 | export access codes inside the container. |
---|
[9261] | 31 | """ |
---|
| 32 | grok.implements(ICSVExporter) |
---|
| 33 | grok.name('accesscodebatches') |
---|
| 34 | |
---|
[9262] | 35 | fields = tuple(sorted(iface_names(IAccessCodeBatch))) + ('batch_id',) |
---|
[12180] | 36 | title = _(u'Access Code Batches') |
---|
[9261] | 37 | |
---|
[9262] | 38 | def mangle_value(self, value, name, context=None): |
---|
| 39 | if name == 'batch_id' and context is not None: |
---|
| 40 | value = u'%s-%s' % (getattr(context, 'prefix', None), |
---|
| 41 | getattr(context, 'num', None)) |
---|
| 42 | return super( |
---|
| 43 | AccessCodeBatchExporter, self).mangle_value( |
---|
| 44 | value, name, context=context) |
---|
| 45 | |
---|
[9261] | 46 | def export_all(self, site, filepath=None): |
---|
| 47 | """Export accesscode batches into filepath as CSV data. |
---|
| 48 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 49 | """ |
---|
| 50 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 51 | accesscodebatches = site.get('accesscodes', {}) |
---|
| 52 | for batch in accesscodebatches.values(): |
---|
| 53 | self.write_item(batch, writer) |
---|
| 54 | return self.close_outfile(filepath, outfile) |
---|
| 55 | |
---|
[12859] | 56 | |
---|
[9261] | 57 | class AccessCodeExporter(grok.GlobalUtility, ExporterBase): |
---|
[12859] | 58 | """The Access Code Exporter exports all access codes stored in the |
---|
| 59 | access code batch containers. The exporter iterates over all access code |
---|
| 60 | batches and over all access codes inside each batch container. |
---|
[9261] | 61 | """ |
---|
| 62 | grok.implements(ICSVExporter) |
---|
| 63 | grok.name('accesscodes') |
---|
| 64 | |
---|
| 65 | fields = tuple(sorted(iface_names(IAccessCode))) |
---|
[12180] | 66 | title = _(u'Access Codes') |
---|
[9261] | 67 | |
---|
| 68 | def mangle_value(self, value, name, context=None): |
---|
[12859] | 69 | """The mangler adds a hash symbol at the end of ``random_num`` |
---|
| 70 | to avoid annoying automatic number transformation by Excel or Calc. |
---|
| 71 | """ |
---|
[9261] | 72 | if name == 'random_num' and value is not None: |
---|
| 73 | # Append hash '#' to numbers to circumvent |
---|
| 74 | # unwanted excel automatic |
---|
| 75 | value = str('%s#' % value) |
---|
| 76 | return super( |
---|
| 77 | AccessCodeExporter, self).mangle_value( |
---|
| 78 | value, name, context=context) |
---|
| 79 | |
---|
| 80 | def export_all(self, site, filepath=None): |
---|
| 81 | """Export accesscodes into filepath as CSV data. |
---|
| 82 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
| 83 | """ |
---|
| 84 | writer, outfile = self.get_csv_writer(filepath) |
---|
| 85 | accesscodebatches = site.get('accesscodes', {}) |
---|
| 86 | for batch in accesscodebatches.values(): |
---|
| 87 | for ac in batch.values(): |
---|
| 88 | self.write_item(ac, writer) |
---|
| 89 | return self.close_outfile(filepath, outfile) |
---|
| 90 | |
---|