1 | ## $Id: export.py 14220 2016-10-20 12:50:20Z 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) |
---|
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 | ICustomerSampleDocument, ISampleContractProcess) |
---|
31 | from waeup.ikoba.utils.batching import ExporterBase |
---|
32 | from waeup.ikoba.utils.helpers import iface_names, to_timezone |
---|
33 | |
---|
34 | def get_customers(site, cust_filter=CustomersQuery()): |
---|
35 | """Get all customers registered in catalog in `site`. |
---|
36 | """ |
---|
37 | return cust_filter.query() |
---|
38 | |
---|
39 | def get_documents(customers, class_name): |
---|
40 | """Get all documents of `customers`. |
---|
41 | """ |
---|
42 | documents = [] |
---|
43 | for customer in customers: |
---|
44 | for document in customer.get('documents', {}).values(): |
---|
45 | if document.class_name == class_name: |
---|
46 | documents.append(document) |
---|
47 | return documents |
---|
48 | |
---|
49 | def get_contracts(customers, class_name): |
---|
50 | """Get all contracts of `customers`. |
---|
51 | """ |
---|
52 | contracts = [] |
---|
53 | for customer in customers: |
---|
54 | for contract in customer.get('contracts', {}).values(): |
---|
55 | if getattr(contract, 'class_name', None) == class_name: |
---|
56 | contracts.append(contract) |
---|
57 | return contracts |
---|
58 | |
---|
59 | class CustomerExporterBase(ExporterBase): |
---|
60 | """Exporter for customers or related objects. |
---|
61 | |
---|
62 | This is a baseclass. |
---|
63 | """ |
---|
64 | grok.baseclass() |
---|
65 | grok.implements(ICSVCustomerExporter) |
---|
66 | grok.provides(ICSVCustomerExporter) |
---|
67 | |
---|
68 | def filter_func(self, x, **kw): |
---|
69 | return x |
---|
70 | |
---|
71 | def get_filtered(self, site, **kw): |
---|
72 | """Get customers from a catalog filtered by keywords. |
---|
73 | |
---|
74 | customers_catalog is the default catalog. The keys must be valid |
---|
75 | catalog index names. |
---|
76 | Returns a simple empty list, a list with `Customer` |
---|
77 | objects or a catalog result set with `Customer` |
---|
78 | objects. |
---|
79 | |
---|
80 | .. seealso:: `waeup.ikoba.customers.catalog.CustomersCatalog` |
---|
81 | |
---|
82 | """ |
---|
83 | # Pass only given keywords to create FilteredCatalogQuery objects. |
---|
84 | # This way we avoid |
---|
85 | # trouble with `None` value ambivalences and queries are also |
---|
86 | # faster (normally less indexes to ask). Drawback is, that |
---|
87 | # developers must look into catalog to see what keywords are |
---|
88 | # valid. |
---|
89 | query = CustomersQuery(**kw) |
---|
90 | return query.query() |
---|
91 | |
---|
92 | def export(self, values, filepath=None): |
---|
93 | """Export `values`, an iterable, as CSV file. |
---|
94 | |
---|
95 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
96 | """ |
---|
97 | writer, outfile = self.get_csv_writer(filepath) |
---|
98 | for value in values: |
---|
99 | self.write_item(value, writer) |
---|
100 | return self.close_outfile(filepath, outfile) |
---|
101 | |
---|
102 | def export_all(self, site, filepath=None): |
---|
103 | """Export customers into filepath as CSV data. |
---|
104 | |
---|
105 | If `filepath` is ``None``, a raw string with CSV data is returned. |
---|
106 | """ |
---|
107 | return self.export(self.filter_func(get_customers(site)), filepath) |
---|
108 | |
---|
109 | def export_customer(self, customer, filepath=None): |
---|
110 | return self.export(self.filter_func([customer]), filepath=filepath) |
---|
111 | |
---|
112 | def export_filtered(self, site, filepath=None, **kw): |
---|
113 | """Export items denoted by `kw`. |
---|
114 | |
---|
115 | If `filepath` is ``None``, a raw string with CSV data should |
---|
116 | be returned. |
---|
117 | """ |
---|
118 | data = self.get_filtered(site, **kw) |
---|
119 | return self.export(self.filter_func(data, **kw), filepath=filepath) |
---|
120 | |
---|
121 | |
---|
122 | class CustomerExporter(grok.GlobalUtility, CustomerExporterBase): |
---|
123 | """Exporter for Customers. |
---|
124 | """ |
---|
125 | grok.name('customers') |
---|
126 | |
---|
127 | iface = ICustomer |
---|
128 | |
---|
129 | #: The title under which this exporter will be displayed |
---|
130 | title = _(u'Customers') |
---|
131 | |
---|
132 | #: Fieldnames considered by this exporter |
---|
133 | @property |
---|
134 | def fields(self): |
---|
135 | return tuple(sorted(iface_names(self.iface))) + ( |
---|
136 | 'password', 'state', 'history',) |
---|
137 | |
---|
138 | def mangle_value(self, value, name, context=None): |
---|
139 | if name == 'history': |
---|
140 | value = value.messages |
---|
141 | if name == 'phone' and value is not None: |
---|
142 | # Append hash '#' to phone numbers to circumvent |
---|
143 | # unwanted excel automatic |
---|
144 | value = str('%s#' % value) |
---|
145 | return super( |
---|
146 | CustomerExporter, self).mangle_value( |
---|
147 | value, name, context=context) |
---|
148 | |
---|
149 | |
---|
150 | class CustomerDocumentExporterBase(grok.GlobalUtility, CustomerExporterBase): |
---|
151 | """Exporter for Customer Document instances. |
---|
152 | |
---|
153 | This is a baseclass. |
---|
154 | """ |
---|
155 | grok.baseclass() |
---|
156 | iface = None |
---|
157 | class_name = None |
---|
158 | title = None |
---|
159 | |
---|
160 | #: Fieldnames considered by this exporter |
---|
161 | @property |
---|
162 | def fields(self): |
---|
163 | return tuple( |
---|
164 | sorted(iface_names( |
---|
165 | self.iface, exclude_attribs=False, |
---|
166 | omit=['is_editable_by_customer', |
---|
167 | 'is_editable_by_manager', |
---|
168 | 'is_verifiable', |
---|
169 | 'translated_state', |
---|
170 | 'formatted_transition_date', |
---|
171 | 'translated_class_name', |
---|
172 | 'connected_files', # Could be used to export file URLs |
---|
173 | 'form_fields_interface', |
---|
174 | 'filenames', |
---|
175 | 'local_roles', |
---|
176 | ]))) |
---|
177 | |
---|
178 | def filter_func(self, x, **kw): |
---|
179 | return get_documents(x, self.class_name) |
---|
180 | |
---|
181 | def mangle_value(self, value, name, context=None): |
---|
182 | |
---|
183 | if name == 'history': |
---|
184 | value = value.messages |
---|
185 | return super( |
---|
186 | CustomerDocumentExporterBase, self).mangle_value( |
---|
187 | value, name, context=context) |
---|
188 | |
---|
189 | |
---|
190 | class CustomerSampleDocumentExporter(CustomerDocumentExporterBase): |
---|
191 | """Exporter for CustomerSampleDocument instances. |
---|
192 | """ |
---|
193 | grok.name('customersampledocuments') |
---|
194 | |
---|
195 | iface = ICustomerSampleDocument |
---|
196 | class_name = 'CustomerSampleDocument' |
---|
197 | title = _(u'Customer Sample Documents') |
---|
198 | |
---|
199 | |
---|
200 | class ContractExporterBase(grok.GlobalUtility, CustomerExporterBase): |
---|
201 | """Exporter for Contract instances. |
---|
202 | |
---|
203 | This is a baseclass. |
---|
204 | """ |
---|
205 | grok.baseclass() |
---|
206 | iface = None |
---|
207 | class_name = None |
---|
208 | title = None |
---|
209 | |
---|
210 | #: Fieldnames considered by this exporter |
---|
211 | @property |
---|
212 | def fields(self): |
---|
213 | return tuple( |
---|
214 | sorted(iface_names( |
---|
215 | self.iface, exclude_attribs=False, |
---|
216 | omit=['translated_state', |
---|
217 | 'formatted_transition_date', |
---|
218 | 'translated_class_name', |
---|
219 | 'is_editable', |
---|
220 | 'is_approvable', |
---|
221 | 'customer']))) |
---|
222 | |
---|
223 | def filter_func(self, x, **kw): |
---|
224 | return get_contracts(x, self.class_name) |
---|
225 | |
---|
226 | def mangle_value(self, value, name, context=None): |
---|
227 | |
---|
228 | if name == 'history': |
---|
229 | return value.messages |
---|
230 | if name.endswith('_object'): |
---|
231 | mangled_value = getattr(value, 'document_id', None) |
---|
232 | if mangled_value: |
---|
233 | return mangled_value |
---|
234 | mangled_value = getattr(value, 'product_id', None) |
---|
235 | if mangled_value: |
---|
236 | return mangled_value |
---|
237 | if name == 'product_options' and value is not None: |
---|
238 | return [eval(entry.to_string()) for entry in value] |
---|
239 | return super( |
---|
240 | ContractExporterBase, self).mangle_value( |
---|
241 | value, name, context=context) |
---|
242 | |
---|
243 | |
---|
244 | class SampleContractExporter(ContractExporterBase): |
---|
245 | """Exporter for sample contracts. |
---|
246 | """ |
---|
247 | grok.name('samplecontracts') |
---|
248 | iface = ISampleContractProcess |
---|
249 | class_name = 'SampleContract' |
---|
250 | title = _(u'Customer Sample Contracts') |
---|