source: main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/export.py @ 16157

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

Add more exporter documentation.

  • Property svn:keywords set to Id
File size: 3.7 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 access codes and access code batches.
19"""
20import grok
21from zope.securitypolicy.interfaces import IPrincipalRoleManager
22from waeup.kofa.interfaces import ICSVExporter
23from waeup.kofa.interfaces import MessageFactory as _
24from waeup.kofa.utils.batching import ExporterBase
25from waeup.kofa.utils.helpers import iface_names
26from waeup.kofa.accesscodes.interfaces import IAccessCode, IAccessCodeBatch
27
28class AccessCodeBatchExporter(grok.GlobalUtility, ExporterBase):
29    """The Access Code Batch Exporter exports container data. It does not
30    export access codes inside the container.
31    """
32    grok.implements(ICSVExporter)
33    grok.name('accesscodebatches')
34
35    fields = tuple(sorted(iface_names(IAccessCodeBatch))) + ('batch_id',)
36    title = _(u'Access Code Batches')
37
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
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
56
57class AccessCodeExporter(grok.GlobalUtility, ExporterBase):
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.
61    """
62    grok.implements(ICSVExporter)
63    grok.name('accesscodes')
64
65    fields = tuple(sorted(iface_names(IAccessCode)))
66    title = _(u'Access Codes')
67
68    def mangle_value(self, value, name, context=None):
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        """
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
Note: See TracBrowser for help on using the repository browser.