source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/catalog.py @ 12517

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

Uups, we forgot to index and export the contract user_id which is always the customer_id for contracts.

  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1## $Id: catalog.py 12289 2014-12-21 22:17:06Z 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"""Cataloging and searching components for customers.
19"""
20import grok
21from grok import index
22from hurry.query import Eq, Text
23from hurry.query.query import Query
24from zope.catalog.interfaces import ICatalog
25from zope.component import queryUtility
26from waeup.ikoba.catalog import FilteredCatalogQueryBase
27from waeup.ikoba.interfaces import (
28    ICompany, IQueryResultItem)
29from waeup.ikoba.customers.interfaces import ICustomer, IContract
30
31
32class CustomersCatalog(grok.Indexes):
33    """A catalog for customers.
34    """
35    grok.site(ICompany)
36    grok.name('customers_catalog')
37    grok.context(ICustomer)
38
39    customer_id = index.Field(attribute='customer_id')
40    fullname = index.Text(attribute='fullname')
41    email = index.Field(attribute='email')
42    reg_number = index.Field(attribute='reg_number')
43    state = index.Field(attribute='state')
44
45
46class CustomerQueryResultItem(object):
47    grok.implements(IQueryResultItem)
48
49    title = u'Customer Query Item'
50    description = u'Some customer found in a search'
51
52    def __init__(self, context, view):
53        self.context = context
54        self.url = view.url(context)
55        self.customer_id = context.customer_id
56        self.display_fullname = context.display_fullname
57        self.reg_number = context.reg_number
58        self.state = context.state
59        self.translated_state = context.translated_state
60
61
62def search(query=None, searchtype=None, view=None):
63    hitlist = []
64    if searchtype in ('fullname',):
65        results = Query().searchResults(
66            Text(('customers_catalog', searchtype), query))
67    elif searchtype == 'suspended':
68        # 'suspended' is not indexed
69        cat = queryUtility(ICatalog, name='customers_catalog')
70        all = cat.searchResults(customer_id=(None, None))
71        for customer in all:
72            if customer.suspended:
73                hitlist.append(CustomerQueryResultItem(customer, view=view))
74        return hitlist
75    else:
76        # Temporary solution to display all customers added
77        if query == '*':
78            cat = queryUtility(ICatalog, name='customers_catalog')
79            results = cat.searchResults(customer_id=(None, None))
80        else:
81            results = Query().searchResults(
82                Eq(('customers_catalog', searchtype), query))
83    for result in results:
84        hitlist.append(CustomerQueryResultItem(result, view=view))
85    return hitlist
86
87
88class SimpleFieldSearch(object):
89    """A programmatic (no UI required) search.
90
91    Looks up a given field attribute of the customers catalog for a
92    single value. So normally you would call an instance of this
93    search like this:
94
95      >>> SimpleFieldSearch()(reg_number='somevalue')
96
97    """
98    catalog_name = 'customers_catalog'
99
100    def __call__(self, **kw):
101        """Search customers catalog programmatically.
102        """
103        if len(kw) != 1:
104            raise ValueError('must give exactly one index name to search')
105        cat = queryUtility(ICatalog, name=self.catalog_name)
106        index_name, query_term = kw.items()[0]
107        results = cat.searchResults(index_name=(query_term, query_term))
108        return results
109
110#: an instance of `SimpleFieldSearch` looking up customers catalog.
111simple_search = SimpleFieldSearch()
112
113
114class CustomersQuery(FilteredCatalogQueryBase):
115    """Query customers in a site. See waeup.ikoba.catalog for more info.
116    """
117    cat_name = 'customers_catalog'
118    defaults = dict(customer_id=None)  # make sure we get all studs by default
119
120
121# Catalog for customer contracts
122
123class ContractsCatalog(grok.Indexes):
124    """A catalog for all contracts.
125    """
126    grok.site(ICompany)
127    grok.name('contracts_catalog')
128    grok.context(IContract)
129
130    contract_id = grok.index.Field(attribute='contract_id')
131    last_product_id = grok.index.Field(attribute='last_product_id')
132    contract_category = grok.index.Field(attribute='contract_category')
133    user_id = grok.index.Field(attribute='user_id')
Note: See TracBrowser for help on using the repository browser.