1 | ## $Id: catalog.py 10465 2013-08-07 11:18:43Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 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 | """ |
---|
20 | import grok |
---|
21 | from grok import index |
---|
22 | from hurry.query import Eq, Text |
---|
23 | from hurry.query.query import Query |
---|
24 | from zope.catalog.interfaces import ICatalog |
---|
25 | from zope.component import queryUtility |
---|
26 | from waeup.ikoba.catalog import FilteredCatalogQueryBase |
---|
27 | from waeup.ikoba.interfaces import ( |
---|
28 | ICompany, IQueryResultItem) |
---|
29 | from waeup.ikoba.customers.interfaces import ICustomer |
---|
30 | |
---|
31 | class CustomersCatalog(grok.Indexes): |
---|
32 | """A catalog for customers. |
---|
33 | """ |
---|
34 | grok.site(ICompany) |
---|
35 | grok.name('customers_catalog') |
---|
36 | grok.context(ICustomer) |
---|
37 | |
---|
38 | customer_id = index.Field(attribute='customer_id') |
---|
39 | fullname = index.Text(attribute='fullname') |
---|
40 | email = index.Field(attribute='email') |
---|
41 | reg_number = index.Field(attribute='reg_number') |
---|
42 | state = index.Field(attribute='state') |
---|
43 | |
---|
44 | class CustomerQueryResultItem(object): |
---|
45 | grok.implements(IQueryResultItem) |
---|
46 | |
---|
47 | title = u'Customer Query Item' |
---|
48 | description = u'Some customer found in a search' |
---|
49 | |
---|
50 | def __init__(self, context, view): |
---|
51 | self.context = context |
---|
52 | self.url = view.url(context) |
---|
53 | self.customer_id = context.customer_id |
---|
54 | self.display_fullname = context.display_fullname |
---|
55 | self.reg_number = context.reg_number |
---|
56 | self.state = context.state |
---|
57 | self.translated_state = context.translated_state |
---|
58 | |
---|
59 | def search(query=None, searchtype=None, view=None): |
---|
60 | hitlist = [] |
---|
61 | if searchtype in ('fullname',): |
---|
62 | results = Query().searchResults( |
---|
63 | Text(('customers_catalog', searchtype), query)) |
---|
64 | elif searchtype == 'suspended': |
---|
65 | # 'suspended' is not indexed |
---|
66 | cat = queryUtility(ICatalog, name='customers_catalog') |
---|
67 | all = cat.searchResults(customer_id=(None, None)) |
---|
68 | for customer in all: |
---|
69 | if customer.suspended: |
---|
70 | hitlist.append(CustomerQueryResultItem(customer, view=view)) |
---|
71 | return hitlist |
---|
72 | else: |
---|
73 | # Temporary solution to display all customers added |
---|
74 | if query == '*': |
---|
75 | cat = queryUtility(ICatalog, name='customers_catalog') |
---|
76 | results = cat.searchResults(customer_id=(None, None)) |
---|
77 | else: |
---|
78 | results = Query().searchResults( |
---|
79 | Eq(('customers_catalog', searchtype), query)) |
---|
80 | for result in results: |
---|
81 | hitlist.append(CustomerQueryResultItem(result, view=view)) |
---|
82 | return hitlist |
---|
83 | |
---|
84 | class SimpleFieldSearch(object): |
---|
85 | """A programmatic (no UI required) search. |
---|
86 | |
---|
87 | Looks up a given field attribute of the customers catalog for a |
---|
88 | single value. So normally you would call an instance of this |
---|
89 | search like this: |
---|
90 | |
---|
91 | >>> SimpleFieldSearch()(reg_number='somevalue') |
---|
92 | |
---|
93 | """ |
---|
94 | catalog_name = 'customers_catalog' |
---|
95 | def __call__(self, **kw): |
---|
96 | """Search customers catalog programmatically. |
---|
97 | """ |
---|
98 | if len(kw) != 1: |
---|
99 | raise ValueError('must give exactly one index name to search') |
---|
100 | cat = queryUtility(ICatalog, name=self.catalog_name) |
---|
101 | index_name, query_term = kw.items()[0] |
---|
102 | results = cat.searchResults(index_name=(query_term, query_term)) |
---|
103 | return results |
---|
104 | |
---|
105 | #: an instance of `SimpleFieldSearch` looking up customers catalog. |
---|
106 | simple_search = SimpleFieldSearch() |
---|
107 | |
---|
108 | class CustomersQuery(FilteredCatalogQueryBase): |
---|
109 | """Query customers in a site. See waeup.ikoba.catalog for more info. |
---|
110 | """ |
---|
111 | cat_name = 'customers_catalog' |
---|
112 | defaults = dict(customer_id=None) # make sure we get all studs by default |
---|