author | sylvain.thenault@logilab.fr |
Tue, 17 Feb 2009 18:17:40 +0100 | |
branch | tls-sprint |
changeset 684 | ddeab9691381 |
parent 536 | 781cd9fccbaa |
child 692 | 800592b8d39b |
child 949 | 1fba39d6ee70 |
permissions | -rw-r--r-- |
0 | 1 |
"""contains utility functions and some visual component to restrict results of |
2 |
a search |
|
3 |
||
4 |
:organization: Logilab |
|
407 | 5 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from itertools import chain |
|
11 |
from copy import deepcopy |
|
12 |
||
13 |
from logilab.mtconverter import html_escape |
|
14 |
||
15 |
from logilab.common.graph import has_path |
|
16 |
from logilab.common.decorators import cached |
|
17 |
from logilab.common.compat import all |
|
18 |
||
19 |
from rql import parse, nodes |
|
20 |
||
21 |
from cubicweb import Unauthorized, typed_eid |
|
407 | 22 |
from cubicweb.common.selectors import match_context_prop, one_has_relation |
0 | 23 |
from cubicweb.common.registerers import priority_registerer |
24 |
from cubicweb.common.appobject import AppRsetObject |
|
25 |
from cubicweb.common.utils import AcceptMixIn |
|
26 |
from cubicweb.web.htmlwidgets import HTMLWidget |
|
27 |
||
28 |
## rqlst manipulation functions used by facets ################################ |
|
29 |
||
30 |
def prepare_facets_rqlst(rqlst, args=None): |
|
31 |
"""prepare a syntax tree to generate facet filters |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
32 |
|
0 | 33 |
* remove ORDERBY clause |
34 |
* cleanup selection (remove everything) |
|
35 |
* undefine unnecessary variables |
|
36 |
* set DISTINCT |
|
37 |
* unset LIMIT/OFFSET |
|
38 |
""" |
|
39 |
if len(rqlst.children) > 1: |
|
40 |
raise NotImplementedError('FIXME: union not yet supported') |
|
41 |
select = rqlst.children[0] |
|
42 |
mainvar = filtered_variable(select) |
|
43 |
select.set_limit(None) |
|
44 |
select.set_offset(None) |
|
45 |
baserql = select.as_string(kwargs=args) |
|
46 |
# cleanup sort terms |
|
47 |
select.remove_sort_terms() |
|
48 |
# selection: only vocabulary entity |
|
49 |
for term in select.selection[:]: |
|
50 |
select.remove_selected(term) |
|
51 |
# remove unbound variables which only have some type restriction |
|
52 |
for dvar in select.defined_vars.values(): |
|
53 |
if not (dvar is mainvar or dvar.stinfo['relations']): |
|
54 |
select.undefine_variable(dvar) |
|
55 |
# global tree config: DISTINCT, LIMIT, OFFSET |
|
56 |
select.set_distinct(True) |
|
57 |
return mainvar, baserql |
|
58 |
||
59 |
def filtered_variable(rqlst): |
|
60 |
vref = rqlst.selection[0].iget_nodes(nodes.VariableRef).next() |
|
61 |
return vref.variable |
|
62 |
||
63 |
||
64 |
def get_facet(req, facetid, rqlst, mainvar): |
|
65 |
return req.vreg.object_by_id('facets', facetid, req, rqlst=rqlst, |
|
66 |
filtered_variable=mainvar) |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
67 |
|
0 | 68 |
|
69 |
def filter_hiddens(w, **kwargs): |
|
70 |
for key, val in kwargs.items(): |
|
71 |
w(u'<input type="hidden" name="%s" value="%s" />' % ( |
|
72 |
key, html_escape(val))) |
|
73 |
||
74 |
||
75 |
def _may_be_removed(rel, schema, mainvar): |
|
76 |
"""if the given relation may be removed from the tree, return the variable |
|
77 |
on the other side of `mainvar`, else return None |
|
78 |
Conditions: |
|
79 |
* the relation is an attribute selection of the main variable |
|
80 |
* the relation is optional relation linked to the main variable |
|
81 |
* the relation is a mandatory relation linked to the main variable |
|
82 |
without any restriction on the other variable |
|
83 |
""" |
|
84 |
lhs, rhs = rel.get_variable_parts() |
|
85 |
rschema = schema.rschema(rel.r_type) |
|
86 |
if lhs.variable is mainvar: |
|
87 |
try: |
|
88 |
ovar = rhs.variable |
|
89 |
except AttributeError: |
|
90 |
# constant restriction |
|
91 |
# XXX: X title LOWER(T) if it makes sense? |
|
92 |
return None |
|
93 |
if rschema.is_final(): |
|
94 |
if len(ovar.stinfo['relations']) == 1: |
|
95 |
# attribute selection |
|
96 |
return ovar |
|
97 |
return None |
|
98 |
opt = 'right' |
|
99 |
cardidx = 0 |
|
100 |
elif getattr(rhs, 'variable', None) is mainvar: |
|
101 |
ovar = lhs.variable |
|
102 |
opt = 'left' |
|
103 |
cardidx = 1 |
|
104 |
else: |
|
105 |
# not directly linked to the main variable |
|
106 |
return None |
|
107 |
if rel.optional in (opt, 'both'): |
|
108 |
# optional relation |
|
109 |
return ovar |
|
110 |
if all(rschema.rproperty(s, o, 'cardinality')[cardidx] in '1+' |
|
111 |
for s,o in rschema.iter_rdefs()): |
|
112 |
# mandatory relation without any restriction on the other variable |
|
113 |
for orel in ovar.stinfo['relations']: |
|
114 |
if rel is orel: |
|
115 |
continue |
|
116 |
if _may_be_removed(orel, schema, ovar) is None: |
|
117 |
return None |
|
118 |
return ovar |
|
119 |
return None |
|
120 |
||
121 |
def _add_rtype_relation(rqlst, mainvar, rtype, role): |
|
122 |
newvar = rqlst.make_variable() |
|
123 |
if role == 'object': |
|
124 |
rel = rqlst.add_relation(newvar, rtype, mainvar) |
|
125 |
else: |
|
126 |
rel = rqlst.add_relation(mainvar, rtype, newvar) |
|
127 |
return newvar, rel |
|
128 |
||
129 |
def _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role): |
|
130 |
"""prepare a syntax tree to generate a filter vocabulary rql using the given |
|
131 |
relation: |
|
132 |
* create a variable to filter on this relation |
|
133 |
* add the relation |
|
134 |
* add the new variable to GROUPBY clause if necessary |
|
135 |
* add the new variable to the selection |
|
136 |
""" |
|
137 |
newvar, rel = _add_rtype_relation(rqlst, mainvar, rtype, role) |
|
138 |
if rqlst.groupby: |
|
139 |
rqlst.add_group_var(newvar) |
|
140 |
rqlst.add_selected(newvar) |
|
141 |
return newvar, rel |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
142 |
|
0 | 143 |
def _remove_relation(rqlst, rel, var): |
144 |
"""remove a constraint relation from the syntax tree""" |
|
145 |
# remove the relation |
|
146 |
rqlst.remove_node(rel) |
|
147 |
# remove relations where the filtered variable appears on the |
|
148 |
# lhs and rhs is a constant restriction |
|
149 |
extra = [] |
|
150 |
for vrel in var.stinfo['relations']: |
|
151 |
if vrel is rel: |
|
152 |
continue |
|
153 |
if vrel.children[0].variable is var: |
|
154 |
if not vrel.children[1].get_nodes(nodes.Constant): |
|
155 |
extra.append(vrel) |
|
156 |
rqlst.remove_node(vrel) |
|
157 |
return extra |
|
158 |
||
159 |
def _set_orderby(rqlst, newvar, sortasc, sortfuncname): |
|
160 |
if sortfuncname is None: |
|
161 |
rqlst.add_sort_var(newvar, sortasc) |
|
162 |
else: |
|
163 |
vref = nodes.variable_ref(newvar) |
|
164 |
vref.register_reference() |
|
165 |
sortfunc = nodes.Function(sortfuncname) |
|
166 |
sortfunc.append(vref) |
|
167 |
term = nodes.SortTerm(sortfunc, sortasc) |
|
168 |
rqlst.add_sort_term(term) |
|
169 |
||
170 |
def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
|
171 |
sortfuncname=None, sortasc=True): |
|
172 |
"""modify a syntax tree to retrieve only relevant attribute `attr` of `var`""" |
|
173 |
_cleanup_rqlst(rqlst, mainvar) |
|
174 |
var, mainrel = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role) |
|
175 |
# not found, create one |
|
176 |
attrvar = rqlst.make_variable() |
|
177 |
attrrel = rqlst.add_relation(var, attrname, attrvar) |
|
178 |
# if query is grouped, we have to add the attribute variable |
|
179 |
if rqlst.groupby: |
|
180 |
if not attrvar in rqlst.groupby: |
|
181 |
rqlst.add_group_var(attrvar) |
|
182 |
_set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
|
183 |
# add attribute variable to selection |
|
184 |
rqlst.add_selected(attrvar) |
|
185 |
# add is restriction if necessary |
|
186 |
if not mainvar.stinfo['typerels']: |
|
187 |
etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
|
188 |
rqlst.add_type_restriction(mainvar, etypes) |
|
189 |
return var |
|
190 |
||
191 |
def _cleanup_rqlst(rqlst, mainvar): |
|
192 |
"""cleanup tree from unnecessary restriction: |
|
193 |
* attribute selection |
|
194 |
* optional relations linked to the main variable |
|
195 |
* mandatory relations linked to the main variable |
|
196 |
""" |
|
197 |
if rqlst.where is None: |
|
198 |
return |
|
199 |
schema = rqlst.root.schema |
|
200 |
toremove = set() |
|
201 |
vargraph = deepcopy(rqlst.vargraph) # graph representing links between variable |
|
202 |
for rel in rqlst.where.get_nodes(nodes.Relation): |
|
203 |
ovar = _may_be_removed(rel, schema, mainvar) |
|
204 |
if ovar is not None: |
|
205 |
toremove.add(ovar) |
|
206 |
removed = set() |
|
207 |
while toremove: |
|
208 |
trvar = toremove.pop() |
|
209 |
trvarname = trvar.name |
|
210 |
# remove paths using this variable from the graph |
|
211 |
linkedvars = vargraph.pop(trvarname) |
|
212 |
for ovarname in linkedvars: |
|
213 |
vargraph[ovarname].remove(trvarname) |
|
214 |
# remove relation using this variable |
|
215 |
for rel in chain(trvar.stinfo['relations'], trvar.stinfo['typerels']): |
|
216 |
if rel in removed: |
|
217 |
# already removed |
|
218 |
continue |
|
219 |
rqlst.remove_node(rel) |
|
220 |
removed.add(rel) |
|
221 |
# cleanup groupby clause |
|
222 |
if rqlst.groupby: |
|
223 |
for vref in rqlst.groupby[:]: |
|
224 |
if vref.name == trvarname: |
|
225 |
rqlst.remove_group_var(vref) |
|
226 |
# we can also remove all variables which are linked to this variable |
|
227 |
# and have no path to the main variable |
|
228 |
for ovarname in linkedvars: |
|
408
a8814ff6824e
reactivate tests and fix bug triggering removal of undesired relation (eg type restriction) in some cases
sylvain.thenault@logilab.fr
parents:
407
diff
changeset
|
229 |
if ovarname == mainvar.name: |
a8814ff6824e
reactivate tests and fix bug triggering removal of undesired relation (eg type restriction) in some cases
sylvain.thenault@logilab.fr
parents:
407
diff
changeset
|
230 |
continue |
0 | 231 |
if not has_path(vargraph, ovarname, mainvar.name): |
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
232 |
toremove.add(rqlst.defined_vars[ovarname]) |
0 | 233 |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
234 |
|
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
235 |
|
0 | 236 |
## base facet classes ######################################################### |
237 |
class AbstractFacet(AcceptMixIn, AppRsetObject): |
|
238 |
__registerer__ = priority_registerer |
|
239 |
__abstract__ = True |
|
240 |
__registry__ = 'facets' |
|
241 |
property_defs = { |
|
242 |
_('visible'): dict(type='Boolean', default=True, |
|
243 |
help=_('display the box or not')), |
|
244 |
_('order'): dict(type='Int', default=99, |
|
245 |
help=_('display order of the box')), |
|
496
e25a3c2f5393
set default to empty string, not None
sylvain.thenault@logilab.fr
parents:
467
diff
changeset
|
246 |
_('context'): dict(type='String', default='', |
0 | 247 |
# None <-> both |
446
3a3ab6bbccc5
use empty string instead of None as possible context value to avoid getting an <optgroup> tag in associated widget
sylvain.thenault@logilab.fr
parents:
408
diff
changeset
|
248 |
vocabulary=(_('tablefilter'), _('facetbox'), ''), |
0 | 249 |
help=_('context where this box should be displayed')), |
250 |
} |
|
251 |
visible = True |
|
446
3a3ab6bbccc5
use empty string instead of None as possible context value to avoid getting an <optgroup> tag in associated widget
sylvain.thenault@logilab.fr
parents:
408
diff
changeset
|
252 |
context = '' |
0 | 253 |
needs_update = False |
254 |
start_unfolded = True |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
255 |
|
0 | 256 |
@classmethod |
257 |
def selected(cls, req, rset=None, rqlst=None, context=None, |
|
258 |
filtered_variable=None): |
|
259 |
assert rset is not None or rqlst is not None |
|
260 |
assert filtered_variable |
|
261 |
instance = super(AbstractFacet, cls).selected(req, rset) |
|
262 |
#instance = AppRsetObject.selected(req, rset) |
|
263 |
#instance.__class__ = cls |
|
264 |
# facet retreived using `object_by_id` from an ajax call |
|
265 |
if rset is None: |
|
266 |
instance.init_from_form(rqlst=rqlst) |
|
267 |
# facet retreived from `select` using the result set to filter |
|
268 |
else: |
|
269 |
instance.init_from_rset() |
|
270 |
instance.filtered_variable = filtered_variable |
|
271 |
return instance |
|
272 |
||
273 |
def init_from_rset(self): |
|
274 |
self.rqlst = self.rset.syntax_tree().children[0] |
|
275 |
||
276 |
def init_from_form(self, rqlst): |
|
277 |
self.rqlst = rqlst |
|
278 |
||
279 |
@property |
|
280 |
def operator(self): |
|
281 |
# OR between selected values by default |
|
282 |
return self.req.form.get(self.id + '_andor', 'OR') |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
283 |
|
0 | 284 |
def get_widget(self): |
285 |
"""return the widget instance to use to display this facet |
|
286 |
""" |
|
287 |
raise NotImplementedError |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
288 |
|
0 | 289 |
def add_rql_restrictions(self): |
290 |
"""add restriction for this facet into the rql syntax tree""" |
|
291 |
raise NotImplementedError |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
292 |
|
0 | 293 |
|
294 |
class VocabularyFacet(AbstractFacet): |
|
295 |
needs_update = True |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
296 |
|
0 | 297 |
def get_widget(self): |
298 |
"""return the widget instance to use to display this facet |
|
299 |
||
300 |
default implentation expects a .vocabulary method on the facet and |
|
301 |
return a combobox displaying this vocabulary |
|
302 |
""" |
|
303 |
vocab = self.vocabulary() |
|
304 |
if len(vocab) <= 1: |
|
305 |
return None |
|
306 |
wdg = FacetVocabularyWidget(self) |
|
307 |
selected = frozenset(typed_eid(eid) for eid in self.req.list_form_param(self.id)) |
|
308 |
for label, value in vocab: |
|
309 |
if value is None: |
|
310 |
wdg.append(FacetSeparator(label)) |
|
311 |
else: |
|
203
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
312 |
wdg.append(FacetItem(self.req, label, value, value in selected)) |
0 | 313 |
return wdg |
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
314 |
|
0 | 315 |
def vocabulary(self): |
316 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
317 |
""" |
|
318 |
raise NotImplementedError |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
319 |
|
0 | 320 |
def possible_values(self): |
321 |
"""return a list of possible values (as string since it's used to |
|
322 |
compare to a form value in javascript) for this facet |
|
323 |
""" |
|
324 |
raise NotImplementedError |
|
325 |
||
326 |
def support_and(self): |
|
327 |
return False |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
328 |
|
0 | 329 |
def rqlexec(self, rql, args=None, cachekey=None): |
330 |
try: |
|
331 |
return self.req.execute(rql, args, cachekey) |
|
332 |
except Unauthorized: |
|
333 |
return [] |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
334 |
|
0 | 335 |
|
336 |
class RelationFacet(VocabularyFacet): |
|
407 | 337 |
__selectors__ = (one_has_relation, match_context_prop) |
0 | 338 |
# class attributes to configure the relation facet |
339 |
rtype = None |
|
340 |
role = 'subject' |
|
341 |
target_attr = 'eid' |
|
342 |
# set this to a stored procedure name if you want to sort on the result of |
|
343 |
# this function's result instead of direct value |
|
344 |
sortfunc = None |
|
345 |
# ascendant/descendant sorting |
|
346 |
sortasc = True |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
347 |
|
0 | 348 |
@property |
349 |
def title(self): |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
350 |
return display_name(self.req, self.rtype, form=self.role) |
0 | 351 |
|
352 |
def vocabulary(self): |
|
353 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
354 |
""" |
|
355 |
rqlst = self.rqlst |
|
356 |
rqlst.save_state() |
|
357 |
try: |
|
358 |
mainvar = self.filtered_variable |
|
359 |
insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role, |
|
360 |
self.target_attr, self.sortfunc, self.sortasc) |
|
346
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
361 |
try: |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
362 |
rset = self.rqlexec(rqlst.as_string(), self.rset.args, self.rset.cachekey) |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
363 |
except: |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
364 |
self.exception('error while getting vocabulary for %s, rql: %s', |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
365 |
self, rqlst.as_string()) |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
366 |
return () |
0 | 367 |
finally: |
368 |
rqlst.recover() |
|
369 |
return self.rset_vocabulary(rset) |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
370 |
|
0 | 371 |
def possible_values(self): |
372 |
"""return a list of possible values (as string since it's used to |
|
373 |
compare to a form value in javascript) for this facet |
|
374 |
""" |
|
375 |
rqlst = self.rqlst |
|
376 |
rqlst.save_state() |
|
377 |
try: |
|
378 |
_cleanup_rqlst(rqlst, self.filtered_variable) |
|
379 |
_prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, self.role) |
|
380 |
return [str(x) for x, in self.rqlexec(rqlst.as_string())] |
|
381 |
finally: |
|
382 |
rqlst.recover() |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
383 |
|
0 | 384 |
def rset_vocabulary(self, rset): |
385 |
_ = self.req._ |
|
386 |
return [(_(label), eid) for eid, label in rset] |
|
387 |
||
388 |
@cached |
|
389 |
def support_and(self): |
|
390 |
rschema = self.schema.rschema(self.rtype) |
|
391 |
if self.role == 'subject': |
|
392 |
cardidx = 0 |
|
393 |
else: |
|
394 |
cardidx = 1 |
|
395 |
# XXX when called via ajax, no rset to compute possible types |
|
396 |
possibletypes = self.rset and self.rset.column_types(0) |
|
397 |
for subjtype, objtype in rschema.iter_rdefs(): |
|
398 |
if possibletypes is not None: |
|
399 |
if self.role == 'subject': |
|
400 |
if not subjtype in possibletypes: |
|
401 |
continue |
|
402 |
elif not objtype in possibletypes: |
|
403 |
continue |
|
404 |
if rschema.rproperty(subjtype, objtype, 'cardinality')[cardidx] in '+*': |
|
405 |
return True |
|
406 |
return False |
|
407 |
||
408 |
def add_rql_restrictions(self): |
|
409 |
"""add restriction for this facet into the rql syntax tree""" |
|
410 |
value = self.req.form.get(self.id) |
|
411 |
if not value: |
|
412 |
return |
|
413 |
mainvar = self.filtered_variable |
|
414 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role)[0] |
|
415 |
if isinstance(value, basestring): |
|
416 |
# only one value selected |
|
417 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
418 |
elif self.operator == 'OR': |
|
419 |
# multiple values with OR operator |
|
420 |
# set_distinct only if rtype cardinality is > 1 |
|
421 |
if self.support_and(): |
|
422 |
self.rqlst.set_distinct(True) |
|
423 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
424 |
else: |
|
425 |
# multiple values with AND operator |
|
426 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
427 |
while value: |
|
428 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role)[0] |
|
429 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
430 |
||
431 |
||
432 |
class AttributeFacet(RelationFacet): |
|
433 |
# attribute type |
|
434 |
attrtype = 'String' |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
435 |
# type of comparison: default is an exact match on the attribute value |
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
436 |
comparator = '=' # could be '<', '<=', '>', '>=' |
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
437 |
|
0 | 438 |
def vocabulary(self): |
439 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
440 |
""" |
|
441 |
rqlst = self.rqlst |
|
442 |
rqlst.save_state() |
|
443 |
try: |
|
444 |
mainvar = self.filtered_variable |
|
445 |
_cleanup_rqlst(rqlst, mainvar) |
|
446 |
newvar, rel = _prepare_vocabulary_rqlst(rqlst, mainvar, self.rtype, self.role) |
|
447 |
_set_orderby(rqlst, newvar, self.sortasc, self.sortfunc) |
|
346
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
448 |
try: |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
449 |
rset = self.rqlexec(rqlst.as_string(), self.rset.args, self.rset.cachekey) |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
450 |
except: |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
451 |
self.exception('error while getting vocabulary for %s, rql: %s', |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
452 |
self, rqlst.as_string()) |
5bbb01a133ae
add try except to avoid error w/ unsupported stuff, log it and return incomplete filter form
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
203
diff
changeset
|
453 |
return () |
0 | 454 |
finally: |
455 |
rqlst.recover() |
|
456 |
return self.rset_vocabulary(rset) |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
457 |
|
0 | 458 |
def rset_vocabulary(self, rset): |
459 |
_ = self.req._ |
|
460 |
return [(_(value), value) for value, in rset] |
|
461 |
||
462 |
def support_and(self): |
|
463 |
return False |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
464 |
|
0 | 465 |
def add_rql_restrictions(self): |
466 |
"""add restriction for this facet into the rql syntax tree""" |
|
467 |
value = self.req.form.get(self.id) |
|
468 |
if not value: |
|
469 |
return |
|
470 |
mainvar = self.filtered_variable |
|
471 |
self.rqlst.add_constant_restriction(mainvar, self.rtype, value, |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
472 |
self.attrtype, self.comparator) |
0 | 473 |
|
474 |
||
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
475 |
|
0 | 476 |
class FilterRQLBuilder(object): |
477 |
"""called by javascript to get a rql string from filter form""" |
|
478 |
||
479 |
def __init__(self, req): |
|
480 |
self.req = req |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
481 |
|
0 | 482 |
def build_rql(self):#, tablefilter=False): |
483 |
form = self.req.form |
|
484 |
facetids = form['facets'].split(',') |
|
485 |
select = parse(form['baserql']).children[0] # XXX Union unsupported yet |
|
486 |
mainvar = filtered_variable(select) |
|
487 |
toupdate = [] |
|
488 |
for facetid in facetids: |
|
489 |
facet = get_facet(self.req, facetid, select, mainvar) |
|
490 |
facet.add_rql_restrictions() |
|
491 |
if facet.needs_update: |
|
492 |
toupdate.append(facetid) |
|
493 |
return select.as_string(), toupdate |
|
494 |
||
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
495 |
|
0 | 496 |
## html widets ################################################################ |
497 |
||
498 |
class FacetVocabularyWidget(HTMLWidget): |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
499 |
|
0 | 500 |
def __init__(self, facet): |
501 |
self.facet = facet |
|
502 |
self.items = [] |
|
503 |
||
504 |
def append(self, item): |
|
505 |
self.items.append(item) |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
506 |
|
0 | 507 |
def _render(self): |
508 |
title = html_escape(self.facet.title) |
|
509 |
facetid = html_escape(self.facet.id) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
510 |
if len(self.items) > 6: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
511 |
self.w(u'<div id="%s" class="facet overflowed">\n' % facetid) |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
512 |
else: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
513 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
0 | 514 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
515 |
(html_escape(facetid), title)) |
|
516 |
if self.facet.support_and(): |
|
517 |
_ = self.facet.req._ |
|
518 |
self.w(u'''<select name="%s" class="radio facetOperator" title="%s"> |
|
519 |
<option value="OR">%s</option> |
|
520 |
<option value="AND">%s</option> |
|
521 |
</select>''' % (facetid + '_andor', _('and/or between different values'), |
|
522 |
_('OR'), _('AND'))) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
523 |
cssclass = '' |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
524 |
if not self.facet.start_unfolded: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
525 |
cssclass += ' hidden' |
0 | 526 |
self.w(u'<div class="facetBody%s">\n' % cssclass) |
527 |
for item in self.items: |
|
528 |
item.render(self.w) |
|
529 |
self.w(u'</div>\n') |
|
530 |
self.w(u'</div>\n') |
|
531 |
||
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
532 |
|
0 | 533 |
class FacetStringWidget(HTMLWidget): |
534 |
def __init__(self, facet): |
|
535 |
self.facet = facet |
|
536 |
self.value = None |
|
537 |
||
538 |
def _render(self): |
|
539 |
title = html_escape(self.facet.title) |
|
540 |
facetid = html_escape(self.facet.id) |
|
541 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
|
542 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
|
543 |
(facetid, title)) |
|
544 |
self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u'')) |
|
545 |
self.w(u'</div>\n') |
|
546 |
||
547 |
||
548 |
class FacetItem(HTMLWidget): |
|
549 |
||
203
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
550 |
selected_img = "black-check.png" |
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
551 |
unselected_img = "no-check-no-border.png" |
0 | 552 |
|
203
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
553 |
def __init__(self, req, label, value, selected=False): |
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
554 |
self.req = req |
0 | 555 |
self.label = label |
556 |
self.value = value |
|
557 |
self.selected = selected |
|
558 |
||
559 |
def _render(self): |
|
560 |
if self.selected: |
|
561 |
cssclass = ' facetValueSelected' |
|
203
60cd67acf7fd
FacetItem now takes req as first parameter of __init__, THIS IS BACKWARD INCOMPATIBLE
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
562 |
imgsrc = self.req.datadir_url + self.selected_img |
536 | 563 |
imgalt = self.req._('selected') |
0 | 564 |
else: |
565 |
cssclass = '' |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
566 |
imgsrc = self.req.datadir_url + self.unselected_img |
536 | 567 |
imgalt = self.req._('not selected') |
0 | 568 |
self.w(u'<div class="facetValue facetCheckBox%s" cubicweb:value="%s">\n' |
569 |
% (cssclass, html_escape(unicode(self.value)))) |
|
536 | 570 |
self.w(u'<img src="%s" alt="%s"/> ' % (imgsrc, imgalt)) |
0 | 571 |
self.w(u'<a href="javascript: {}">%s</a>' % html_escape(self.label)) |
572 |
self.w(u'</div>') |
|
573 |
||
574 |
||
575 |
class FacetSeparator(HTMLWidget): |
|
576 |
def __init__(self, label=None): |
|
577 |
self.label = label or u' ' |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
578 |
|
0 | 579 |
def _render(self): |
580 |
pass |
|
581 |