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