source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/export.py @ 12045

Last change on this file since 12045 was 11958, checked in by Henrik Bettermann, 10 years ago

Add components for customer management. Some tests are still missing.

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