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