1 | ## $Id: export.py 11757 2014-07-10 12:20:30Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 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 customer related stuff. |
---|
19 | """ |
---|
20 | import os |
---|
21 | import grok |
---|
22 | from datetime import datetime |
---|
23 | from zope.component import getUtility |
---|
24 | from waeup.ikoba.interfaces import ( |
---|
25 | IExtFileStore, IFileStoreNameChooser, IIkobaUtils) |
---|
26 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
27 | from waeup.ikoba.customers.catalog import CustomersQuery |
---|
28 | from waeup.ikoba.customers.interfaces import ( |
---|
29 | ICustomer, ICSVCustomerExporter) |
---|
30 | from waeup.ikoba.utils.batching import ExporterBase |
---|
31 | from waeup.ikoba.utils.helpers import iface_names, to_timezone |
---|
32 | |
---|
33 | #: A tuple containing all exporter names referring to customers or |
---|
34 | #: subobjects thereof. |
---|
35 | EXPORTER_NAMES = ('customers', ) |
---|
36 | |
---|
37 | |
---|
38 | def get_customers(site, cust_filter=CustomersQuery()): |
---|
39 | """Get all customers registered in catalog in `site`. |
---|
40 | """ |
---|
41 | return cust_filter.query() |
---|
42 | |
---|
43 | |
---|
44 | class CustomerExporterBase(ExporterBase): |
---|
45 | """Exporter for customers or related objects. |
---|
46 | |
---|
47 | This is a baseclass. |
---|
48 | """ |
---|
49 | grok.baseclass() |
---|
50 | grok.implements(ICSVCustomerExporter) |
---|
51 | grok.provides(ICSVCustomerExporter) |
---|
52 | |
---|
53 | def filter_func(self, x, **kw): |
---|
54 | return x |
---|
55 | |
---|
56 | def get_filtered(self, site, **kw): |
---|
57 | """Get customers from a catalog filtered by keywords. |
---|
58 | |
---|
59 | customers_catalog is the default catalog. The keys must be valid |
---|
60 | catalog index names. |
---|
61 | Returns a simple empty list, a list with `Customer` |
---|
62 | objects or a catalog result set with `Customer` |
---|
63 | objects. |
---|
64 | |
---|
65 | .. seealso:: `waeup.ikoba.customers.catalog.CustomersCatalog` |
---|
66 | |
---|
67 | """ |
---|
68 | # Pass only given keywords to create FilteredCatalogQuery objects. |
---|
69 | # This way we avoid |
---|
70 | # trouble with `None` value ambivalences and queries are also |
---|
71 | # faster (normally less indexes to ask). Drawback is, that |
---|
72 | # developers must look into catalog to see what keywords are |
---|
73 | # valid. |
---|
74 | query = CustomersQuery(**kw) |
---|
75 | return query.query() |
---|
76 | |
---|
77 | def export(self, values, filepath=None): |
---|
78 | """Export `values`, an iterable, as CSV file. |
---|
79 | |
---|
80 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
81 | """ |
---|
82 | writer, outfile = self.get_csv_writer(filepath) |
---|
83 | for value in values: |
---|
84 | self.write_item(value, writer) |
---|
85 | return self.close_outfile(filepath, outfile) |
---|
86 | |
---|
87 | def export_all(self, site, filepath=None): |
---|
88 | """Export customers into filepath as CSV data. |
---|
89 | |
---|
90 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
91 | """ |
---|
92 | return self.export(self.filter_func(get_customers(site)), filepath) |
---|
93 | |
---|
94 | def export_customer(self, customer, filepath=None): |
---|
95 | return self.export(self.filter_func([customer]), filepath=filepath) |
---|
96 | |
---|
97 | def export_filtered(self, site, filepath=None, **kw): |
---|
98 | """Export items denoted by `kw`. |
---|
99 | |
---|
100 | If `filepath` is ``None``, a raw string with CSV data should |
---|
101 | be returned. |
---|
102 | """ |
---|
103 | data = self.get_filtered(site, **kw) |
---|
104 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
105 | |
---|
106 | |
---|
107 | class CustomersExporter(grok.GlobalUtility, CustomerExporterBase): |
---|
108 | """Exporter for Customers. |
---|
109 | """ |
---|
110 | grok.name('customers') |
---|
111 | |
---|
112 | #: Fieldnames considered by this exporter |
---|
113 | fields = tuple(sorted(iface_names(ICustomer))) + ( |
---|
114 | 'password', 'state', 'history',) |
---|
115 | |
---|
116 | #: The title under which this exporter will be displayed |
---|
117 | title = _(u'Customers') |
---|
118 | |
---|
119 | def mangle_value(self, value, name, context=None): |
---|
120 | if name == 'history': |
---|
121 | value = value.messages |
---|
122 | if name == 'phone' and value is not None: |
---|
123 | # Append hash '#' to phone numbers to circumvent |
---|
124 | # unwanted excel automatic |
---|
125 | value = str('%s#' % value) |
---|
126 | return super( |
---|
127 | CustomersExporter, self).mangle_value( |
---|
128 | value, name, context=context) |
---|