author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Fri, 29 May 2009 19:36:08 +0200 | |
changeset 2010 | c594323929cb |
parent 1977 | 606923dff11b |
child 2014 | 71a9ab77d9f8 |
permissions | -rw-r--r-- |
0 | 1 |
"""contains utility functions and some visual component to restrict results of |
2 |
a search |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1933
diff
changeset
|
5 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1933
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
from itertools import chain |
|
12 |
from copy import deepcopy |
|
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
13 |
from datetime import date |
0 | 14 |
|
15 |
from logilab.mtconverter import html_escape |
|
16 |
||
17 |
from logilab.common.graph import has_path |
|
18 |
from logilab.common.decorators import cached |
|
19 |
from logilab.common.compat import all |
|
20 |
||
21 |
from rql import parse, nodes |
|
22 |
||
23 |
from cubicweb import Unauthorized, typed_eid |
|
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
24 |
from cubicweb.utils import datetime2ticks, make_uid, ustrftime |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
25 |
from cubicweb.selectors import match_context_prop, partial_relation_possible |
722 | 26 |
from cubicweb.appobject import AppRsetObject |
0 | 27 |
from cubicweb.web.htmlwidgets import HTMLWidget |
28 |
||
29 |
## rqlst manipulation functions used by facets ################################ |
|
30 |
||
31 |
def prepare_facets_rqlst(rqlst, args=None): |
|
32 |
"""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
|
33 |
|
0 | 34 |
* remove ORDERBY clause |
35 |
* cleanup selection (remove everything) |
|
36 |
* undefine unnecessary variables |
|
37 |
* set DISTINCT |
|
38 |
* unset LIMIT/OFFSET |
|
39 |
""" |
|
40 |
if len(rqlst.children) > 1: |
|
41 |
raise NotImplementedError('FIXME: union not yet supported') |
|
42 |
select = rqlst.children[0] |
|
43 |
mainvar = filtered_variable(select) |
|
44 |
select.set_limit(None) |
|
45 |
select.set_offset(None) |
|
46 |
baserql = select.as_string(kwargs=args) |
|
47 |
# cleanup sort terms |
|
48 |
select.remove_sort_terms() |
|
49 |
# selection: only vocabulary entity |
|
50 |
for term in select.selection[:]: |
|
51 |
select.remove_selected(term) |
|
52 |
# remove unbound variables which only have some type restriction |
|
53 |
for dvar in select.defined_vars.values(): |
|
54 |
if not (dvar is mainvar or dvar.stinfo['relations']): |
|
55 |
select.undefine_variable(dvar) |
|
56 |
# global tree config: DISTINCT, LIMIT, OFFSET |
|
57 |
select.set_distinct(True) |
|
58 |
return mainvar, baserql |
|
59 |
||
60 |
def filtered_variable(rqlst): |
|
61 |
vref = rqlst.selection[0].iget_nodes(nodes.VariableRef).next() |
|
62 |
return vref.variable |
|
63 |
||
64 |
||
65 |
def get_facet(req, facetid, rqlst, mainvar): |
|
66 |
return req.vreg.object_by_id('facets', facetid, req, rqlst=rqlst, |
|
67 |
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
|
68 |
|
0 | 69 |
|
70 |
def filter_hiddens(w, **kwargs): |
|
71 |
for key, val in kwargs.items(): |
|
72 |
w(u'<input type="hidden" name="%s" value="%s" />' % ( |
|
73 |
key, html_escape(val))) |
|
74 |
||
75 |
||
76 |
def _may_be_removed(rel, schema, mainvar): |
|
77 |
"""if the given relation may be removed from the tree, return the variable |
|
78 |
on the other side of `mainvar`, else return None |
|
79 |
Conditions: |
|
80 |
* the relation is an attribute selection of the main variable |
|
81 |
* the relation is optional relation linked to the main variable |
|
82 |
* the relation is a mandatory relation linked to the main variable |
|
83 |
without any restriction on the other variable |
|
84 |
""" |
|
85 |
lhs, rhs = rel.get_variable_parts() |
|
86 |
rschema = schema.rschema(rel.r_type) |
|
87 |
if lhs.variable is mainvar: |
|
88 |
try: |
|
89 |
ovar = rhs.variable |
|
90 |
except AttributeError: |
|
91 |
# constant restriction |
|
92 |
# XXX: X title LOWER(T) if it makes sense? |
|
93 |
return None |
|
94 |
if rschema.is_final(): |
|
95 |
if len(ovar.stinfo['relations']) == 1: |
|
96 |
# attribute selection |
|
97 |
return ovar |
|
98 |
return None |
|
99 |
opt = 'right' |
|
100 |
cardidx = 0 |
|
101 |
elif getattr(rhs, 'variable', None) is mainvar: |
|
102 |
ovar = lhs.variable |
|
103 |
opt = 'left' |
|
104 |
cardidx = 1 |
|
105 |
else: |
|
106 |
# not directly linked to the main variable |
|
107 |
return None |
|
108 |
if rel.optional in (opt, 'both'): |
|
109 |
# optional relation |
|
110 |
return ovar |
|
111 |
if all(rschema.rproperty(s, o, 'cardinality')[cardidx] in '1+' |
|
112 |
for s,o in rschema.iter_rdefs()): |
|
113 |
# mandatory relation without any restriction on the other variable |
|
114 |
for orel in ovar.stinfo['relations']: |
|
115 |
if rel is orel: |
|
116 |
continue |
|
117 |
if _may_be_removed(orel, schema, ovar) is None: |
|
118 |
return None |
|
119 |
return ovar |
|
120 |
return None |
|
121 |
||
122 |
def _add_rtype_relation(rqlst, mainvar, rtype, role): |
|
1149 | 123 |
"""add a relation relying `mainvar` to entities linked by the `rtype` |
124 |
relation (where `mainvar` has `role`) |
|
125 |
||
126 |
return the inserted variable for linked entities. |
|
127 |
""" |
|
0 | 128 |
newvar = rqlst.make_variable() |
129 |
if role == 'object': |
|
1149 | 130 |
rqlst.add_relation(newvar, rtype, mainvar) |
0 | 131 |
else: |
1149 | 132 |
rqlst.add_relation(mainvar, rtype, newvar) |
133 |
return newvar |
|
0 | 134 |
|
135 |
def _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role): |
|
136 |
"""prepare a syntax tree to generate a filter vocabulary rql using the given |
|
137 |
relation: |
|
138 |
* create a variable to filter on this relation |
|
139 |
* add the relation |
|
140 |
* add the new variable to GROUPBY clause if necessary |
|
141 |
* add the new variable to the selection |
|
142 |
""" |
|
1149 | 143 |
newvar = _add_rtype_relation(rqlst, mainvar, rtype, role) |
0 | 144 |
if rqlst.groupby: |
145 |
rqlst.add_group_var(newvar) |
|
146 |
rqlst.add_selected(newvar) |
|
1149 | 147 |
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
|
148 |
|
0 | 149 |
def _remove_relation(rqlst, rel, var): |
150 |
"""remove a constraint relation from the syntax tree""" |
|
151 |
# remove the relation |
|
152 |
rqlst.remove_node(rel) |
|
153 |
# remove relations where the filtered variable appears on the |
|
154 |
# lhs and rhs is a constant restriction |
|
155 |
extra = [] |
|
156 |
for vrel in var.stinfo['relations']: |
|
157 |
if vrel is rel: |
|
158 |
continue |
|
159 |
if vrel.children[0].variable is var: |
|
160 |
if not vrel.children[1].get_nodes(nodes.Constant): |
|
161 |
extra.append(vrel) |
|
162 |
rqlst.remove_node(vrel) |
|
163 |
return extra |
|
164 |
||
165 |
def _set_orderby(rqlst, newvar, sortasc, sortfuncname): |
|
166 |
if sortfuncname is None: |
|
167 |
rqlst.add_sort_var(newvar, sortasc) |
|
168 |
else: |
|
169 |
vref = nodes.variable_ref(newvar) |
|
170 |
vref.register_reference() |
|
171 |
sortfunc = nodes.Function(sortfuncname) |
|
172 |
sortfunc.append(vref) |
|
173 |
term = nodes.SortTerm(sortfunc, sortasc) |
|
174 |
rqlst.add_sort_term(term) |
|
175 |
||
176 |
def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
|
177 |
sortfuncname=None, sortasc=True): |
|
178 |
"""modify a syntax tree to retrieve only relevant attribute `attr` of `var`""" |
|
179 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 180 |
var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role) |
0 | 181 |
# not found, create one |
182 |
attrvar = rqlst.make_variable() |
|
1149 | 183 |
rqlst.add_relation(var, attrname, attrvar) |
0 | 184 |
# if query is grouped, we have to add the attribute variable |
185 |
if rqlst.groupby: |
|
186 |
if not attrvar in rqlst.groupby: |
|
187 |
rqlst.add_group_var(attrvar) |
|
188 |
_set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
|
189 |
# add attribute variable to selection |
|
190 |
rqlst.add_selected(attrvar) |
|
191 |
# add is restriction if necessary |
|
192 |
if not mainvar.stinfo['typerels']: |
|
193 |
etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
|
194 |
rqlst.add_type_restriction(mainvar, etypes) |
|
195 |
return var |
|
196 |
||
197 |
def _cleanup_rqlst(rqlst, mainvar): |
|
198 |
"""cleanup tree from unnecessary restriction: |
|
199 |
* attribute selection |
|
200 |
* optional relations linked to the main variable |
|
201 |
* mandatory relations linked to the main variable |
|
202 |
""" |
|
203 |
if rqlst.where is None: |
|
204 |
return |
|
205 |
schema = rqlst.root.schema |
|
206 |
toremove = set() |
|
207 |
vargraph = deepcopy(rqlst.vargraph) # graph representing links between variable |
|
208 |
for rel in rqlst.where.get_nodes(nodes.Relation): |
|
209 |
ovar = _may_be_removed(rel, schema, mainvar) |
|
210 |
if ovar is not None: |
|
211 |
toremove.add(ovar) |
|
212 |
removed = set() |
|
213 |
while toremove: |
|
214 |
trvar = toremove.pop() |
|
215 |
trvarname = trvar.name |
|
216 |
# remove paths using this variable from the graph |
|
217 |
linkedvars = vargraph.pop(trvarname) |
|
218 |
for ovarname in linkedvars: |
|
219 |
vargraph[ovarname].remove(trvarname) |
|
220 |
# remove relation using this variable |
|
221 |
for rel in chain(trvar.stinfo['relations'], trvar.stinfo['typerels']): |
|
222 |
if rel in removed: |
|
223 |
# already removed |
|
224 |
continue |
|
225 |
rqlst.remove_node(rel) |
|
226 |
removed.add(rel) |
|
227 |
# cleanup groupby clause |
|
228 |
if rqlst.groupby: |
|
229 |
for vref in rqlst.groupby[:]: |
|
230 |
if vref.name == trvarname: |
|
231 |
rqlst.remove_group_var(vref) |
|
232 |
# we can also remove all variables which are linked to this variable |
|
233 |
# and have no path to the main variable |
|
234 |
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
|
235 |
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
|
236 |
continue |
0 | 237 |
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
|
238 |
toremove.add(rqlst.defined_vars[ovarname]) |
0 | 239 |
|
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
|
240 |
|
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
|
241 |
|
0 | 242 |
## base facet classes ######################################################### |
722 | 243 |
class AbstractFacet(AppRsetObject): |
0 | 244 |
__abstract__ = True |
245 |
__registry__ = 'facets' |
|
246 |
property_defs = { |
|
247 |
_('visible'): dict(type='Boolean', default=True, |
|
248 |
help=_('display the box or not')), |
|
249 |
_('order'): dict(type='Int', default=99, |
|
250 |
help=_('display order of the box')), |
|
496
e25a3c2f5393
set default to empty string, not None
sylvain.thenault@logilab.fr
parents:
467
diff
changeset
|
251 |
_('context'): dict(type='String', default='', |
0 | 252 |
# 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
|
253 |
vocabulary=(_('tablefilter'), _('facetbox'), ''), |
0 | 254 |
help=_('context where this box should be displayed')), |
255 |
} |
|
256 |
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
|
257 |
context = '' |
0 | 258 |
needs_update = False |
259 |
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
|
260 |
|
0 | 261 |
@classmethod |
262 |
def selected(cls, req, rset=None, rqlst=None, context=None, |
|
263 |
filtered_variable=None): |
|
264 |
assert rset is not None or rqlst is not None |
|
265 |
assert filtered_variable |
|
266 |
instance = super(AbstractFacet, cls).selected(req, rset) |
|
267 |
#instance = AppRsetObject.selected(req, rset) |
|
268 |
#instance.__class__ = cls |
|
269 |
# facet retreived using `object_by_id` from an ajax call |
|
270 |
if rset is None: |
|
271 |
instance.init_from_form(rqlst=rqlst) |
|
272 |
# facet retreived from `select` using the result set to filter |
|
273 |
else: |
|
274 |
instance.init_from_rset() |
|
275 |
instance.filtered_variable = filtered_variable |
|
276 |
return instance |
|
277 |
||
278 |
def init_from_rset(self): |
|
279 |
self.rqlst = self.rset.syntax_tree().children[0] |
|
280 |
||
281 |
def init_from_form(self, rqlst): |
|
282 |
self.rqlst = rqlst |
|
283 |
||
284 |
@property |
|
285 |
def operator(self): |
|
286 |
# OR between selected values by default |
|
287 |
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
|
288 |
|
0 | 289 |
def get_widget(self): |
290 |
"""return the widget instance to use to display this facet |
|
291 |
""" |
|
292 |
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
|
293 |
|
0 | 294 |
def add_rql_restrictions(self): |
295 |
"""add restriction for this facet into the rql syntax tree""" |
|
296 |
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
|
297 |
|
0 | 298 |
|
299 |
class VocabularyFacet(AbstractFacet): |
|
300 |
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
|
301 |
|
0 | 302 |
def get_widget(self): |
303 |
"""return the widget instance to use to display this facet |
|
304 |
||
305 |
default implentation expects a .vocabulary method on the facet and |
|
306 |
return a combobox displaying this vocabulary |
|
307 |
""" |
|
308 |
vocab = self.vocabulary() |
|
309 |
if len(vocab) <= 1: |
|
310 |
return None |
|
311 |
wdg = FacetVocabularyWidget(self) |
|
312 |
selected = frozenset(typed_eid(eid) for eid in self.req.list_form_param(self.id)) |
|
313 |
for label, value in vocab: |
|
314 |
if value is None: |
|
315 |
wdg.append(FacetSeparator(label)) |
|
316 |
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
|
317 |
wdg.append(FacetItem(self.req, label, value, value in selected)) |
0 | 318 |
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
|
319 |
|
0 | 320 |
def vocabulary(self): |
321 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
322 |
""" |
|
323 |
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
|
324 |
|
0 | 325 |
def possible_values(self): |
326 |
"""return a list of possible values (as string since it's used to |
|
327 |
compare to a form value in javascript) for this facet |
|
328 |
""" |
|
329 |
raise NotImplementedError |
|
330 |
||
331 |
def support_and(self): |
|
332 |
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
|
333 |
|
0 | 334 |
def rqlexec(self, rql, args=None, cachekey=None): |
335 |
try: |
|
336 |
return self.req.execute(rql, args, cachekey) |
|
337 |
except Unauthorized: |
|
338 |
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
|
339 |
|
0 | 340 |
|
341 |
class RelationFacet(VocabularyFacet): |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
342 |
__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
|
343 |
# class attributes to configure the rel ation facet |
0 | 344 |
rtype = None |
1433 | 345 |
role = 'subject' |
0 | 346 |
target_attr = 'eid' |
347 |
# set this to a stored procedure name if you want to sort on the result of |
|
348 |
# this function's result instead of direct value |
|
349 |
sortfunc = None |
|
350 |
# ascendant/descendant sorting |
|
351 |
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
|
352 |
|
0 | 353 |
@property |
354 |
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
|
355 |
return display_name(self.req, self.rtype, form=self.role) |
0 | 356 |
|
357 |
def vocabulary(self): |
|
358 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
359 |
""" |
|
360 |
rqlst = self.rqlst |
|
361 |
rqlst.save_state() |
|
362 |
try: |
|
363 |
mainvar = self.filtered_variable |
|
364 |
insert_attr_select_relation(rqlst, mainvar, self.rtype, self.role, |
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
return () |
0 | 372 |
finally: |
373 |
rqlst.recover() |
|
374 |
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
|
375 |
|
0 | 376 |
def possible_values(self): |
377 |
"""return a list of possible values (as string since it's used to |
|
378 |
compare to a form value in javascript) for this facet |
|
379 |
""" |
|
380 |
rqlst = self.rqlst |
|
381 |
rqlst.save_state() |
|
382 |
try: |
|
383 |
_cleanup_rqlst(rqlst, self.filtered_variable) |
|
384 |
_prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, self.role) |
|
385 |
return [str(x) for x, in self.rqlexec(rqlst.as_string())] |
|
386 |
finally: |
|
387 |
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
|
388 |
|
0 | 389 |
def rset_vocabulary(self, rset): |
390 |
_ = self.req._ |
|
391 |
return [(_(label), eid) for eid, label in rset] |
|
392 |
||
393 |
@cached |
|
394 |
def support_and(self): |
|
395 |
rschema = self.schema.rschema(self.rtype) |
|
396 |
if self.role == 'subject': |
|
397 |
cardidx = 0 |
|
398 |
else: |
|
399 |
cardidx = 1 |
|
400 |
# XXX when called via ajax, no rset to compute possible types |
|
401 |
possibletypes = self.rset and self.rset.column_types(0) |
|
402 |
for subjtype, objtype in rschema.iter_rdefs(): |
|
403 |
if possibletypes is not None: |
|
404 |
if self.role == 'subject': |
|
405 |
if not subjtype in possibletypes: |
|
406 |
continue |
|
407 |
elif not objtype in possibletypes: |
|
408 |
continue |
|
409 |
if rschema.rproperty(subjtype, objtype, 'cardinality')[cardidx] in '+*': |
|
410 |
return True |
|
411 |
return False |
|
412 |
||
413 |
def add_rql_restrictions(self): |
|
414 |
"""add restriction for this facet into the rql syntax tree""" |
|
415 |
value = self.req.form.get(self.id) |
|
416 |
if not value: |
|
417 |
return |
|
418 |
mainvar = self.filtered_variable |
|
1149 | 419 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 420 |
if isinstance(value, basestring): |
421 |
# only one value selected |
|
422 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
423 |
elif self.operator == 'OR': |
|
424 |
# multiple values with OR operator |
|
425 |
# set_distinct only if rtype cardinality is > 1 |
|
426 |
if self.support_and(): |
|
427 |
self.rqlst.set_distinct(True) |
|
428 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
429 |
else: |
|
430 |
# multiple values with AND operator |
|
431 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
432 |
while value: |
|
1149 | 433 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 434 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
435 |
||
436 |
||
437 |
class AttributeFacet(RelationFacet): |
|
438 |
# attribute type |
|
439 |
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
|
440 |
# 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
|
441 |
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
|
442 |
|
0 | 443 |
def vocabulary(self): |
444 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
445 |
""" |
|
446 |
rqlst = self.rqlst |
|
447 |
rqlst.save_state() |
|
448 |
try: |
|
449 |
mainvar = self.filtered_variable |
|
450 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 451 |
newvar = _prepare_vocabulary_rqlst(rqlst, mainvar, self.rtype, self.role) |
0 | 452 |
_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
|
453 |
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
|
454 |
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
|
455 |
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
|
456 |
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
|
457 |
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
|
458 |
return () |
0 | 459 |
finally: |
460 |
rqlst.recover() |
|
461 |
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
|
462 |
|
0 | 463 |
def rset_vocabulary(self, rset): |
464 |
_ = self.req._ |
|
465 |
return [(_(value), value) for value, in rset] |
|
466 |
||
467 |
def support_and(self): |
|
468 |
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
|
469 |
|
0 | 470 |
def add_rql_restrictions(self): |
471 |
"""add restriction for this facet into the rql syntax tree""" |
|
472 |
value = self.req.form.get(self.id) |
|
473 |
if not value: |
|
474 |
return |
|
475 |
mainvar = self.filtered_variable |
|
476 |
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
|
477 |
self.attrtype, self.comparator) |
0 | 478 |
|
479 |
||
480 |
class FilterRQLBuilder(object): |
|
481 |
"""called by javascript to get a rql string from filter form""" |
|
482 |
||
483 |
def __init__(self, req): |
|
484 |
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
|
485 |
|
0 | 486 |
def build_rql(self):#, tablefilter=False): |
487 |
form = self.req.form |
|
488 |
facetids = form['facets'].split(',') |
|
489 |
select = parse(form['baserql']).children[0] # XXX Union unsupported yet |
|
490 |
mainvar = filtered_variable(select) |
|
491 |
toupdate = [] |
|
492 |
for facetid in facetids: |
|
493 |
facet = get_facet(self.req, facetid, select, mainvar) |
|
494 |
facet.add_rql_restrictions() |
|
495 |
if facet.needs_update: |
|
496 |
toupdate.append(facetid) |
|
497 |
return select.as_string(), toupdate |
|
498 |
||
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
499 |
|
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
500 |
class RangeFacet(AttributeFacet): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
501 |
attrtype = 'Float' # only numerical types are supported |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
502 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
503 |
@property |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
504 |
def wdgclass(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
505 |
return FacetRangeWidget |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
506 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
507 |
def get_widget(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
508 |
"""return the widget instance to use to display this facet |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
509 |
""" |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
510 |
values = set(value for _, value in self.vocabulary() if value is not None) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
511 |
return self.wdgclass(self, min(values), max(values)) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
512 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
513 |
def infvalue(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
514 |
return self.req.form.get('%s_inf' % self.id) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
515 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
516 |
def supvalue(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
517 |
return self.req.form.get('%s_sup' % self.id) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
518 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
519 |
def formatvalue(self, value): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
520 |
return unicode(value) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
521 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
522 |
def add_rql_restrictions(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
523 |
infvalue = self.infvalue() |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
524 |
if infvalue is None: # nothing sent |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
525 |
return |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
526 |
supvalue = self.supvalue() |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
527 |
self.rqlst.add_constant_restriction(self.filtered_variable, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
528 |
self.rtype, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
529 |
self.formatvalue(infvalue), |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
530 |
self.attrtype, '>=') |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
531 |
self.rqlst.add_constant_restriction(self.filtered_variable, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
532 |
self.rtype, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
533 |
self.formatvalue(supvalue), |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
534 |
self.attrtype, '<=') |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
535 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
536 |
class DateRangeFacet(RangeFacet): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
537 |
attrtype = 'Date' # only date types are supported |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
538 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
539 |
@property |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
540 |
def wdgclass(self): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
541 |
fmt = self.req.property_value('ui.date-format') |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
542 |
self.req.html_headers.define_var('DATE_FMT', fmt) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
543 |
return DateFacetRangeWidget |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
544 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
545 |
def formatvalue(self, value): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
546 |
return '"%s"' % date.fromtimestamp(float(value) / 1000).strftime('%Y/%m/%d') |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
547 |
|
0 | 548 |
## html widets ################################################################ |
549 |
||
550 |
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
|
551 |
|
0 | 552 |
def __init__(self, facet): |
553 |
self.facet = facet |
|
554 |
self.items = [] |
|
555 |
||
556 |
def append(self, item): |
|
557 |
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
|
558 |
|
0 | 559 |
def _render(self): |
560 |
title = html_escape(self.facet.title) |
|
561 |
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
|
562 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
0 | 563 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
564 |
(html_escape(facetid), title)) |
|
565 |
if self.facet.support_and(): |
|
566 |
_ = self.facet.req._ |
|
567 |
self.w(u'''<select name="%s" class="radio facetOperator" title="%s"> |
|
568 |
<option value="OR">%s</option> |
|
569 |
<option value="AND">%s</option> |
|
570 |
</select>''' % (facetid + '_andor', _('and/or between different values'), |
|
571 |
_('OR'), _('AND'))) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
572 |
cssclass = '' |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
573 |
if not self.facet.start_unfolded: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
574 |
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
|
575 |
if len(self.items) > 6: |
1132 | 576 |
cssclass += ' overflowed' |
0 | 577 |
self.w(u'<div class="facetBody%s">\n' % cssclass) |
578 |
for item in self.items: |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1933
diff
changeset
|
579 |
item.render(w=self.w) |
0 | 580 |
self.w(u'</div>\n') |
581 |
self.w(u'</div>\n') |
|
582 |
||
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
|
583 |
|
0 | 584 |
class FacetStringWidget(HTMLWidget): |
585 |
def __init__(self, facet): |
|
586 |
self.facet = facet |
|
587 |
self.value = None |
|
588 |
||
589 |
def _render(self): |
|
590 |
title = html_escape(self.facet.title) |
|
591 |
facetid = html_escape(self.facet.id) |
|
592 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
|
593 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
|
594 |
(facetid, title)) |
|
595 |
self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u'')) |
|
596 |
self.w(u'</div>\n') |
|
597 |
||
598 |
||
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
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
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
|
603 |
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
|
604 |
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
|
605 |
values: [%(minvalue)s, %(maxvalue)s], |
1933
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
606 |
stop: function(event, ui) { // submit when the user stops sliding |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
607 |
var form = $('#%(sliderid)s').closest('form'); |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
608 |
buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs'))); |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
609 |
}, |
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
|
610 |
slide: function(event, ui) { |
1933
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
611 |
$('#%(sliderid)s_inf').html(ui.values[0]); |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
612 |
$('#%(sliderid)s_sup').html(ui.values[1]); |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
613 |
$('input[name=%(facetid)s_inf]').val(ui.values[0]); |
f40ee76ecdf1
[facet][RangeFacet] submit facet form when the user stops sliding
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1896
diff
changeset
|
614 |
$('input[name=%(facetid)s_sup]').val(ui.values[1]); |
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
|
615 |
} |
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 |
''' |
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
|
618 |
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
|
619 |
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
|
620 |
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
|
621 |
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
|
622 |
|
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
623 |
def formatvalue(self, value): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
624 |
return value |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
625 |
|
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
|
626 |
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
|
627 |
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
|
628 |
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
|
629 |
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
|
630 |
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
|
631 |
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
|
632 |
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
|
633 |
'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
|
634 |
'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
|
635 |
'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
|
636 |
'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
|
637 |
}) |
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
|
638 |
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
|
639 |
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
|
640 |
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
|
641 |
(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
|
642 |
self.w(u'<span id="%s_inf">%s</span> - <span id="%s_sup">%s</span>' |
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
643 |
% (sliderid, self.formatvalue(self.minvalue), |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
644 |
sliderid, self.formatvalue(self.maxvalue))) |
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
|
645 |
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
|
646 |
% (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
|
647 |
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
|
648 |
% (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
|
649 |
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
|
650 |
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
|
651 |
|
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
|
652 |
|
2010
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
653 |
class DateFacetRangeWidget(FacetRangeWidget): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
654 |
onload = u''' |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
655 |
jQuery("#%(sliderid)s").slider({ |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
656 |
range: true, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
657 |
min: %(minvalue)s, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
658 |
max: %(maxvalue)s, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
659 |
values: [%(minvalue)s, %(maxvalue)s], |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
660 |
stop: function(event, ui) { // submit when the user stops sliding |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
661 |
UI = ui; |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
662 |
var form = $('#%(sliderid)s').closest('form'); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
663 |
buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs'))); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
664 |
}, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
665 |
slide: function(event, ui) { |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
666 |
$('#%(sliderid)s_inf').html( (new Date(ui.values[0])).strftime(DATE_FMT)); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
667 |
$('#%(sliderid)s_sup').html( (new Date(ui.values[1])).strftime(DATE_FMT)); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
668 |
$('input[name=%(facetid)s_inf]').val(ui.values[0]); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
669 |
$('input[name=%(facetid)s_sup]').val(ui.values[1]); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
670 |
} |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
671 |
}); |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
672 |
''' |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
673 |
def __init__(self, facet, minvalue, maxvalue): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
674 |
super(DateFacetRangeWidget, self).__init__(facet, |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
675 |
datetime2ticks(minvalue), |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
676 |
datetime2ticks(maxvalue)) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
677 |
def formatvalue(self, value): |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
678 |
datefmt = self.facet.req.property_value('ui.date-format') |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
679 |
return ustrftime(date.fromtimestamp(float(value) / 1000), datefmt) |
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
680 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
681 |
|
0 | 682 |
class FacetItem(HTMLWidget): |
683 |
||
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
|
684 |
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
|
685 |
unselected_img = "no-check-no-border.png" |
0 | 686 |
|
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
|
687 |
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
|
688 |
self.req = req |
0 | 689 |
self.label = label |
690 |
self.value = value |
|
691 |
self.selected = selected |
|
692 |
||
693 |
def _render(self): |
|
694 |
if self.selected: |
|
695 |
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
|
696 |
imgsrc = self.req.datadir_url + self.selected_img |
536 | 697 |
imgalt = self.req._('selected') |
0 | 698 |
else: |
699 |
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
|
700 |
imgsrc = self.req.datadir_url + self.unselected_img |
536 | 701 |
imgalt = self.req._('not selected') |
0 | 702 |
self.w(u'<div class="facetValue facetCheckBox%s" cubicweb:value="%s">\n' |
703 |
% (cssclass, html_escape(unicode(self.value)))) |
|
536 | 704 |
self.w(u'<img src="%s" alt="%s"/> ' % (imgsrc, imgalt)) |
0 | 705 |
self.w(u'<a href="javascript: {}">%s</a>' % html_escape(self.label)) |
706 |
self.w(u'</div>') |
|
707 |
||
708 |
||
709 |
class FacetSeparator(HTMLWidget): |
|
710 |
def __init__(self, label=None): |
|
711 |
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
|
712 |
|
0 | 713 |
def _render(self): |
714 |
pass |