author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Wed, 27 May 2009 12:46:20 +0200 | |
branch | stable |
changeset 1957 | 92d1a15f08f7 |
parent 1896 | 8182746170c6 |
child 1933 | f40ee76ecdf1 |
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 |
|
1896
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
22 |
from cubicweb.utils import make_uid |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
23 |
from cubicweb.selectors import match_context_prop, partial_relation_possible |
722 | 24 |
from cubicweb.appobject import AppRsetObject |
0 | 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): |
|
1149 | 121 |
"""add a relation relying `mainvar` to entities linked by the `rtype` |
122 |
relation (where `mainvar` has `role`) |
|
123 |
||
124 |
return the inserted variable for linked entities. |
|
125 |
""" |
|
0 | 126 |
newvar = rqlst.make_variable() |
127 |
if role == 'object': |
|
1149 | 128 |
rqlst.add_relation(newvar, rtype, mainvar) |
0 | 129 |
else: |
1149 | 130 |
rqlst.add_relation(mainvar, rtype, newvar) |
131 |
return newvar |
|
0 | 132 |
|
133 |
def _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role): |
|
134 |
"""prepare a syntax tree to generate a filter vocabulary rql using the given |
|
135 |
relation: |
|
136 |
* create a variable to filter on this relation |
|
137 |
* add the relation |
|
138 |
* add the new variable to GROUPBY clause if necessary |
|
139 |
* add the new variable to the selection |
|
140 |
""" |
|
1149 | 141 |
newvar = _add_rtype_relation(rqlst, mainvar, rtype, role) |
0 | 142 |
if rqlst.groupby: |
143 |
rqlst.add_group_var(newvar) |
|
144 |
rqlst.add_selected(newvar) |
|
1149 | 145 |
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
|
146 |
|
0 | 147 |
def _remove_relation(rqlst, rel, var): |
148 |
"""remove a constraint relation from the syntax tree""" |
|
149 |
# remove the relation |
|
150 |
rqlst.remove_node(rel) |
|
151 |
# remove relations where the filtered variable appears on the |
|
152 |
# lhs and rhs is a constant restriction |
|
153 |
extra = [] |
|
154 |
for vrel in var.stinfo['relations']: |
|
155 |
if vrel is rel: |
|
156 |
continue |
|
157 |
if vrel.children[0].variable is var: |
|
158 |
if not vrel.children[1].get_nodes(nodes.Constant): |
|
159 |
extra.append(vrel) |
|
160 |
rqlst.remove_node(vrel) |
|
161 |
return extra |
|
162 |
||
163 |
def _set_orderby(rqlst, newvar, sortasc, sortfuncname): |
|
164 |
if sortfuncname is None: |
|
165 |
rqlst.add_sort_var(newvar, sortasc) |
|
166 |
else: |
|
167 |
vref = nodes.variable_ref(newvar) |
|
168 |
vref.register_reference() |
|
169 |
sortfunc = nodes.Function(sortfuncname) |
|
170 |
sortfunc.append(vref) |
|
171 |
term = nodes.SortTerm(sortfunc, sortasc) |
|
172 |
rqlst.add_sort_term(term) |
|
173 |
||
174 |
def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
|
175 |
sortfuncname=None, sortasc=True): |
|
176 |
"""modify a syntax tree to retrieve only relevant attribute `attr` of `var`""" |
|
177 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 178 |
var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role) |
0 | 179 |
# not found, create one |
180 |
attrvar = rqlst.make_variable() |
|
1149 | 181 |
rqlst.add_relation(var, attrname, attrvar) |
0 | 182 |
# if query is grouped, we have to add the attribute variable |
183 |
if rqlst.groupby: |
|
184 |
if not attrvar in rqlst.groupby: |
|
185 |
rqlst.add_group_var(attrvar) |
|
186 |
_set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
|
187 |
# add attribute variable to selection |
|
188 |
rqlst.add_selected(attrvar) |
|
189 |
# add is restriction if necessary |
|
190 |
if not mainvar.stinfo['typerels']: |
|
191 |
etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
|
192 |
rqlst.add_type_restriction(mainvar, etypes) |
|
193 |
return var |
|
194 |
||
195 |
def _cleanup_rqlst(rqlst, mainvar): |
|
196 |
"""cleanup tree from unnecessary restriction: |
|
197 |
* attribute selection |
|
198 |
* optional relations linked to the main variable |
|
199 |
* mandatory relations linked to the main variable |
|
200 |
""" |
|
201 |
if rqlst.where is None: |
|
202 |
return |
|
203 |
schema = rqlst.root.schema |
|
204 |
toremove = set() |
|
205 |
vargraph = deepcopy(rqlst.vargraph) # graph representing links between variable |
|
206 |
for rel in rqlst.where.get_nodes(nodes.Relation): |
|
207 |
ovar = _may_be_removed(rel, schema, mainvar) |
|
208 |
if ovar is not None: |
|
209 |
toremove.add(ovar) |
|
210 |
removed = set() |
|
211 |
while toremove: |
|
212 |
trvar = toremove.pop() |
|
213 |
trvarname = trvar.name |
|
214 |
# remove paths using this variable from the graph |
|
215 |
linkedvars = vargraph.pop(trvarname) |
|
216 |
for ovarname in linkedvars: |
|
217 |
vargraph[ovarname].remove(trvarname) |
|
218 |
# remove relation using this variable |
|
219 |
for rel in chain(trvar.stinfo['relations'], trvar.stinfo['typerels']): |
|
220 |
if rel in removed: |
|
221 |
# already removed |
|
222 |
continue |
|
223 |
rqlst.remove_node(rel) |
|
224 |
removed.add(rel) |
|
225 |
# cleanup groupby clause |
|
226 |
if rqlst.groupby: |
|
227 |
for vref in rqlst.groupby[:]: |
|
228 |
if vref.name == trvarname: |
|
229 |
rqlst.remove_group_var(vref) |
|
230 |
# we can also remove all variables which are linked to this variable |
|
231 |
# and have no path to the main variable |
|
232 |
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
|
233 |
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
|
234 |
continue |
0 | 235 |
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
|
236 |
toremove.add(rqlst.defined_vars[ovarname]) |
0 | 237 |
|
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
|
238 |
|
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
|
239 |
|
0 | 240 |
## base facet classes ######################################################### |
722 | 241 |
class AbstractFacet(AppRsetObject): |
0 | 242 |
__abstract__ = True |
243 |
__registry__ = 'facets' |
|
244 |
property_defs = { |
|
245 |
_('visible'): dict(type='Boolean', default=True, |
|
246 |
help=_('display the box or not')), |
|
247 |
_('order'): dict(type='Int', default=99, |
|
248 |
help=_('display order of the box')), |
|
496
e25a3c2f5393
set default to empty string, not None
sylvain.thenault@logilab.fr
parents:
467
diff
changeset
|
249 |
_('context'): dict(type='String', default='', |
0 | 250 |
# 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
|
251 |
vocabulary=(_('tablefilter'), _('facetbox'), ''), |
0 | 252 |
help=_('context where this box should be displayed')), |
253 |
} |
|
254 |
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
|
255 |
context = '' |
0 | 256 |
needs_update = False |
257 |
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
|
258 |
|
0 | 259 |
@classmethod |
260 |
def selected(cls, req, rset=None, rqlst=None, context=None, |
|
261 |
filtered_variable=None): |
|
262 |
assert rset is not None or rqlst is not None |
|
263 |
assert filtered_variable |
|
264 |
instance = super(AbstractFacet, cls).selected(req, rset) |
|
265 |
#instance = AppRsetObject.selected(req, rset) |
|
266 |
#instance.__class__ = cls |
|
267 |
# facet retreived using `object_by_id` from an ajax call |
|
268 |
if rset is None: |
|
269 |
instance.init_from_form(rqlst=rqlst) |
|
270 |
# facet retreived from `select` using the result set to filter |
|
271 |
else: |
|
272 |
instance.init_from_rset() |
|
273 |
instance.filtered_variable = filtered_variable |
|
274 |
return instance |
|
275 |
||
276 |
def init_from_rset(self): |
|
277 |
self.rqlst = self.rset.syntax_tree().children[0] |
|
278 |
||
279 |
def init_from_form(self, rqlst): |
|
280 |
self.rqlst = rqlst |
|
281 |
||
282 |
@property |
|
283 |
def operator(self): |
|
284 |
# OR between selected values by default |
|
285 |
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
|
286 |
|
0 | 287 |
def get_widget(self): |
288 |
"""return the widget instance to use to display this facet |
|
289 |
""" |
|
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 |
def add_rql_restrictions(self): |
293 |
"""add restriction for this facet into the rql syntax tree""" |
|
294 |
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
|
295 |
|
0 | 296 |
|
297 |
class VocabularyFacet(AbstractFacet): |
|
298 |
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
|
299 |
|
0 | 300 |
def get_widget(self): |
301 |
"""return the widget instance to use to display this facet |
|
302 |
||
303 |
default implentation expects a .vocabulary method on the facet and |
|
304 |
return a combobox displaying this vocabulary |
|
305 |
""" |
|
306 |
vocab = self.vocabulary() |
|
307 |
if len(vocab) <= 1: |
|
308 |
return None |
|
309 |
wdg = FacetVocabularyWidget(self) |
|
310 |
selected = frozenset(typed_eid(eid) for eid in self.req.list_form_param(self.id)) |
|
311 |
for label, value in vocab: |
|
312 |
if value is None: |
|
313 |
wdg.append(FacetSeparator(label)) |
|
314 |
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
|
315 |
wdg.append(FacetItem(self.req, label, value, value in selected)) |
0 | 316 |
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
|
317 |
|
0 | 318 |
def vocabulary(self): |
319 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
320 |
""" |
|
321 |
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
|
322 |
|
0 | 323 |
def possible_values(self): |
324 |
"""return a list of possible values (as string since it's used to |
|
325 |
compare to a form value in javascript) for this facet |
|
326 |
""" |
|
327 |
raise NotImplementedError |
|
328 |
||
329 |
def support_and(self): |
|
330 |
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
|
331 |
|
0 | 332 |
def rqlexec(self, rql, args=None, cachekey=None): |
333 |
try: |
|
334 |
return self.req.execute(rql, args, cachekey) |
|
335 |
except Unauthorized: |
|
336 |
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
|
337 |
|
0 | 338 |
|
339 |
class RelationFacet(VocabularyFacet): |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
340 |
__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
|
341 |
# class attributes to configure the rel ation facet |
0 | 342 |
rtype = None |
1433 | 343 |
role = 'subject' |
0 | 344 |
target_attr = 'eid' |
345 |
# set this to a stored procedure name if you want to sort on the result of |
|
346 |
# this function's result instead of direct value |
|
347 |
sortfunc = None |
|
348 |
# ascendant/descendant sorting |
|
349 |
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
|
350 |
|
0 | 351 |
@property |
352 |
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
|
353 |
return display_name(self.req, self.rtype, form=self.role) |
0 | 354 |
|
355 |
def vocabulary(self): |
|
356 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
357 |
""" |
|
358 |
rqlst = self.rqlst |
|
359 |
rqlst.save_state() |
|
360 |
try: |
|
361 |
mainvar = self.filtered_variable |
|
362 |
insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role, |
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
return () |
0 | 370 |
finally: |
371 |
rqlst.recover() |
|
372 |
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
|
373 |
|
0 | 374 |
def possible_values(self): |
375 |
"""return a list of possible values (as string since it's used to |
|
376 |
compare to a form value in javascript) for this facet |
|
377 |
""" |
|
378 |
rqlst = self.rqlst |
|
379 |
rqlst.save_state() |
|
380 |
try: |
|
381 |
_cleanup_rqlst(rqlst, self.filtered_variable) |
|
382 |
_prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, self.role) |
|
383 |
return [str(x) for x, in self.rqlexec(rqlst.as_string())] |
|
384 |
finally: |
|
385 |
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
|
386 |
|
0 | 387 |
def rset_vocabulary(self, rset): |
388 |
_ = self.req._ |
|
389 |
return [(_(label), eid) for eid, label in rset] |
|
390 |
||
391 |
@cached |
|
392 |
def support_and(self): |
|
393 |
rschema = self.schema.rschema(self.rtype) |
|
394 |
if self.role == 'subject': |
|
395 |
cardidx = 0 |
|
396 |
else: |
|
397 |
cardidx = 1 |
|
398 |
# XXX when called via ajax, no rset to compute possible types |
|
399 |
possibletypes = self.rset and self.rset.column_types(0) |
|
400 |
for subjtype, objtype in rschema.iter_rdefs(): |
|
401 |
if possibletypes is not None: |
|
402 |
if self.role == 'subject': |
|
403 |
if not subjtype in possibletypes: |
|
404 |
continue |
|
405 |
elif not objtype in possibletypes: |
|
406 |
continue |
|
407 |
if rschema.rproperty(subjtype, objtype, 'cardinality')[cardidx] in '+*': |
|
408 |
return True |
|
409 |
return False |
|
410 |
||
411 |
def add_rql_restrictions(self): |
|
412 |
"""add restriction for this facet into the rql syntax tree""" |
|
413 |
value = self.req.form.get(self.id) |
|
414 |
if not value: |
|
415 |
return |
|
416 |
mainvar = self.filtered_variable |
|
1149 | 417 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 418 |
if isinstance(value, basestring): |
419 |
# only one value selected |
|
420 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
421 |
elif self.operator == 'OR': |
|
422 |
# multiple values with OR operator |
|
423 |
# set_distinct only if rtype cardinality is > 1 |
|
424 |
if self.support_and(): |
|
425 |
self.rqlst.set_distinct(True) |
|
426 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
427 |
else: |
|
428 |
# multiple values with AND operator |
|
429 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
430 |
while value: |
|
1149 | 431 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 432 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
433 |
||
434 |
||
435 |
class AttributeFacet(RelationFacet): |
|
436 |
# attribute type |
|
437 |
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
|
438 |
# 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
|
439 |
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
|
440 |
|
0 | 441 |
def vocabulary(self): |
442 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
443 |
""" |
|
444 |
rqlst = self.rqlst |
|
445 |
rqlst.save_state() |
|
446 |
try: |
|
447 |
mainvar = self.filtered_variable |
|
448 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 449 |
newvar = _prepare_vocabulary_rqlst(rqlst, mainvar, self.rtype, self.role) |
0 | 450 |
_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
|
451 |
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
|
452 |
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
|
453 |
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
|
454 |
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
|
455 |
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
|
456 |
return () |
0 | 457 |
finally: |
458 |
rqlst.recover() |
|
459 |
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
|
460 |
|
0 | 461 |
def rset_vocabulary(self, rset): |
462 |
_ = self.req._ |
|
463 |
return [(_(value), value) for value, in rset] |
|
464 |
||
465 |
def support_and(self): |
|
466 |
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
|
467 |
|
0 | 468 |
def add_rql_restrictions(self): |
469 |
"""add restriction for this facet into the rql syntax tree""" |
|
470 |
value = self.req.form.get(self.id) |
|
471 |
if not value: |
|
472 |
return |
|
473 |
mainvar = self.filtered_variable |
|
474 |
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
|
475 |
self.attrtype, self.comparator) |
0 | 476 |
|
477 |
||
1896
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
478 |
class RangeFacet(AttributeFacet): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
479 |
|
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
480 |
def get_widget(self): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
481 |
"""return the widget instance to use to display this facet |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
482 |
""" |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
483 |
values = set(value for _, value in self.vocabulary() if value is not None) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
484 |
return FacetRangeWidget(self, min(values), max(values)) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
485 |
|
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
486 |
def add_rql_restrictions(self): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
487 |
infvalue = self.req.form.get('%s_inf' % self.id) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
488 |
if not infvalue: |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
489 |
return |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
490 |
supvalue = self.req.form.get('%s_sup' % self.id) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
491 |
self.rqlst.add_constant_restriction(self.filtered_variable, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
492 |
self.rtype, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
493 |
u'%s' % infvalue, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
494 |
'Float', '>=') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
495 |
self.rqlst.add_constant_restriction(self.filtered_variable, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
496 |
self.rtype, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
497 |
u'%s' % supvalue, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
498 |
'Float', '<=') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
499 |
|
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
|
500 |
|
0 | 501 |
class FilterRQLBuilder(object): |
502 |
"""called by javascript to get a rql string from filter form""" |
|
503 |
||
504 |
def __init__(self, req): |
|
505 |
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
|
506 |
|
0 | 507 |
def build_rql(self):#, tablefilter=False): |
508 |
form = self.req.form |
|
509 |
facetids = form['facets'].split(',') |
|
510 |
select = parse(form['baserql']).children[0] # XXX Union unsupported yet |
|
511 |
mainvar = filtered_variable(select) |
|
512 |
toupdate = [] |
|
513 |
for facetid in facetids: |
|
514 |
facet = get_facet(self.req, facetid, select, mainvar) |
|
515 |
facet.add_rql_restrictions() |
|
516 |
if facet.needs_update: |
|
517 |
toupdate.append(facetid) |
|
518 |
return select.as_string(), toupdate |
|
519 |
||
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
|
520 |
|
0 | 521 |
## html widets ################################################################ |
522 |
||
523 |
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
|
524 |
|
0 | 525 |
def __init__(self, facet): |
526 |
self.facet = facet |
|
527 |
self.items = [] |
|
528 |
||
529 |
def append(self, item): |
|
530 |
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
|
531 |
|
0 | 532 |
def _render(self): |
533 |
title = html_escape(self.facet.title) |
|
534 |
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
|
535 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
0 | 536 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
537 |
(html_escape(facetid), title)) |
|
538 |
if self.facet.support_and(): |
|
539 |
_ = self.facet.req._ |
|
540 |
self.w(u'''<select name="%s" class="radio facetOperator" title="%s"> |
|
541 |
<option value="OR">%s</option> |
|
542 |
<option value="AND">%s</option> |
|
543 |
</select>''' % (facetid + '_andor', _('and/or between different values'), |
|
544 |
_('OR'), _('AND'))) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
545 |
cssclass = '' |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
546 |
if not self.facet.start_unfolded: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
547 |
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
|
548 |
if len(self.items) > 6: |
1132 | 549 |
cssclass += ' overflowed' |
0 | 550 |
self.w(u'<div class="facetBody%s">\n' % cssclass) |
551 |
for item in self.items: |
|
552 |
item.render(self.w) |
|
553 |
self.w(u'</div>\n') |
|
554 |
self.w(u'</div>\n') |
|
555 |
||
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
|
556 |
|
0 | 557 |
class FacetStringWidget(HTMLWidget): |
558 |
def __init__(self, facet): |
|
559 |
self.facet = facet |
|
560 |
self.value = None |
|
561 |
||
562 |
def _render(self): |
|
563 |
title = html_escape(self.facet.title) |
|
564 |
facetid = html_escape(self.facet.id) |
|
565 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
|
566 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
|
567 |
(facetid, title)) |
|
568 |
self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u'')) |
|
569 |
self.w(u'</div>\n') |
|
570 |
||
571 |
||
1896
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
572 |
class FacetRangeWidget(HTMLWidget): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
573 |
onload = u''' |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
574 |
jQuery("#%(sliderid)s").slider({ |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
575 |
range: true, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
576 |
min: %(minvalue)s, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
577 |
max: %(maxvalue)s, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
578 |
values: [%(minvalue)s, %(maxvalue)s], |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
579 |
slide: function(event, ui) { |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
580 |
$('#%(sliderid)s_inf').html(ui.values[0]); |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
581 |
$('#%(sliderid)s_sup').html(ui.values[1]); |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
582 |
$('input[name=%(facetid)s_inf]').val(ui.values[0]); |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
583 |
$('input[name=%(facetid)s_sup]').val(ui.values[1]); |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
584 |
} |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
585 |
}); |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
586 |
''' |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
587 |
def __init__(self, facet, minvalue, maxvalue): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
588 |
self.facet = facet |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
589 |
self.minvalue = minvalue |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
590 |
self.maxvalue = maxvalue |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
591 |
|
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
592 |
def _render(self): |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
593 |
facet = self.facet |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
594 |
facet.req.add_js('ui.slider.js') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
595 |
facet.req.add_css('ui.all.css') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
596 |
sliderid = make_uid('the slider') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
597 |
facetid = html_escape(self.facet.id) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
598 |
facet.req.html_headers.add_onload(self.onload % { |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
599 |
'sliderid': sliderid, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
600 |
'facetid': facetid, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
601 |
'minvalue': self.minvalue, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
602 |
'maxvalue': self.maxvalue, |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
603 |
}) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
604 |
title = html_escape(self.facet.title) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
605 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
606 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
607 |
(facetid, title)) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
608 |
self.w(u'<span id="%s_inf">%s</span> - <span id="%s_sup">%s</span>' |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
609 |
% (sliderid, self.minvalue, sliderid, self.maxvalue)) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
610 |
self.w(u'<input type="hidden" name="%s_inf" value="%s" />' |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
611 |
% (facetid, self.minvalue)) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
612 |
self.w(u'<input type="hidden" name="%s_sup" value="%s" />' |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
613 |
% (facetid, self.maxvalue)) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
614 |
self.w(u'<div id="%s"></div>' % sliderid) |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
615 |
self.w(u'</div>\n') |
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
616 |
|
8182746170c6
work in progress: new 'range facet' usable for numerical values, still lacks the form.submit on mouseUp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1433
diff
changeset
|
617 |
|
0 | 618 |
class FacetItem(HTMLWidget): |
619 |
||
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
|
620 |
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
|
621 |
unselected_img = "no-check-no-border.png" |
0 | 622 |
|
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
|
623 |
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
|
624 |
self.req = req |
0 | 625 |
self.label = label |
626 |
self.value = value |
|
627 |
self.selected = selected |
|
628 |
||
629 |
def _render(self): |
|
630 |
if self.selected: |
|
631 |
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
|
632 |
imgsrc = self.req.datadir_url + self.selected_img |
536 | 633 |
imgalt = self.req._('selected') |
0 | 634 |
else: |
635 |
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
|
636 |
imgsrc = self.req.datadir_url + self.unselected_img |
536 | 637 |
imgalt = self.req._('not selected') |
0 | 638 |
self.w(u'<div class="facetValue facetCheckBox%s" cubicweb:value="%s">\n' |
639 |
% (cssclass, html_escape(unicode(self.value)))) |
|
536 | 640 |
self.w(u'<img src="%s" alt="%s"/> ' % (imgsrc, imgalt)) |
0 | 641 |
self.w(u'<a href="javascript: {}">%s</a>' % html_escape(self.label)) |
642 |
self.w(u'</div>') |
|
643 |
||
644 |
||
645 |
class FacetSeparator(HTMLWidget): |
|
646 |
def __init__(self, label=None): |
|
647 |
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
|
648 |
|
0 | 649 |
def _render(self): |
650 |
pass |
|
651 |