author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 18 Aug 2010 13:58:12 +0200 | |
branch | stable |
changeset 6119 | b217635d3b28 |
parent 6038 | 5c5d819e8e93 |
child 6120 | c000e41316ec |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5225
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""contains utility functions and some visual component to restrict results of |
19 |
a search |
|
5768
1e73a466aa69
[fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5618
diff
changeset
|
20 |
""" |
0 | 21 |
|
22 |
__docformat__ = "restructuredtext en" |
|
23 |
||
24 |
from copy import deepcopy |
|
2152 | 25 |
from datetime import date, datetime, timedelta |
0 | 26 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
27 |
from logilab.mtconverter import xml_escape |
0 | 28 |
from logilab.common.graph import has_path |
29 |
from logilab.common.decorators import cached |
|
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4701
diff
changeset
|
30 |
from logilab.common.date import datetime2ticks |
0 | 31 |
from logilab.common.compat import all |
32 |
||
33 |
from rql import parse, nodes |
|
34 |
||
35 |
from cubicweb import Unauthorized, typed_eid |
|
2305
8f6dbe884700
import display_name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2152
diff
changeset
|
36 |
from cubicweb.schema import display_name |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4333
diff
changeset
|
37 |
from cubicweb.utils import make_uid |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
38 |
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
|
39 |
from cubicweb.appobject import AppObject |
0 | 40 |
from cubicweb.web.htmlwidgets import HTMLWidget |
41 |
||
42 |
## rqlst manipulation functions used by facets ################################ |
|
43 |
||
44 |
def prepare_facets_rqlst(rqlst, args=None): |
|
45 |
"""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
|
46 |
|
0 | 47 |
* remove ORDERBY clause |
48 |
* cleanup selection (remove everything) |
|
49 |
* undefine unnecessary variables |
|
50 |
* set DISTINCT |
|
51 |
* unset LIMIT/OFFSET |
|
52 |
""" |
|
53 |
if len(rqlst.children) > 1: |
|
54 |
raise NotImplementedError('FIXME: union not yet supported') |
|
55 |
select = rqlst.children[0] |
|
56 |
mainvar = filtered_variable(select) |
|
57 |
select.set_limit(None) |
|
58 |
select.set_offset(None) |
|
59 |
baserql = select.as_string(kwargs=args) |
|
60 |
# cleanup sort terms |
|
61 |
select.remove_sort_terms() |
|
62 |
# selection: only vocabulary entity |
|
63 |
for term in select.selection[:]: |
|
64 |
select.remove_selected(term) |
|
65 |
# remove unbound variables which only have some type restriction |
|
66 |
for dvar in select.defined_vars.values(): |
|
67 |
if not (dvar is mainvar or dvar.stinfo['relations']): |
|
68 |
select.undefine_variable(dvar) |
|
69 |
# global tree config: DISTINCT, LIMIT, OFFSET |
|
70 |
select.set_distinct(True) |
|
71 |
return mainvar, baserql |
|
72 |
||
73 |
def filtered_variable(rqlst): |
|
74 |
vref = rqlst.selection[0].iget_nodes(nodes.VariableRef).next() |
|
75 |
return vref.variable |
|
76 |
||
77 |
||
78 |
def get_facet(req, facetid, rqlst, mainvar): |
|
4333
7add8a73a168
[vreg] object_by_id should not have been deprecated. Fix facets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
79 |
return req.vreg['facets'].object_by_id(facetid, req, rqlst=rqlst, |
7add8a73a168
[vreg] object_by_id should not have been deprecated. Fix facets.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
80 |
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
|
81 |
|
0 | 82 |
|
83 |
def filter_hiddens(w, **kwargs): |
|
84 |
for key, val in kwargs.items(): |
|
85 |
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
|
86 |
key, xml_escape(val))) |
0 | 87 |
|
88 |
||
89 |
def _may_be_removed(rel, schema, mainvar): |
|
90 |
"""if the given relation may be removed from the tree, return the variable |
|
91 |
on the other side of `mainvar`, else return None |
|
92 |
Conditions: |
|
93 |
* the relation is an attribute selection of the main variable |
|
94 |
* the relation is optional relation linked to the main variable |
|
95 |
* the relation is a mandatory relation linked to the main variable |
|
96 |
without any restriction on the other variable |
|
97 |
""" |
|
98 |
lhs, rhs = rel.get_variable_parts() |
|
99 |
rschema = schema.rschema(rel.r_type) |
|
100 |
if lhs.variable is mainvar: |
|
101 |
try: |
|
102 |
ovar = rhs.variable |
|
103 |
except AttributeError: |
|
104 |
# constant restriction |
|
105 |
# XXX: X title LOWER(T) if it makes sense? |
|
106 |
return None |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3333
diff
changeset
|
107 |
if rschema.final: |
0 | 108 |
if len(ovar.stinfo['relations']) == 1: |
109 |
# attribute selection |
|
110 |
return ovar |
|
111 |
return None |
|
112 |
opt = 'right' |
|
113 |
cardidx = 0 |
|
114 |
elif getattr(rhs, 'variable', None) is mainvar: |
|
115 |
ovar = lhs.variable |
|
116 |
opt = 'left' |
|
117 |
cardidx = 1 |
|
118 |
else: |
|
119 |
# not directly linked to the main variable |
|
120 |
return None |
|
121 |
if rel.optional in (opt, 'both'): |
|
122 |
# optional relation |
|
123 |
return ovar |
|
4073
03681ba6da0b
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
124 |
if all(rdef.cardinality[cardidx] in '1+' |
03681ba6da0b
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
125 |
for rdef in rschema.rdefs.values()): |
0 | 126 |
# mandatory relation without any restriction on the other variable |
127 |
for orel in ovar.stinfo['relations']: |
|
128 |
if rel is orel: |
|
129 |
continue |
|
130 |
if _may_be_removed(orel, schema, ovar) is None: |
|
131 |
return None |
|
132 |
return ovar |
|
133 |
return None |
|
134 |
||
135 |
def _add_rtype_relation(rqlst, mainvar, rtype, role): |
|
1149 | 136 |
"""add a relation relying `mainvar` to entities linked by the `rtype` |
137 |
relation (where `mainvar` has `role`) |
|
138 |
||
139 |
return the inserted variable for linked entities. |
|
140 |
""" |
|
0 | 141 |
newvar = rqlst.make_variable() |
142 |
if role == 'object': |
|
1149 | 143 |
rqlst.add_relation(newvar, rtype, mainvar) |
0 | 144 |
else: |
1149 | 145 |
rqlst.add_relation(mainvar, rtype, newvar) |
146 |
return newvar |
|
0 | 147 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
148 |
def _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
149 |
select_target_entity=True): |
0 | 150 |
"""prepare a syntax tree to generate a filter vocabulary rql using the given |
151 |
relation: |
|
152 |
* create a variable to filter on this relation |
|
153 |
* add the relation |
|
154 |
* add the new variable to GROUPBY clause if necessary |
|
155 |
* add the new variable to the selection |
|
156 |
""" |
|
1149 | 157 |
newvar = _add_rtype_relation(rqlst, mainvar, rtype, role) |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
158 |
if select_target_entity: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
159 |
if rqlst.groupby: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
160 |
rqlst.add_group_var(newvar) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
161 |
rqlst.add_selected(newvar) |
5618
24cc5d495fdf
[facet] we must add type restriction for attribute facets as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5526
diff
changeset
|
162 |
# add is restriction if necessary |
24cc5d495fdf
[facet] we must add type restriction for attribute facets as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5526
diff
changeset
|
163 |
if mainvar.stinfo['typerel'] is None: |
24cc5d495fdf
[facet] we must add type restriction for attribute facets as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5526
diff
changeset
|
164 |
etypes = frozenset(sol[mainvar.name] for sol in rqlst.solutions) |
24cc5d495fdf
[facet] we must add type restriction for attribute facets as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5526
diff
changeset
|
165 |
rqlst.add_type_restriction(mainvar, etypes) |
1149 | 166 |
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
|
167 |
|
0 | 168 |
def _remove_relation(rqlst, rel, var): |
169 |
"""remove a constraint relation from the syntax tree""" |
|
170 |
# remove the relation |
|
171 |
rqlst.remove_node(rel) |
|
172 |
# remove relations where the filtered variable appears on the |
|
173 |
# lhs and rhs is a constant restriction |
|
174 |
extra = [] |
|
175 |
for vrel in var.stinfo['relations']: |
|
176 |
if vrel is rel: |
|
177 |
continue |
|
178 |
if vrel.children[0].variable is var: |
|
179 |
if not vrel.children[1].get_nodes(nodes.Constant): |
|
180 |
extra.append(vrel) |
|
181 |
rqlst.remove_node(vrel) |
|
182 |
return extra |
|
183 |
||
184 |
def _set_orderby(rqlst, newvar, sortasc, sortfuncname): |
|
185 |
if sortfuncname is None: |
|
186 |
rqlst.add_sort_var(newvar, sortasc) |
|
187 |
else: |
|
188 |
vref = nodes.variable_ref(newvar) |
|
189 |
vref.register_reference() |
|
190 |
sortfunc = nodes.Function(sortfuncname) |
|
191 |
sortfunc.append(vref) |
|
192 |
term = nodes.SortTerm(sortfunc, sortasc) |
|
193 |
rqlst.add_sort_term(term) |
|
194 |
||
195 |
def insert_attr_select_relation(rqlst, mainvar, rtype, role, attrname, |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
196 |
sortfuncname=None, sortasc=True, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
197 |
select_target_entity=True): |
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
198 |
"""modify a syntax tree to : |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
199 |
* link a new variable to `mainvar` through `rtype` (where mainvar has `role`) |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
200 |
* retrieve only the newly inserted variable and its `attrname` |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
201 |
|
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
202 |
Sorting: |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
203 |
* on `attrname` ascendant (`sortasc`=True) or descendant (`sortasc`=False) |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
204 |
* on `sortfuncname`(`attrname`) if `sortfuncname` is specified |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
205 |
* no sort if `sortasc` is None |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
206 |
""" |
0 | 207 |
_cleanup_rqlst(rqlst, mainvar) |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
208 |
var = _prepare_vocabulary_rqlst(rqlst, mainvar, rtype, role, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
209 |
select_target_entity) |
0 | 210 |
# not found, create one |
211 |
attrvar = rqlst.make_variable() |
|
1149 | 212 |
rqlst.add_relation(var, attrname, attrvar) |
0 | 213 |
# if query is grouped, we have to add the attribute variable |
214 |
if rqlst.groupby: |
|
215 |
if not attrvar in rqlst.groupby: |
|
216 |
rqlst.add_group_var(attrvar) |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
217 |
if sortasc is not None: |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
218 |
_set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
0 | 219 |
# add attribute variable to selection |
220 |
rqlst.add_selected(attrvar) |
|
221 |
return var |
|
222 |
||
223 |
def _cleanup_rqlst(rqlst, mainvar): |
|
224 |
"""cleanup tree from unnecessary restriction: |
|
225 |
* attribute selection |
|
226 |
* optional relations linked to the main variable |
|
227 |
* mandatory relations linked to the main variable |
|
228 |
""" |
|
229 |
if rqlst.where is None: |
|
230 |
return |
|
231 |
schema = rqlst.root.schema |
|
232 |
toremove = set() |
|
233 |
vargraph = deepcopy(rqlst.vargraph) # graph representing links between variable |
|
234 |
for rel in rqlst.where.get_nodes(nodes.Relation): |
|
235 |
ovar = _may_be_removed(rel, schema, mainvar) |
|
236 |
if ovar is not None: |
|
237 |
toremove.add(ovar) |
|
238 |
removed = set() |
|
239 |
while toremove: |
|
240 |
trvar = toremove.pop() |
|
241 |
trvarname = trvar.name |
|
242 |
# remove paths using this variable from the graph |
|
243 |
linkedvars = vargraph.pop(trvarname) |
|
244 |
for ovarname in linkedvars: |
|
245 |
vargraph[ovarname].remove(trvarname) |
|
246 |
# remove relation using this variable |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
247 |
for rel in trvar.stinfo['relations']: |
0 | 248 |
if rel in removed: |
249 |
# already removed |
|
250 |
continue |
|
251 |
rqlst.remove_node(rel) |
|
252 |
removed.add(rel) |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
253 |
rel = trvar.stinfo['typerel'] |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
254 |
if rel is not None and not rel in removed: |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
255 |
rqlst.remove_node(rel) |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
256 |
removed.add(rel) |
0 | 257 |
# cleanup groupby clause |
258 |
if rqlst.groupby: |
|
259 |
for vref in rqlst.groupby[:]: |
|
260 |
if vref.name == trvarname: |
|
261 |
rqlst.remove_group_var(vref) |
|
262 |
# we can also remove all variables which are linked to this variable |
|
263 |
# and have no path to the main variable |
|
264 |
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
|
265 |
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
|
266 |
continue |
0 | 267 |
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
|
268 |
toremove.add(rqlst.defined_vars[ovarname]) |
0 | 269 |
|
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
|
270 |
|
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
|
271 |
|
0 | 272 |
## 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
|
273 |
class AbstractFacet(AppObject): |
0 | 274 |
__abstract__ = True |
275 |
__registry__ = 'facets' |
|
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
276 |
cw_property_defs = { |
0 | 277 |
_('visible'): dict(type='Boolean', default=True, |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
278 |
help=_('display the facet or not')), |
0 | 279 |
_('order'): dict(type='Int', default=99, |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
280 |
help=_('display order of the facet')), |
496
e25a3c2f5393
set default to empty string, not None
sylvain.thenault@logilab.fr
parents:
467
diff
changeset
|
281 |
_('context'): dict(type='String', default='', |
0 | 282 |
# 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
|
283 |
vocabulary=(_('tablefilter'), _('facetbox'), ''), |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
284 |
help=_('context where this facet should be displayed, ' |
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
285 |
'leave empty for both')), |
0 | 286 |
} |
287 |
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
|
288 |
context = '' |
0 | 289 |
needs_update = False |
290 |
start_unfolded = True |
|
4701
3acd198a66c9
[facet] before 3.6 we were sure to have a cw_rset attribute. Restore this for facets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4615
diff
changeset
|
291 |
cw_rset = None # ensure facets have a cw_rset attribute |
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
|
292 |
|
4614
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
293 |
def __init__(self, req, rqlst=None, filtered_variable=None, |
2658
5535857eeaa5
[appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
294 |
**kwargs): |
4614
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
295 |
super(AbstractFacet, self).__init__(req, **kwargs) |
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
296 |
assert rqlst is not None |
0 | 297 |
assert filtered_variable |
4614
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
298 |
# take care: facet may be retreived using `object_by_id` from an ajax call |
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
299 |
# or from `select` using the result set to filter |
95ccd84c892b
fix the bad rqlst syntax tree usage during facets initialization by copying it
Charles Hébert <charles.hebert@logilab.fr>
parents:
4546
diff
changeset
|
300 |
self.rqlst = rqlst |
2658
5535857eeaa5
[appobject selection process] drop the need for the .selected method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
301 |
self.filtered_variable = filtered_variable |
0 | 302 |
|
303 |
@property |
|
304 |
def operator(self): |
|
305 |
# OR between selected values by default |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
306 |
return self._cw.form.get(self.__regid__ + '_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
|
307 |
|
0 | 308 |
def get_widget(self): |
309 |
"""return the widget instance to use to display this facet |
|
310 |
""" |
|
311 |
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
|
312 |
|
0 | 313 |
def add_rql_restrictions(self): |
314 |
"""add restriction for this facet into the rql syntax tree""" |
|
315 |
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
|
316 |
|
0 | 317 |
|
318 |
class VocabularyFacet(AbstractFacet): |
|
319 |
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
|
320 |
|
0 | 321 |
def get_widget(self): |
322 |
"""return the widget instance to use to display this facet |
|
323 |
||
324 |
default implentation expects a .vocabulary method on the facet and |
|
325 |
return a combobox displaying this vocabulary |
|
326 |
""" |
|
327 |
vocab = self.vocabulary() |
|
328 |
if len(vocab) <= 1: |
|
329 |
return None |
|
330 |
wdg = FacetVocabularyWidget(self) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
331 |
selected = frozenset(typed_eid(eid) for eid in self._cw.list_form_param(self.__regid__)) |
0 | 332 |
for label, value in vocab: |
333 |
if value is None: |
|
334 |
wdg.append(FacetSeparator(label)) |
|
335 |
else: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
336 |
wdg.append(FacetItem(self._cw, label, value, value in selected)) |
0 | 337 |
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
|
338 |
|
0 | 339 |
def vocabulary(self): |
340 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
341 |
""" |
|
342 |
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
|
343 |
|
0 | 344 |
def possible_values(self): |
345 |
"""return a list of possible values (as string since it's used to |
|
346 |
compare to a form value in javascript) for this facet |
|
347 |
""" |
|
348 |
raise NotImplementedError |
|
349 |
||
350 |
def support_and(self): |
|
351 |
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
|
352 |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
353 |
def rqlexec(self, rql, args=None): |
0 | 354 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
355 |
return self._cw.execute(rql, args) |
0 | 356 |
except Unauthorized: |
357 |
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
|
358 |
|
0 | 359 |
|
360 |
class RelationFacet(VocabularyFacet): |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
361 |
"""Base facet to filter some entities according to other entities to which |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
362 |
they are related. Create concret facet by inheriting from this class an then |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
363 |
configuring it by setting class attribute described below. |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
364 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
365 |
The relation is defined by the `rtype` and `role` attributes. |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
366 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
367 |
The values displayed for related entities will be: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
368 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
369 |
* result of calling their `label_vid` view if specified |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
370 |
* else their `target_attr` attribute value if specified |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
371 |
* else their eid (you usually want something nicer...) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
372 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
373 |
When no `label_vid` is set, you will get translated value if `i18nable` is |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
374 |
set. |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
375 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
376 |
You can filter out target entity types by specifying `target_type` |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
377 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
378 |
By default, vocabulary will be displayed sorted on `target_attr` value in an |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
379 |
ascending way. You can control sorting with: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
380 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
381 |
* `sortfunc`: set this to a stored procedure name if you want to sort on the |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
382 |
result of this function's result instead of direct value |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
383 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
384 |
* `sortasc`: boolean flag to control ascendant/descendant sorting |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
385 |
""" |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
386 |
__select__ = partial_relation_possible() & match_context_prop() |
5006
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
387 |
# class attributes to configure the relation facet |
0 | 388 |
rtype = None |
1433 | 389 |
role = 'subject' |
0 | 390 |
target_attr = 'eid' |
5006
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
391 |
target_type = None |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
392 |
# should value be internationalized (XXX could be guessed from the schema) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
393 |
i18nable = True |
0 | 394 |
# set this to a stored procedure name if you want to sort on the result of |
395 |
# this function's result instead of direct value |
|
396 |
sortfunc = None |
|
397 |
# ascendant/descendant sorting |
|
398 |
sortasc = True |
|
3328
76888cd23d83
facets enhancement: can now set label_vid to use a view instead of a simple attribute as label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
399 |
# if you want to call a view on the entity instead of using `target_attr` |
76888cd23d83
facets enhancement: can now set label_vid to use a view instead of a simple attribute as label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
400 |
label_vid = None |
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
|
401 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
402 |
# internal purpose |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
403 |
_select_target_entity = True |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
404 |
|
0 | 405 |
@property |
406 |
def title(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
407 |
return display_name(self._cw, self.rtype, form=self.role) |
0 | 408 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
409 |
def rql_sort(self): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
410 |
"""return true if we can handle sorting in the rql query. E.g. if |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
411 |
sortfunc is set or if we have not to transform the returned value (eg no |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
412 |
label_vid and not i18nable) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
413 |
""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
414 |
return self.sortfunc is not None or (self.label_vid is None |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
415 |
and not self.i18nable) |
0 | 416 |
def vocabulary(self): |
417 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
418 |
""" |
|
419 |
rqlst = self.rqlst |
|
420 |
rqlst.save_state() |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
421 |
if self.rql_sort: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
422 |
sort = self.sortasc |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
423 |
else: |
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
424 |
sort = None # will be sorted on label |
0 | 425 |
try: |
426 |
mainvar = self.filtered_variable |
|
5006
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
427 |
var = insert_attr_select_relation( |
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
428 |
rqlst, mainvar, self.rtype, self.role, self.target_attr, |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
429 |
self.sortfunc, sort, self._select_target_entity) |
5006
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
430 |
if self.target_type is not None: |
871269e5e020
[facet] allow to specify a target type on relation facets for case such as Keyword/CodeKeyword where we only targets Keyword
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
431 |
rqlst.add_type_restriction(var, self.target_type) |
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
|
432 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
433 |
rset = self.rqlexec(rqlst.as_string(), self.cw_rset.args) |
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
|
434 |
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
|
435 |
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
|
436 |
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
|
437 |
return () |
0 | 438 |
finally: |
439 |
rqlst.recover() |
|
5225
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
440 |
# don't call rset_vocabulary on empty result set, it may be an empty |
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
441 |
# *list* (see rqlexec implementation) |
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
442 |
return rset and 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
|
443 |
|
0 | 444 |
def possible_values(self): |
445 |
"""return a list of possible values (as string since it's used to |
|
446 |
compare to a form value in javascript) for this facet |
|
447 |
""" |
|
448 |
rqlst = self.rqlst |
|
449 |
rqlst.save_state() |
|
450 |
try: |
|
451 |
_cleanup_rqlst(rqlst, self.filtered_variable) |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
452 |
if self._select_target_entity: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
453 |
_prepare_vocabulary_rqlst(rqlst, self.filtered_variable, self.rtype, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
454 |
self.role, select_target_entity=True) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
455 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
456 |
insert_attr_select_relation( |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
457 |
rqlst, self.filtered_variable, self.rtype, self.role, self.target_attr, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
458 |
select_target_entity=False) |
0 | 459 |
return [str(x) for x, in self.rqlexec(rqlst.as_string())] |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
460 |
except: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
461 |
import traceback |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
462 |
traceback.print_exc() |
0 | 463 |
finally: |
464 |
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
|
465 |
|
0 | 466 |
def rset_vocabulary(self, rset): |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
467 |
if self.i18nable: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
468 |
_ = self._cw._ |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
469 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
470 |
_ = unicode |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
471 |
if self.rql_sort: |
3328
76888cd23d83
facets enhancement: can now set label_vid to use a view instead of a simple attribute as label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2996
diff
changeset
|
472 |
return [(_(label), eid) for eid, label in rset] |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
473 |
if self.label_vid is None: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
474 |
assert self.i18nable |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
475 |
values = [(_(label), eid) for eid, label in rset] |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
476 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
477 |
values = [(entity.view(self.label_vid), entity.eid) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
478 |
for entity in rset.entities()] |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
479 |
values = sorted(values) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
480 |
if self.sortasc: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
481 |
return values |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
482 |
return reversed(values) |
0 | 483 |
|
484 |
@cached |
|
485 |
def support_and(self): |
|
3456
1a63a252601f
[facets] typo fix
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
486 |
rschema = self._cw.vreg.schema.rschema(self.rtype) |
0 | 487 |
# XXX when called via ajax, no rset to compute possible types |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
488 |
possibletypes = self.cw_rset and self.cw_rset.column_types(0) |
4087 | 489 |
for rdef in rschema.rdefs.itervalues(): |
0 | 490 |
if possibletypes is not None: |
491 |
if self.role == 'subject': |
|
4087 | 492 |
if not rdef.subject in possibletypes: |
0 | 493 |
continue |
4087 | 494 |
elif not rdef.object in possibletypes: |
0 | 495 |
continue |
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4087
diff
changeset
|
496 |
if rdef.role_cardinality(self.role) in '+*': |
0 | 497 |
return True |
498 |
return False |
|
499 |
||
500 |
def add_rql_restrictions(self): |
|
501 |
"""add restriction for this facet into the rql syntax tree""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
502 |
value = self._cw.form.get(self.__regid__) |
0 | 503 |
if not value: |
504 |
return |
|
505 |
mainvar = self.filtered_variable |
|
1149 | 506 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 507 |
if isinstance(value, basestring): |
508 |
# only one value selected |
|
509 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
510 |
elif self.operator == 'OR': |
|
511 |
# set_distinct only if rtype cardinality is > 1 |
|
512 |
if self.support_and(): |
|
513 |
self.rqlst.set_distinct(True) |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
514 |
# multiple ORed values: using IN is fine |
0 | 515 |
self.rqlst.add_eid_restriction(restrvar, value) |
516 |
else: |
|
517 |
# multiple values with AND operator |
|
518 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
519 |
while value: |
|
1149 | 520 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 521 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
522 |
||
523 |
||
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
524 |
class RelationAttributeFacet(RelationFacet): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
525 |
"""Base facet to filter some entities according to an attribute of other |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
526 |
entities to which they are related. Most things work similarly as |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
527 |
:class:`RelationFacet`, except that: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
528 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
529 |
* `label_vid` doesn't make sense here |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
530 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
531 |
* you should specify the attribute type using `attrtype` if it's not a |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
532 |
String |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
533 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
534 |
* you can specify a comparison operator using `comparator` |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
535 |
""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
536 |
_select_target_entity = False |
0 | 537 |
# attribute type |
538 |
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
|
539 |
# 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
|
540 |
comparator = '=' # could be '<', '<=', '>', '>=' |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
541 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
542 |
def rset_vocabulary(self, rset): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
543 |
if self.i18nable: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
544 |
_ = self._cw._ |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
545 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
546 |
_ = unicode |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
547 |
if self.rql_sort: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
548 |
return [(_(value), value) for value, in rset] |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
549 |
values = [(_(value), value) for value, in rset] |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
550 |
if self.sortasc: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
551 |
return sorted(values) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
552 |
return reversed(sorted(values)) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
553 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
554 |
def add_rql_restrictions(self): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
555 |
"""add restriction for this facet into the rql syntax tree""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
556 |
value = self._cw.form.get(self.__regid__) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
557 |
if not value: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
558 |
return |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
559 |
mainvar = self.filtered_variable |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
560 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
561 |
self.rqlst.set_distinct(True) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
562 |
if isinstance(value, basestring) or self.operator == 'OR': |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
563 |
# only one value selected or multiple ORed values: using IN is fine |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
564 |
self.rqlst.add_constant_restriction(restrvar, self.target_attr, value, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
565 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
566 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
567 |
# multiple values with AND operator |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
568 |
self.rqlst.add_constant_restriction(restrvar, self.target_attr, value.pop(), |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
569 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
570 |
while value: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
571 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
572 |
self.rqlst.add_constant_restriction(restrvar, self.target_attr, value.pop(), |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
573 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
574 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
575 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
576 |
class AttributeFacet(RelationAttributeFacet): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
577 |
"""Base facet to filter some entities according one of their attribute. |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
578 |
Configuration is mostly similarly as :class:`RelationAttributeFacet`, except that: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
579 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
580 |
* `target_attr` doesn't make sense here (you specify the attribute using `rtype` |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
581 |
* `role` neither, it's systematically 'subject' |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
582 |
""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
583 |
_select_target_entity = 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
|
584 |
|
0 | 585 |
def vocabulary(self): |
586 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
587 |
""" |
|
588 |
rqlst = self.rqlst |
|
589 |
rqlst.save_state() |
|
590 |
try: |
|
591 |
mainvar = self.filtered_variable |
|
592 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 593 |
newvar = _prepare_vocabulary_rqlst(rqlst, mainvar, self.rtype, self.role) |
0 | 594 |
_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
|
595 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
596 |
rset = self.rqlexec(rqlst.as_string(), self.cw_rset.args) |
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
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
return () |
0 | 601 |
finally: |
602 |
rqlst.recover() |
|
5225
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
603 |
# don't call rset_vocabulary on empty result set, it may be an empty |
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
604 |
# *list* (see rqlexec implementation) |
9ff0dee81eb2
[facet] don't crash in rset_vocabulary if an unauthorized error was raised in rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5006
diff
changeset
|
605 |
return rset and 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
|
606 |
|
0 | 607 |
def support_and(self): |
608 |
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
|
609 |
|
0 | 610 |
def add_rql_restrictions(self): |
611 |
"""add restriction for this facet into the rql syntax tree""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
612 |
value = self._cw.form.get(self.__regid__) |
0 | 613 |
if not value: |
614 |
return |
|
615 |
mainvar = self.filtered_variable |
|
616 |
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
|
617 |
self.attrtype, self.comparator) |
0 | 618 |
|
619 |
||
620 |
class FilterRQLBuilder(object): |
|
621 |
"""called by javascript to get a rql string from filter form""" |
|
622 |
||
623 |
def __init__(self, req): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
624 |
self._cw = 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
|
625 |
|
0 | 626 |
def build_rql(self):#, tablefilter=False): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
627 |
form = self._cw.form |
0 | 628 |
facetids = form['facets'].split(',') |
5773
aae03cd19151
[facet] use vreg.parse so the rql syntax tree is properly annotated as well
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
5618
diff
changeset
|
629 |
select = self._cw.vreg.parse(self._cw, form['baserql']).children[0] # XXX Union unsupported yet |
0 | 630 |
mainvar = filtered_variable(select) |
631 |
toupdate = [] |
|
632 |
for facetid in facetids: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
633 |
facet = get_facet(self._cw, facetid, select, mainvar) |
0 | 634 |
facet.add_rql_restrictions() |
635 |
if facet.needs_update: |
|
636 |
toupdate.append(facetid) |
|
637 |
return select.as_string(), toupdate |
|
638 |
||
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
|
639 |
|
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
|
640 |
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
|
641 |
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
|
642 |
|
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 |
@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
|
644 |
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
|
645 |
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
|
646 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
647 |
def get_widget(self): |
4546
f8ac61376b2b
provides a fallback_on_none_attribute field attribute, allowing to specify default value for attributes of *existing* entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
648 |
"""return the widget instance to use to display this facet""" |
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
|
649 |
values = set(value for _, value in self.vocabulary() if value is not None) |
4615
d69d7ccbe046
Fix: RangeFacet is selected (valid rset) but without any values
Charles Hébert <charles.hebert@logilab.fr>
parents:
4614
diff
changeset
|
650 |
# Rset with entities (the facet is selected) but without values |
d69d7ccbe046
Fix: RangeFacet is selected (valid rset) but without any values
Charles Hébert <charles.hebert@logilab.fr>
parents:
4614
diff
changeset
|
651 |
if len(values) == 0: |
d69d7ccbe046
Fix: RangeFacet is selected (valid rset) but without any values
Charles Hébert <charles.hebert@logilab.fr>
parents:
4614
diff
changeset
|
652 |
return None |
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 |
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
|
654 |
|
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 |
def infvalue(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
656 |
return self._cw.form.get('%s_inf' % self.__regid__) |
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
|
657 |
|
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 |
def supvalue(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
659 |
return self._cw.form.get('%s_sup' % self.__regid__) |
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
|
660 |
|
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 |
def formatvalue(self, value): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
662 |
"""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
|
663 |
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
|
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 |
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
|
666 |
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
|
667 |
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
|
668 |
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
|
669 |
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
|
670 |
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
|
671 |
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
|
672 |
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
|
673 |
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
|
674 |
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
|
675 |
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
|
676 |
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
|
677 |
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
|
678 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
679 |
|
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
|
680 |
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
|
681 |
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
|
682 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
683 |
@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
|
684 |
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
|
685 |
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
|
686 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
687 |
def formatvalue(self, value): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
688 |
"""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
|
689 |
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
|
690 |
|
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
691 |
|
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
692 |
class HasRelationFacet(AbstractFacet): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
693 |
rtype = None # override me in subclass |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
694 |
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
|
695 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
696 |
@property |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
697 |
def title(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
698 |
return display_name(self._cw, self.rtype, self.role) |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
699 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
700 |
def support_and(self): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
701 |
return False |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
702 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
703 |
def get_widget(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
704 |
return CheckBoxFacetWidget(self._cw, self, |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
705 |
'%s:%s' % (self.rtype, self), |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
706 |
self._cw.form.get(self.__regid__)) |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
707 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
708 |
def add_rql_restrictions(self): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
709 |
"""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
|
710 |
self.rqlst.set_distinct(True) # XXX |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
711 |
value = self._cw.form.get(self.__regid__) |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
712 |
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
|
713 |
return |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
714 |
var = self.rqlst.make_variable() |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
715 |
if self.role == 'subject': |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
716 |
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
|
717 |
else: |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
718 |
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
|
719 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
720 |
|
0 | 721 |
## html widets ################################################################ |
722 |
||
723 |
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
|
724 |
|
0 | 725 |
def __init__(self, facet): |
726 |
self.facet = facet |
|
727 |
self.items = [] |
|
728 |
||
729 |
def append(self, item): |
|
730 |
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
|
731 |
|
0 | 732 |
def _render(self): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
733 |
title = xml_escape(self.facet.title) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
734 |
facetid = xml_escape(self.facet.__regid__) |
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
|
735 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
0 | 736 |
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
|
737 |
(xml_escape(facetid), title)) |
0 | 738 |
if self.facet.support_and(): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
739 |
_ = self.facet._cw._ |
0 | 740 |
self.w(u'''<select name="%s" class="radio facetOperator" title="%s"> |
741 |
<option value="OR">%s</option> |
|
742 |
<option value="AND">%s</option> |
|
743 |
</select>''' % (facetid + '_andor', _('and/or between different values'), |
|
744 |
_('OR'), _('AND'))) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
745 |
cssclass = '' |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
746 |
if not self.facet.start_unfolded: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
747 |
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
|
748 |
if len(self.items) > 6: |
1132 | 749 |
cssclass += ' overflowed' |
0 | 750 |
self.w(u'<div class="facetBody%s">\n' % cssclass) |
751 |
for item in self.items: |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1933
diff
changeset
|
752 |
item.render(w=self.w) |
0 | 753 |
self.w(u'</div>\n') |
754 |
self.w(u'</div>\n') |
|
755 |
||
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
|
756 |
|
0 | 757 |
class FacetStringWidget(HTMLWidget): |
758 |
def __init__(self, facet): |
|
759 |
self.facet = facet |
|
760 |
self.value = None |
|
761 |
||
762 |
def _render(self): |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
763 |
title = xml_escape(self.facet.title) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
764 |
facetid = xml_escape(self.facet.__regid__) |
0 | 765 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
766 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
|
767 |
(facetid, title)) |
|
768 |
self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u'')) |
|
769 |
self.w(u'</div>\n') |
|
770 |
||
771 |
||
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
|
772 |
class FacetRangeWidget(HTMLWidget): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
773 |
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
|
774 |
onload = u''' |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
775 |
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
|
776 |
jQuery("#%(sliderid)s").slider({ |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
777 |
range: true, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
778 |
min: %(minvalue)s, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
779 |
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
|
780 |
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
|
781 |
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
|
782 |
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
|
783 |
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
|
784 |
}, |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
785 |
slide: function(event, ui) { |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
786 |
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
|
787 |
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
|
788 |
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
|
789 |
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
|
790 |
} |
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
|
791 |
}); |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
792 |
// 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
|
793 |
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
|
794 |
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
|
795 |
''' |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
796 |
#'# 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
|
797 |
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
|
798 |
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
|
799 |
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
|
800 |
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
|
801 |
|
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
|
802 |
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
|
803 |
facet = self.facet |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
804 |
facet._cw.add_js('ui.slider.js') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
805 |
facet._cw.add_css('ui.all.css') |
3333
c61a526b530e
[widgets] change make_uid() seed to make slider ids xhtml-valid
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3329
diff
changeset
|
806 |
sliderid = make_uid('theslider') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
807 |
facetid = xml_escape(self.facet.__regid__) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
808 |
facet._cw.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
|
809 |
'sliderid': sliderid, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
810 |
'facetid': facetid, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
811 |
'minvalue': self.minvalue, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
812 |
'maxvalue': self.maxvalue, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
813 |
'formatter': self.formatter, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
814 |
}) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
815 |
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
|
816 |
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
|
817 |
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
|
818 |
(facetid, title)) |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
819 |
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
|
820 |
% (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
|
821 |
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
|
822 |
% (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
|
823 |
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
|
824 |
% (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
|
825 |
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
|
826 |
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
|
827 |
|
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
|
828 |
|
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
|
829 |
class DateFacetRangeWidget(FacetRangeWidget): |
2149
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
830 |
|
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
831 |
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
|
832 |
|
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
833 |
def round_max_value(self, d): |
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
834 |
'round to upper value to avoid filtering out the max value' |
2152 | 835 |
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
|
836 |
|
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
|
837 |
def __init__(self, facet, minvalue, maxvalue): |
2149
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
838 |
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
|
839 |
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
|
840 |
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
|
841 |
datetime2ticks(maxvalue)) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
842 |
fmt = facet._cw.property_value('ui.date-format') |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
843 |
facet._cw.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
|
844 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
845 |
|
0 | 846 |
class FacetItem(HTMLWidget): |
847 |
||
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
|
848 |
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
|
849 |
unselected_img = "no-check-no-border.png" |
0 | 850 |
|
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
|
851 |
def __init__(self, req, label, value, selected=False): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
852 |
self._cw = req |
0 | 853 |
self.label = label |
854 |
self.value = value |
|
855 |
self.selected = selected |
|
856 |
||
857 |
def _render(self): |
|
858 |
if self.selected: |
|
859 |
cssclass = ' facetValueSelected' |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
860 |
imgsrc = self._cw.datadir_url + self.selected_img |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
861 |
imgalt = self._cw._('selected') |
0 | 862 |
else: |
863 |
cssclass = '' |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
864 |
imgsrc = self._cw.datadir_url + self.unselected_img |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
865 |
imgalt = self._cw._('not selected') |
0 | 866 |
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
|
867 |
% (cssclass, xml_escape(unicode(self.value)))) |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2658
diff
changeset
|
868 |
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
|
869 |
self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label)) |
0 | 870 |
self.w(u'</div>') |
871 |
||
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
872 |
|
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
873 |
class CheckBoxFacetWidget(HTMLWidget): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
874 |
selected_img = "black-check.png" |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
875 |
unselected_img = "black-uncheck.png" |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
876 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
877 |
def __init__(self, req, facet, value, selected): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
878 |
self._cw = req |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
879 |
self.facet = facet |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
880 |
self.value = value |
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
881 |
self.selected = selected |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
882 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
883 |
def _render(self): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
884 |
title = xml_escape(self.facet.title) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
885 |
facetid = xml_escape(self.facet.__regid__) |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
886 |
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
|
887 |
if self.selected: |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
888 |
cssclass = ' facetValueSelected' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
889 |
imgsrc = self._cw.datadir_url + self.selected_img |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
890 |
imgalt = self._cw._('selected') |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
891 |
else: |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
892 |
cssclass = '' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
893 |
imgsrc = self._cw.datadir_url + self.unselected_img |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
894 |
imgalt = self._cw._('not selected') |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
895 |
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
|
896 |
% (cssclass, xml_escape(unicode(self.value)))) |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
897 |
self.w(u'<div class="facetCheckBoxWidget">') |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2658
diff
changeset
|
898 |
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
|
899 |
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
|
900 |
self.w(u'</div>\n') |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
901 |
self.w(u'</div>\n') |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
902 |
self.w(u'</div>\n') |
0 | 903 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
904 |
|
0 | 905 |
class FacetSeparator(HTMLWidget): |
906 |
def __init__(self, label=None): |
|
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2658
diff
changeset
|
907 |
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
|
908 |
|
0 | 909 |
def _render(self): |
910 |
pass |