1 | ## $Id: export.py 12315 2014-12-24 16:00:58Z 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 products. |
---|
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.products.interfaces import IProduct |
---|
27 | |
---|
28 | |
---|
29 | class ProductExporter(grok.GlobalUtility, ExporterBase): |
---|
30 | """Exporter for products. |
---|
31 | """ |
---|
32 | grok.implements(ICSVExporter) |
---|
33 | grok.name('products') |
---|
34 | |
---|
35 | #: Fieldnames considered by this exporter |
---|
36 | fields = tuple(sorted(iface_names(IProduct))) + ( |
---|
37 | 'users_with_local_roles',) |
---|
38 | |
---|
39 | #: The title under which this exporter will be displayed |
---|
40 | title = _(u'Products') |
---|
41 | |
---|
42 | def mangle_value(self, value, name, context=None): |
---|
43 | """Hook for mangling values in derived classes |
---|
44 | """ |
---|
45 | if name == 'users_with_local_roles': |
---|
46 | value = [] |
---|
47 | role_map = IPrincipalRoleMap(context) |
---|
48 | for local_role, user_name, setting in role_map.getPrincipalsAndRoles(): |
---|
49 | value.append({'user_name':user_name,'local_role':local_role}) |
---|
50 | if name == 'options' and value is not None: |
---|
51 | value = [eval(entry.to_string()) for entry in value] |
---|
52 | return super(ProductExporter, self).mangle_value( |
---|
53 | value, name, context) |
---|
54 | |
---|
55 | def export(self, products, filepath=None): |
---|
56 | """Export `products`, an iterable, as CSV file. |
---|
57 | |
---|
58 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
59 | """ |
---|
60 | writer, outfile = self.get_csv_writer(filepath) |
---|
61 | for product in products: |
---|
62 | self.write_item(product, writer) |
---|
63 | return self.close_outfile(filepath, outfile) |
---|
64 | |
---|
65 | def export_all(self, site, filepath=None): |
---|
66 | """Export products in productscontainer into filepath as CSV data. |
---|
67 | |
---|
68 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
69 | """ |
---|
70 | products = site.get('products', {}) |
---|
71 | return self.export(products.values(), filepath) |
---|