1 | ## $Id: export.py 12777 2015-03-16 16:33:56Z 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 | """ |
---|
20 | import grok |
---|
21 | from zope.securitypolicy.interfaces import IPrincipalRoleMap |
---|
22 | from waeup.ikoba.interfaces import ICSVExporter |
---|
23 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
24 | from waeup.ikoba.utils.batching import ExporterBase |
---|
25 | from waeup.ikoba.utils.helpers import iface_names |
---|
26 | from waeup.ikoba.payments.interfaces import IPayment |
---|
27 | |
---|
28 | |
---|
29 | class 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))) |
---|
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 | |
---|
69 | class PaymentExporter(PaymentExporterBase): |
---|
70 | """Exporter for payments. |
---|
71 | |
---|
72 | """ |
---|
73 | grok.implements(ICSVExporter) |
---|
74 | grok.name('payments') |
---|
75 | iface = IPayment |
---|
76 | title = _(u'Payments') |
---|