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

Last change on this file since 17935 was 12820, checked in by uli, 10 years ago

pyflakes.

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