source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/export.py @ 12779

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

Add payment_items to IPayment.

Export attributes too.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1## $Id: export.py 12779 2015-03-17 18:41:43Z 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 payments.
19"""
20import grok
21from zope.securitypolicy.interfaces import IPrincipalRoleMap
22from waeup.ikoba.interfaces import ICSVExporter
23from waeup.ikoba.interfaces import MessageFactory as _
24from waeup.ikoba.utils.batching import ExporterBase
25from waeup.ikoba.utils.helpers import iface_names
26from waeup.ikoba.payments.interfaces import IPayment
27
28
29class PaymentExporterBase(grok.GlobalUtility, ExporterBase):
30    """Exporter for payments.
31
32    This is a baseclass.
33    """
34    grok.baseclass()
35    iface = None
36    title = None
37
38    @property
39    def fields(self):
40        return tuple(sorted(iface_names(self.iface, exclude_attribs=False)))
41
42    def mangle_value(self, value, name, context=None):
43        """Hook for mangling values in derived classes
44        """
45        if name == 'payment_items' and value is not None:
46            value = [eval(entry.to_string()) for entry in value]
47        return super(PaymentExporterBase, self).mangle_value(
48            value, name, context)
49
50    def export(self, payments, filepath=None):
51        """Export `payments`, an iterable, as CSV file.
52
53        If `filepath` is ``None``, a raw string with CSV data is returned.
54        """
55        writer, outfile = self.get_csv_writer(filepath)
56        for payment in payments:
57            self.write_item(payment, writer)
58        return self.close_outfile(filepath, outfile)
59
60    def export_all(self, site, filepath=None):
61        """Export payments in paymentscontainer into filepath as CSV data.
62
63        If `filepath` is ``None``, a raw string with CSV data is returned.
64        """
65        payments = site.get('payments', {})
66        return self.export(payments.values(), filepath)
67
68
69class PaymentExporter(PaymentExporterBase):
70    """Exporter for payments.
71
72    """
73    grok.implements(ICSVExporter)
74    grok.name('payments')
75    iface = IPayment
76    title = _(u'Payments')
Note: See TracBrowser for help on using the repository browser.