root/CPS3/products/CPSLuceneCatalog/trunk/zcatalogquery.py

Revision 50556, 3.9 kB (checked in by gracinet, 3 years ago)

#1796: insert_condition implementation

Line 
1 # (C) Copyright 2006 Nuxeo SAS <http://nuxeo.com>
2 # Author: Julien Anguenot <ja@nuxeo.com>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 # 02111-1307, USA.
17 #
18 # $Id: interfaces.py 29338 2006-01-24 14:13:39Z janguenot $
19 """ZCatalog query
20 """
21
22 import logging
23
24 import zope.interface
25 from interfaces import IZCatalogQuery
26
27 from DateTime.DateTime import DateTime
28
29 logger = logging.getLogger("CPSLuceneCatalog.zcatalogquery")
30
31 class ZCatalogQuery(object):
32
33     zope.interface.implements(IZCatalogQuery)
34
35     def __init__(self, cat, REQUEST, **kw):
36
37         self.REQUEST = REQUEST
38
39         if 'cps_filter_sets' in kw.keys():
40             self.cps_filter_sets = kw['cps_filter_sets']
41             # FIXME : Implement this !
42             # We don't necessarly need them all.
43             del kw['cps_filter_sets']
44
45         self.fields = ()
46         self.options = {}
47
48         # Filter out options
49         for k, v in kw.items():
50
51             field_conf = {}
52
53             # catch condition before all manipulations
54             if isinstance(v, dict):
55                 condition = v.pop('insert_condition', None)
56                 if v.keys() == ['query']: # only one remains
57                     v = v['query']
58             else:
59                 condition = None
60
61             if k not in cat.getCatalog().getFieldNamesFor():
62                 # CPS BBB : ZCTitle case.
63                 if k == 'ZCTitle':
64                     if 'Title' in cat.getCatalog().getFieldNamesFor():
65                         field_conf = {
66                             'id'    : 'Title',
67                             'value' : v,
68                             }
69                         if condition is not None:
70                             field_conf['condition'] = condition
71                         self.fields += (field_conf,)
72                 else:
73                     self.options[k] = v
74             else:
75
76                 field_conf = {
77                     'id'    : k,
78                     'value' : v,
79                     }
80
81                 if condition is not None:
82                     field_conf['condition'] = condition
83                 if isinstance(v, DateTime):
84                     field_conf['value'] = v.ISO()
85
86                 # Date query ranges
87                 elif isinstance(v, dict):
88                     if 'query' in v.keys() and 'range' in v.keys():
89                         v1 = v['query']
90                         range_ = 'range:' + v['range']
91                         if isinstance(v1, DateTime):
92                             field_conf['value'] = v1.ISO()
93                             field_conf['usage'] = range_
94                         elif isinstance(v1, list) or isinstance(v1, tuple):
95                             if len(v1) == 2:
96                                 d1 = v1[0]
97                                 d2 = v1[1]
98                                 if (isinstance(d1, DateTime) and
99                                     isinstance(d2, DateTime)):
100                                     field_conf['value'] =  [d1.ISO(), d2.ISO()]
101                                 else:
102                                     field_conf['value'] =  [d1, d2]
103                         field_conf['usage'] = range_
104                 self.fields += (field_conf,)
105
106 ##        logger.debug("getFielsdMap() %s" % str(self.fields))
107 ##        logger.debug("getQueryOptions() %s" % str(self.options))
108
109     def getFieldsMap(self):
110         return self.fields
111
112     def getQueryOptions(self):
113         return self.options
Note: See TracBrowser for help on using the browser.