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