author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Sep 2010 12:53:56 +0200 | |
branch | stable |
changeset 6335 | d13d488a453e |
parent 6251 | afc757568492 |
child 6336 | ae80aed607f2 |
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/>. |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
18 |
""" |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
19 |
The :mod:`cubicweb.web.facet` module contains a set of abstract classes to use |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
20 |
as bases to build your own facets |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
21 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
22 |
All facet classes inherits from the :class:`AbstractFacet` class, though you'll |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
23 |
usually find some more handy class that do what you want. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
24 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
25 |
Let's see available classes. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
26 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
27 |
Classes you'll want to use |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
28 |
-------------------------- |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
29 |
.. autoclass:: cubicweb.web.facet.RelationFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
30 |
.. autoclass:: cubicweb.web.facet.RelationAttributeFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
31 |
.. autoclass:: cubicweb.web.facet.HasRelationFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
32 |
.. autoclass:: cubicweb.web.facet.AttributeFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
33 |
.. autoclass:: cubicweb.web.facet.RangeFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
34 |
.. autoclass:: cubicweb.web.facet.DateRangeFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
35 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
36 |
Classes for facets implementor |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
37 |
------------------------------ |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
38 |
Unless you didn't find the class that does the job you want above, you may want |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
39 |
to skip those classes... |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
40 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
41 |
.. autoclass:: cubicweb.web.facet.AbstractFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
42 |
.. autoclass:: cubicweb.web.facet.VocabularyFacet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
43 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
44 |
.. comment: XXX widgets |
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
|
45 |
""" |
0 | 46 |
|
47 |
__docformat__ = "restructuredtext en" |
|
48 |
||
49 |
from copy import deepcopy |
|
2152 | 50 |
from datetime import date, datetime, timedelta |
0 | 51 |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
52 |
from logilab.mtconverter import xml_escape |
0 | 53 |
from logilab.common.graph import has_path |
54 |
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
|
55 |
from logilab.common.date import datetime2ticks |
0 | 56 |
from logilab.common.compat import all |
57 |
||
58 |
from rql import parse, nodes |
|
59 |
||
60 |
from cubicweb import Unauthorized, typed_eid |
|
2305
8f6dbe884700
import display_name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2152
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
from cubicweb.appobject import AppObject |
0 | 65 |
from cubicweb.web.htmlwidgets import HTMLWidget |
66 |
||
67 |
## rqlst manipulation functions used by facets ################################ |
|
68 |
||
69 |
def prepare_facets_rqlst(rqlst, args=None): |
|
70 |
"""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
|
71 |
|
0 | 72 |
* remove ORDERBY clause |
73 |
* cleanup selection (remove everything) |
|
74 |
* undefine unnecessary variables |
|
75 |
* set DISTINCT |
|
76 |
* unset LIMIT/OFFSET |
|
77 |
""" |
|
78 |
if len(rqlst.children) > 1: |
|
79 |
raise NotImplementedError('FIXME: union not yet supported') |
|
80 |
select = rqlst.children[0] |
|
81 |
mainvar = filtered_variable(select) |
|
82 |
select.set_limit(None) |
|
83 |
select.set_offset(None) |
|
84 |
baserql = select.as_string(kwargs=args) |
|
85 |
# cleanup sort terms |
|
86 |
select.remove_sort_terms() |
|
87 |
# selection: only vocabulary entity |
|
88 |
for term in select.selection[:]: |
|
89 |
select.remove_selected(term) |
|
90 |
# remove unbound variables which only have some type restriction |
|
91 |
for dvar in select.defined_vars.values(): |
|
92 |
if not (dvar is mainvar or dvar.stinfo['relations']): |
|
93 |
select.undefine_variable(dvar) |
|
94 |
# global tree config: DISTINCT, LIMIT, OFFSET |
|
95 |
select.set_distinct(True) |
|
96 |
return mainvar, baserql |
|
97 |
||
98 |
def filtered_variable(rqlst): |
|
99 |
vref = rqlst.selection[0].iget_nodes(nodes.VariableRef).next() |
|
100 |
return vref.variable |
|
101 |
||
102 |
||
103 |
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
|
104 |
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
|
105 |
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
|
106 |
|
0 | 107 |
|
108 |
def filter_hiddens(w, **kwargs): |
|
109 |
for key, val in kwargs.items(): |
|
110 |
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
|
111 |
key, xml_escape(val))) |
0 | 112 |
|
113 |
||
114 |
def _may_be_removed(rel, schema, mainvar): |
|
115 |
"""if the given relation may be removed from the tree, return the variable |
|
116 |
on the other side of `mainvar`, else return None |
|
117 |
Conditions: |
|
118 |
* the relation is an attribute selection of the main variable |
|
119 |
* the relation is optional relation linked to the main variable |
|
120 |
* the relation is a mandatory relation linked to the main variable |
|
121 |
without any restriction on the other variable |
|
122 |
""" |
|
123 |
lhs, rhs = rel.get_variable_parts() |
|
124 |
rschema = schema.rschema(rel.r_type) |
|
125 |
if lhs.variable is mainvar: |
|
126 |
try: |
|
127 |
ovar = rhs.variable |
|
128 |
except AttributeError: |
|
129 |
# constant restriction |
|
130 |
# XXX: X title LOWER(T) if it makes sense? |
|
131 |
return None |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3333
diff
changeset
|
132 |
if rschema.final: |
0 | 133 |
if len(ovar.stinfo['relations']) == 1: |
134 |
# attribute selection |
|
135 |
return ovar |
|
136 |
return None |
|
137 |
opt = 'right' |
|
138 |
cardidx = 0 |
|
139 |
elif getattr(rhs, 'variable', None) is mainvar: |
|
140 |
ovar = lhs.variable |
|
141 |
opt = 'left' |
|
142 |
cardidx = 1 |
|
143 |
else: |
|
144 |
# not directly linked to the main variable |
|
145 |
return None |
|
146 |
if rel.optional in (opt, 'both'): |
|
147 |
# optional relation |
|
148 |
return ovar |
|
4073
03681ba6da0b
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
149 |
if all(rdef.cardinality[cardidx] in '1+' |
03681ba6da0b
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3720
diff
changeset
|
150 |
for rdef in rschema.rdefs.values()): |
0 | 151 |
# mandatory relation without any restriction on the other variable |
152 |
for orel in ovar.stinfo['relations']: |
|
153 |
if rel is orel: |
|
154 |
continue |
|
155 |
if _may_be_removed(orel, schema, ovar) is None: |
|
156 |
return None |
|
157 |
return ovar |
|
158 |
return None |
|
159 |
||
160 |
def _add_rtype_relation(rqlst, mainvar, rtype, role): |
|
1149 | 161 |
"""add a relation relying `mainvar` to entities linked by the `rtype` |
162 |
relation (where `mainvar` has `role`) |
|
163 |
||
164 |
return the inserted variable for linked entities. |
|
165 |
""" |
|
0 | 166 |
newvar = rqlst.make_variable() |
167 |
if role == 'object': |
|
1149 | 168 |
rqlst.add_relation(newvar, rtype, mainvar) |
0 | 169 |
else: |
1149 | 170 |
rqlst.add_relation(mainvar, rtype, newvar) |
171 |
return newvar |
|
0 | 172 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
173 |
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
|
174 |
select_target_entity=True): |
0 | 175 |
"""prepare a syntax tree to generate a filter vocabulary rql using the given |
176 |
relation: |
|
177 |
* create a variable to filter on this relation |
|
178 |
* add the relation |
|
179 |
* add the new variable to GROUPBY clause if necessary |
|
180 |
* add the new variable to the selection |
|
181 |
""" |
|
1149 | 182 |
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
|
183 |
if select_target_entity: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
184 |
if rqlst.groupby: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
185 |
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
|
186 |
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
|
187 |
# 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
|
188 |
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
|
189 |
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
|
190 |
rqlst.add_type_restriction(mainvar, etypes) |
1149 | 191 |
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
|
192 |
|
0 | 193 |
def _remove_relation(rqlst, rel, var): |
194 |
"""remove a constraint relation from the syntax tree""" |
|
195 |
# remove the relation |
|
196 |
rqlst.remove_node(rel) |
|
197 |
# remove relations where the filtered variable appears on the |
|
198 |
# lhs and rhs is a constant restriction |
|
199 |
extra = [] |
|
200 |
for vrel in var.stinfo['relations']: |
|
201 |
if vrel is rel: |
|
202 |
continue |
|
203 |
if vrel.children[0].variable is var: |
|
204 |
if not vrel.children[1].get_nodes(nodes.Constant): |
|
205 |
extra.append(vrel) |
|
206 |
rqlst.remove_node(vrel) |
|
207 |
return extra |
|
208 |
||
209 |
def _set_orderby(rqlst, newvar, sortasc, sortfuncname): |
|
210 |
if sortfuncname is None: |
|
211 |
rqlst.add_sort_var(newvar, sortasc) |
|
212 |
else: |
|
213 |
vref = nodes.variable_ref(newvar) |
|
214 |
vref.register_reference() |
|
215 |
sortfunc = nodes.Function(sortfuncname) |
|
216 |
sortfunc.append(vref) |
|
217 |
term = nodes.SortTerm(sortfunc, sortasc) |
|
218 |
rqlst.add_sort_term(term) |
|
219 |
||
220 |
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
|
221 |
sortfuncname=None, sortasc=True, |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
222 |
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
|
223 |
"""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
|
224 |
* 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
|
225 |
* 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
|
226 |
|
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
227 |
Sorting: |
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
228 |
* 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
|
229 |
* 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
|
230 |
* 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
|
231 |
""" |
0 | 232 |
_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
|
233 |
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
|
234 |
select_target_entity) |
0 | 235 |
# not found, create one |
236 |
attrvar = rqlst.make_variable() |
|
1149 | 237 |
rqlst.add_relation(var, attrname, attrvar) |
0 | 238 |
# if query is grouped, we have to add the attribute variable |
239 |
if rqlst.groupby: |
|
240 |
if not attrvar in rqlst.groupby: |
|
241 |
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
|
242 |
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
|
243 |
_set_orderby(rqlst, attrvar, sortasc, sortfuncname) |
0 | 244 |
# add attribute variable to selection |
245 |
rqlst.add_selected(attrvar) |
|
246 |
return var |
|
247 |
||
248 |
def _cleanup_rqlst(rqlst, mainvar): |
|
249 |
"""cleanup tree from unnecessary restriction: |
|
250 |
* attribute selection |
|
251 |
* optional relations linked to the main variable |
|
252 |
* mandatory relations linked to the main variable |
|
253 |
""" |
|
254 |
if rqlst.where is None: |
|
255 |
return |
|
256 |
schema = rqlst.root.schema |
|
257 |
toremove = set() |
|
258 |
vargraph = deepcopy(rqlst.vargraph) # graph representing links between variable |
|
259 |
for rel in rqlst.where.get_nodes(nodes.Relation): |
|
260 |
ovar = _may_be_removed(rel, schema, mainvar) |
|
261 |
if ovar is not None: |
|
262 |
toremove.add(ovar) |
|
263 |
removed = set() |
|
264 |
while toremove: |
|
265 |
trvar = toremove.pop() |
|
266 |
trvarname = trvar.name |
|
267 |
# remove paths using this variable from the graph |
|
268 |
linkedvars = vargraph.pop(trvarname) |
|
269 |
for ovarname in linkedvars: |
|
270 |
vargraph[ovarname].remove(trvarname) |
|
271 |
# remove relation using this variable |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
272 |
for rel in trvar.stinfo['relations']: |
0 | 273 |
if rel in removed: |
274 |
# already removed |
|
275 |
continue |
|
276 |
rqlst.remove_node(rel) |
|
277 |
removed.add(rel) |
|
5004
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
278 |
rel = trvar.stinfo['typerel'] |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
279 |
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
|
280 |
rqlst.remove_node(rel) |
4cc020ee70e2
le patch rql26 a été importé
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
281 |
removed.add(rel) |
0 | 282 |
# cleanup groupby clause |
283 |
if rqlst.groupby: |
|
284 |
for vref in rqlst.groupby[:]: |
|
285 |
if vref.name == trvarname: |
|
286 |
rqlst.remove_group_var(vref) |
|
287 |
# we can also remove all variables which are linked to this variable |
|
288 |
# and have no path to the main variable |
|
289 |
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
|
290 |
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
|
291 |
continue |
0 | 292 |
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
|
293 |
toremove.add(rqlst.defined_vars[ovarname]) |
0 | 294 |
|
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
295 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
296 |
## base facet classes ########################################################## |
467
a6f056bc7d1d
[facets] AttributeFacet can now declare which kind of comparator they want to use, default is '=' (+ tidy trailing white spaces)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
446
diff
changeset
|
297 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
298 |
class AbstractFacet(AppObject): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
299 |
"""Abstract base class for all facets. Facets are stored in their own |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
300 |
'facets' registry. They are similar to contextual components since the use |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
301 |
the following configurable properties: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
302 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
303 |
* `visible`, boolean flag telling if a facet should be displayed or not |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
304 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
305 |
* `order`, integer to control facets display order |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
306 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
307 |
* `context`, telling if a facet should be displayed in the table form filter |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
308 |
(context = 'tablefilter') or in the facet box (context = 'facetbox') or in |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
309 |
both (context = '') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
310 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
311 |
The following methods define the facet API: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
312 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
313 |
.. automethod:: cubicweb.web.facet.AbstractFacet.get_widget |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
314 |
.. automethod:: cubicweb.web.facet.AbstractFacet.add_rql_restrictions |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
315 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
316 |
Facets will have the following attributes set (beside the standard |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
317 |
:class:`~cubicweb.appobject.AppObject` ones): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
318 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
319 |
* `rqlst`, the rql syntax tree being facetted |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
320 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
321 |
* `filtered_variable`, the variable node in this rql syntax tree that we're |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
322 |
interested in filtering |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
323 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
324 |
Facets implementors may also be interested in the following properties / |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
325 |
methods: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
326 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
327 |
.. automethod:: cubicweb.web.facet.AbstractFacet.operator |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
328 |
.. automethod:: cubicweb.web.facet.AbstractFacet.rqlexec |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
329 |
""" |
0 | 330 |
__abstract__ = True |
331 |
__registry__ = 'facets' |
|
2799
b703639614e7
refactor property handling to avoid name conflicts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2770
diff
changeset
|
332 |
cw_property_defs = { |
0 | 333 |
_('visible'): dict(type='Boolean', default=True, |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
334 |
help=_('display the facet or not')), |
0 | 335 |
_('order'): dict(type='Int', default=99, |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
336 |
help=_('display order of the facet')), |
496
e25a3c2f5393
set default to empty string, not None
sylvain.thenault@logilab.fr
parents:
467
diff
changeset
|
337 |
_('context'): dict(type='String', default='', |
0 | 338 |
# 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
|
339 |
vocabulary=(_('tablefilter'), _('facetbox'), ''), |
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
340 |
help=_('context where this facet should be displayed, ' |
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5774
diff
changeset
|
341 |
'leave empty for both')), |
0 | 342 |
} |
343 |
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
|
344 |
context = '' |
0 | 345 |
needs_update = False |
346 |
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
|
347 |
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
|
348 |
|
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
|
349 |
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
|
350 |
**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
|
351 |
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
|
352 |
assert rqlst is not None |
0 | 353 |
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
|
354 |
# 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
|
355 |
# 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
|
356 |
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
|
357 |
self.filtered_variable = filtered_variable |
0 | 358 |
|
359 |
@property |
|
360 |
def operator(self): |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
361 |
"""Return the operator (AND or OR) to use for this facet when multiple |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
362 |
values are selected. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
363 |
""" |
0 | 364 |
# 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
|
365 |
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
|
366 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
367 |
def rqlexec(self, rql, args=None): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
368 |
"""Utility method to execute some rql queries, and simply returning an |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
369 |
empty list if :exc:`Unauthorized` is raised. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
370 |
""" |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
371 |
try: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
372 |
return self._cw.execute(rql, args) |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
373 |
except Unauthorized: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
374 |
return [] |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
375 |
|
0 | 376 |
def get_widget(self): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
377 |
"""Return the widget instance to use to display this facet, or None if |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
378 |
the facet can't do anything valuable (only one value in the vocabulary |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
379 |
for instance). |
0 | 380 |
""" |
381 |
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
|
382 |
|
0 | 383 |
def add_rql_restrictions(self): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
384 |
"""When some facet criteria has been updated, this method is called to |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
385 |
add restriction for this facet into the rql syntax tree. It should get |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
386 |
back its value in form parameters, and modify the syntax tree |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
387 |
(`self.rqlst`) accordingly. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
388 |
""" |
0 | 389 |
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
|
390 |
|
0 | 391 |
|
392 |
class VocabularyFacet(AbstractFacet): |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
393 |
"""This abstract class extend :class:`AbstractFacet` to use the |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
394 |
:class:`FacetVocabularyWidget` as widget, suitable for facets that may |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
395 |
restrict values according to a (usually computed) vocabulary. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
396 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
397 |
A class which inherits from VocabularyFacet must define at least these methods: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
398 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
399 |
.. automethod:: cubicweb.web.facet.VocabularyFacet.vocabulary |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
400 |
.. automethod:: cubicweb.web.facet.VocabularyFacet.possible_values |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
401 |
""" |
0 | 402 |
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
|
403 |
|
0 | 404 |
def get_widget(self): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
405 |
"""Return the widget instance to use to display this facet. |
0 | 406 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
407 |
This implementation expects a .vocabulary method on the facet and |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
408 |
return a combobox displaying this vocabulary. |
0 | 409 |
""" |
410 |
vocab = self.vocabulary() |
|
411 |
if len(vocab) <= 1: |
|
412 |
return None |
|
413 |
wdg = FacetVocabularyWidget(self) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
414 |
selected = frozenset(typed_eid(eid) for eid in self._cw.list_form_param(self.__regid__)) |
0 | 415 |
for label, value in vocab: |
416 |
if value is None: |
|
417 |
wdg.append(FacetSeparator(label)) |
|
418 |
else: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
419 |
wdg.append(FacetItem(self._cw, label, value, value in selected)) |
0 | 420 |
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
|
421 |
|
0 | 422 |
def vocabulary(self): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
423 |
"""Return vocabulary for this facet, eg a list of 2-uple (label, value). |
0 | 424 |
""" |
425 |
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
|
426 |
|
0 | 427 |
def possible_values(self): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
428 |
"""Return a list of possible values (as string since it's used to |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
429 |
compare to a form value in javascript) for this facet. |
0 | 430 |
""" |
431 |
raise NotImplementedError |
|
432 |
||
433 |
def support_and(self): |
|
434 |
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
|
435 |
|
0 | 436 |
|
437 |
class RelationFacet(VocabularyFacet): |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
438 |
"""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
|
439 |
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
|
440 |
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
|
441 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
442 |
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
|
443 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
444 |
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
|
445 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
446 |
* 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
|
447 |
* 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
|
448 |
* 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
|
449 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
450 |
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
|
451 |
set. |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
452 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
453 |
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
|
454 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
455 |
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
|
456 |
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
|
457 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
458 |
* `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
|
459 |
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
|
460 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
461 |
* `sortasc`: boolean flag to control ascendant/descendant sorting |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
462 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
463 |
To illustrate this facet, let's take for example an *excerpt* of the schema |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
464 |
of an office location search application: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
465 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
466 |
.. sourcecode:: python |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
467 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
468 |
class Office(WorkflowableEntityType): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
469 |
price = Int(description='euros / m2 / HC / HT') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
470 |
surface = Int(description='m2') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
471 |
has_address = SubjectRelation('PostalAddress', |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
472 |
cardinality='1?', |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
473 |
composite='subject') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
474 |
proposed_by = SubjectRelation('Agency') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
475 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
476 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
477 |
We can simply define a facet to filter offices according to the agency |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
478 |
proposing it: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
479 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
480 |
.. sourcecode:: python |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
481 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
482 |
class AgencyFacet(RelationFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
483 |
__regid__ = 'agency' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
484 |
# this facet should only be selected when visualizing offices |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6120
diff
changeset
|
485 |
__select__ = RelationFacet.__select__ & is_instance('Office') |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
486 |
# this facet is a filter on the 'Agency' entities linked to the office |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
487 |
# through the 'proposed_by' relation, where the office is the subject |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
488 |
# of the relation |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
489 |
rtype = 'has_address' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
490 |
# 'subject' is the default but setting it explicitly doesn't hurt... |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
491 |
role = 'subject' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
492 |
# we want to display the agency's name |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
493 |
target_attr = 'name' |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
494 |
""" |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
782
diff
changeset
|
495 |
__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
|
496 |
# class attributes to configure the relation facet |
0 | 497 |
rtype = None |
1433 | 498 |
role = 'subject' |
0 | 499 |
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
|
500 |
target_type = None |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
501 |
# 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
|
502 |
i18nable = True |
0 | 503 |
# set this to a stored procedure name if you want to sort on the result of |
504 |
# this function's result instead of direct value |
|
505 |
sortfunc = None |
|
506 |
# ascendant/descendant sorting |
|
507 |
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
|
508 |
# 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
|
509 |
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
|
510 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
511 |
# internal purpose |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
512 |
_select_target_entity = True |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
513 |
|
0 | 514 |
@property |
515 |
def title(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
516 |
return display_name(self._cw, self.rtype, form=self.role) |
0 | 517 |
|
6251
afc757568492
[facet] add missing @property making subsequent access always true
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6152
diff
changeset
|
518 |
@property |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
519 |
def rql_sort(self): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
520 |
"""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
|
521 |
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
|
522 |
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
|
523 |
""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
524 |
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
|
525 |
and not self.i18nable) |
0 | 526 |
def vocabulary(self): |
527 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
528 |
""" |
|
529 |
rqlst = self.rqlst |
|
530 |
rqlst.save_state() |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
531 |
if self.rql_sort: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
532 |
sort = self.sortasc |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
533 |
else: |
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
534 |
sort = None # will be sorted on label |
0 | 535 |
try: |
536 |
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
|
537 |
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
|
538 |
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
|
539 |
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
|
540 |
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
|
541 |
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
|
542 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
543 |
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
|
544 |
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
|
545 |
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
|
546 |
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
|
547 |
return () |
0 | 548 |
finally: |
549 |
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
|
550 |
# 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
|
551 |
# *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
|
552 |
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
|
553 |
|
0 | 554 |
def possible_values(self): |
555 |
"""return a list of possible values (as string since it's used to |
|
556 |
compare to a form value in javascript) for this facet |
|
557 |
""" |
|
558 |
rqlst = self.rqlst |
|
559 |
rqlst.save_state() |
|
560 |
try: |
|
561 |
_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
|
562 |
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
|
563 |
_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
|
564 |
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
|
565 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
566 |
insert_attr_select_relation( |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
567 |
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
|
568 |
select_target_entity=False) |
0 | 569 |
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
|
570 |
except: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
571 |
import traceback |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
572 |
traceback.print_exc() |
0 | 573 |
finally: |
574 |
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
|
575 |
|
0 | 576 |
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
|
577 |
if self.i18nable: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
578 |
_ = self._cw._ |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
579 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
580 |
_ = unicode |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
581 |
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
|
582 |
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
|
583 |
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
|
584 |
assert self.i18nable |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
585 |
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
|
586 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
587 |
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
|
588 |
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
|
589 |
values = sorted(values) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
590 |
if self.sortasc: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
591 |
return values |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
592 |
return reversed(values) |
0 | 593 |
|
594 |
@cached |
|
595 |
def support_and(self): |
|
3456
1a63a252601f
[facets] typo fix
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
596 |
rschema = self._cw.vreg.schema.rschema(self.rtype) |
0 | 597 |
# 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
|
598 |
possibletypes = self.cw_rset and self.cw_rset.column_types(0) |
4087 | 599 |
for rdef in rschema.rdefs.itervalues(): |
0 | 600 |
if possibletypes is not None: |
601 |
if self.role == 'subject': |
|
4087 | 602 |
if not rdef.subject in possibletypes: |
0 | 603 |
continue |
4087 | 604 |
elif not rdef.object in possibletypes: |
0 | 605 |
continue |
4089
ff92c7d692bf
typos, api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4087
diff
changeset
|
606 |
if rdef.role_cardinality(self.role) in '+*': |
0 | 607 |
return True |
608 |
return False |
|
609 |
||
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 |
|
1149 | 616 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 617 |
if isinstance(value, basestring): |
618 |
# only one value selected |
|
619 |
self.rqlst.add_eid_restriction(restrvar, value) |
|
620 |
elif self.operator == 'OR': |
|
621 |
# set_distinct only if rtype cardinality is > 1 |
|
622 |
if self.support_and(): |
|
623 |
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
|
624 |
# multiple ORed values: using IN is fine |
0 | 625 |
self.rqlst.add_eid_restriction(restrvar, value) |
626 |
else: |
|
627 |
# multiple values with AND operator |
|
628 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
|
629 |
while value: |
|
1149 | 630 |
restrvar = _add_rtype_relation(self.rqlst, mainvar, self.rtype, self.role) |
0 | 631 |
self.rqlst.add_eid_restriction(restrvar, value.pop()) |
632 |
||
633 |
||
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
634 |
class RelationAttributeFacet(RelationFacet): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
635 |
"""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
|
636 |
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
|
637 |
:class:`RelationFacet`, except that: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
638 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
639 |
* `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
|
640 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
641 |
* 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
|
642 |
String |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
643 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
644 |
* you can specify a comparison operator using `comparator` |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
645 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
646 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
647 |
Back to our example... if you want to search office by postal code and that |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
648 |
you use a :class:`RelationFacet` for that, you won't get the expected |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
649 |
behaviour: if two offices have the same postal code, they've however two |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
650 |
different addresses. So you'll see in the facet the same postal code twice, |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
651 |
though linked to a different address entity. There is a great chance your |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
652 |
users won't understand that... |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
653 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
654 |
That's where this class come in ! It's used to said that you want to filter |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
655 |
according to the *attribute value* of a relatied entity, not to the entity |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
656 |
itself. Now here is the source code for the facet: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
657 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
658 |
.. sourcecode:: python |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
659 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
660 |
class PostalCodeFacet(RelationAttributeFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
661 |
__regid__ = 'postalcode' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
662 |
# this facet should only be selected when visualizing offices |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6120
diff
changeset
|
663 |
__select__ = RelationAttributeFacet.__select__ & is_instance('Office') |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
664 |
# this facet is a filter on the PostalAddress entities linked to the |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
665 |
# office through the 'has_address' relation, where the office is the |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
666 |
# subject of the relation |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
667 |
rtype = 'has_address' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
668 |
role = 'subject' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
669 |
# we want to search according to address 'postal_code' attribute |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
670 |
target_attr = 'postalcode' |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
671 |
""" |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
672 |
_select_target_entity = False |
0 | 673 |
# attribute type |
674 |
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
|
675 |
# 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
|
676 |
comparator = '=' # could be '<', '<=', '>', '>=' |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
677 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
678 |
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
|
679 |
if self.i18nable: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
680 |
_ = self._cw._ |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
681 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
682 |
_ = unicode |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
683 |
if self.rql_sort: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
684 |
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
|
685 |
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
|
686 |
if self.sortasc: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
687 |
return sorted(values) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
688 |
return reversed(sorted(values)) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
689 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
690 |
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
|
691 |
"""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
|
692 |
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
|
693 |
if not value: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
694 |
return |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
695 |
mainvar = self.filtered_variable |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
696 |
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
|
697 |
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
|
698 |
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
|
699 |
# 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
|
700 |
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
|
701 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
702 |
else: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
703 |
# 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
|
704 |
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
|
705 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
706 |
while value: |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
707 |
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
|
708 |
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
|
709 |
self.attrtype, self.comparator) |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
710 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
711 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
712 |
class AttributeFacet(RelationAttributeFacet): |
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
713 |
"""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
|
714 |
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
|
715 |
|
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
716 |
* `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
|
717 |
* `role` neither, it's systematically 'subject' |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
718 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
719 |
So, suppose that in our office search example you want to refine search according |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
720 |
to the office's surface. Here is a code snippet achieving this: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
721 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
722 |
.. sourcecode:: python |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
723 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
724 |
class SurfaceFacet(AttributeFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
725 |
__regid__ = 'surface' |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6120
diff
changeset
|
726 |
__select__ = AttributeFacet.__select__ & is_instance('Office') |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
727 |
# this facet is a filter on the office'surface |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
728 |
rtype = 'surface' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
729 |
# override the default value of operator since we want to filter |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
730 |
# according to a minimal value, not an exact one |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
731 |
comparator = '>=' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
732 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
733 |
def vocabulary(self): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
734 |
'''override the default vocabulary method since we want to |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
735 |
hard-code our threshold values. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
736 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
737 |
Not overriding would generate a filter containing all existing |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
738 |
surfaces defined in the database. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
739 |
''' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
740 |
return [('> 200', '200'), ('> 250', '250'), |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
741 |
('> 275', '275'), ('> 300', '300')] |
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
742 |
""" |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
743 |
|
6119
b217635d3b28
new RelationAttributeFacet + other fixes and cleanup + facets test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
744 |
_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
|
745 |
|
0 | 746 |
def vocabulary(self): |
747 |
"""return vocabulary for this facet, eg a list of 2-uple (label, value) |
|
748 |
""" |
|
749 |
rqlst = self.rqlst |
|
750 |
rqlst.save_state() |
|
751 |
try: |
|
752 |
mainvar = self.filtered_variable |
|
753 |
_cleanup_rqlst(rqlst, mainvar) |
|
1149 | 754 |
newvar = _prepare_vocabulary_rqlst(rqlst, mainvar, self.rtype, self.role) |
0 | 755 |
_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
|
756 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5016
diff
changeset
|
757 |
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
|
758 |
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
|
759 |
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
|
760 |
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
|
761 |
return () |
0 | 762 |
finally: |
763 |
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
|
764 |
# 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
|
765 |
# *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
|
766 |
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
|
767 |
|
0 | 768 |
def support_and(self): |
769 |
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
|
770 |
|
0 | 771 |
def add_rql_restrictions(self): |
772 |
"""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
|
773 |
value = self._cw.form.get(self.__regid__) |
0 | 774 |
if not value: |
775 |
return |
|
776 |
mainvar = self.filtered_variable |
|
777 |
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
|
778 |
self.attrtype, self.comparator) |
0 | 779 |
|
780 |
||
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
781 |
class RangeFacet(AttributeFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
782 |
"""This class allows to filter entities according to an attribute of |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
783 |
numerical type. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
784 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
785 |
It displays a slider using `jquery`_ to choose a lower bound and an upper |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
786 |
bound. |
0 | 787 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
788 |
The example below provides an alternative to the surface facet seen earlier, |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
789 |
in a more powerful way since |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
790 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
791 |
* lower/upper boundaries are computed according to entities to filter |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
792 |
* user can specify lower/upper boundaries, not only the lower one |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
793 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
794 |
.. sourcecode:: python |
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
|
795 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
796 |
class SurfaceFacet(RangeFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
797 |
__regid__ = 'surface' |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6120
diff
changeset
|
798 |
__select__ = RangeFacet.__select__ & is_instance('Office') |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
799 |
# this facet is a filter on the office'surface |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
800 |
rtype = 'surface' |
0 | 801 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
802 |
All this with even less code! |
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
|
803 |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
804 |
The image below display the rendering of the slider: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
805 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
806 |
.. image:: ../images/facet_range.png |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
807 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
808 |
.. _jquery: http://www.jqueryui.com/ |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
809 |
""" |
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
|
810 |
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
|
811 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
812 |
@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
|
813 |
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
|
814 |
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
|
815 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
816 |
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
|
817 |
"""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
|
818 |
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
|
819 |
# 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
|
820 |
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
|
821 |
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
|
822 |
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
|
823 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
824 |
def infvalue(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
825 |
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
|
826 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
827 |
def supvalue(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
828 |
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
|
829 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
830 |
def formatvalue(self, value): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
831 |
"""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
|
832 |
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
|
833 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
834 |
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
|
835 |
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
|
836 |
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
|
837 |
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
|
838 |
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
|
839 |
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
|
840 |
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
|
841 |
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
|
842 |
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
|
843 |
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
|
844 |
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
|
845 |
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
|
846 |
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
|
847 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
848 |
|
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
|
849 |
class DateRangeFacet(RangeFacet): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
850 |
"""This class works similarly as the :class:`RangeFacet` but for attribute |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
851 |
of date type. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
852 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
853 |
The image below display the rendering of the slider for a date range: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
854 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
855 |
.. image:: ../images/facet_date_range.png |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
856 |
""" |
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
|
857 |
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
|
858 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
859 |
@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
|
860 |
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
|
861 |
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
|
862 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
863 |
def formatvalue(self, value): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
864 |
"""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
|
865 |
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
|
866 |
|
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
867 |
|
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
868 |
class HasRelationFacet(AbstractFacet): |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
869 |
"""This class simply filter according to the presence of a relation |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
870 |
(whatever the entity at the other end). It display a simple checkbox that |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
871 |
lets you refine your selection in order to get only entities that actually |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
872 |
have this relation. You simply have to define which relation using the |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
873 |
`rtype` and `role` attributes. |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
874 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
875 |
Here is an example of the rendering of thos facet to filter book with image |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
876 |
and the corresponding code: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
877 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
878 |
.. image:: ../images/facet_has_image.png |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
879 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
880 |
.. sourcecode:: python |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
881 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
882 |
class HasImageFacet(HasRelationFacet): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
883 |
__regid__ = 'hasimage' |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6120
diff
changeset
|
884 |
__select__ = HasRelationFacet.__select__ & is_instance('Book') |
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
885 |
rtype = 'has_image' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
886 |
role = 'subject' |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
887 |
""" |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
888 |
rtype = None # override me in subclass |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
889 |
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
|
890 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
891 |
@property |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
892 |
def title(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
893 |
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
|
894 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
895 |
def support_and(self): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
896 |
return False |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
897 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
898 |
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
|
899 |
return CheckBoxFacetWidget(self._cw, self, |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
900 |
'%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
|
901 |
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
|
902 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
903 |
def add_rql_restrictions(self): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
904 |
"""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
|
905 |
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
|
906 |
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
|
907 |
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
|
908 |
return |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
909 |
var = self.rqlst.make_variable() |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
910 |
if self.role == 'subject': |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
911 |
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
|
912 |
else: |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
913 |
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
|
914 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
915 |
|
0 | 916 |
## html widets ################################################################ |
917 |
||
918 |
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
|
919 |
|
0 | 920 |
def __init__(self, facet): |
921 |
self.facet = facet |
|
922 |
self.items = [] |
|
923 |
||
924 |
def append(self, item): |
|
925 |
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
|
926 |
|
0 | 927 |
def _render(self): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
928 |
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
|
929 |
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
|
930 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
0 | 931 |
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
|
932 |
(xml_escape(facetid), title)) |
0 | 933 |
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
|
934 |
_ = self.facet._cw._ |
0 | 935 |
self.w(u'''<select name="%s" class="radio facetOperator" title="%s"> |
936 |
<option value="OR">%s</option> |
|
937 |
<option value="AND">%s</option> |
|
938 |
</select>''' % (facetid + '_andor', _('and/or between different values'), |
|
939 |
_('OR'), _('AND'))) |
|
368
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
940 |
cssclass = '' |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
941 |
if not self.facet.start_unfolded: |
84a5106840fa
facet widgets css cleanup
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
346
diff
changeset
|
942 |
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
|
943 |
if len(self.items) > 6: |
1132 | 944 |
cssclass += ' overflowed' |
0 | 945 |
self.w(u'<div class="facetBody%s">\n' % cssclass) |
946 |
for item in self.items: |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1933
diff
changeset
|
947 |
item.render(w=self.w) |
0 | 948 |
self.w(u'</div>\n') |
949 |
self.w(u'</div>\n') |
|
950 |
||
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
|
951 |
|
0 | 952 |
class FacetStringWidget(HTMLWidget): |
953 |
def __init__(self, facet): |
|
954 |
self.facet = facet |
|
955 |
self.value = None |
|
956 |
||
957 |
def _render(self): |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
958 |
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
|
959 |
facetid = xml_escape(self.facet.__regid__) |
0 | 960 |
self.w(u'<div id="%s" class="facet">\n' % facetid) |
961 |
self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' % |
|
962 |
(facetid, title)) |
|
963 |
self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u'')) |
|
964 |
self.w(u'</div>\n') |
|
965 |
||
966 |
||
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
|
967 |
class FacetRangeWidget(HTMLWidget): |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
968 |
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
|
969 |
onload = u''' |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
970 |
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
|
971 |
jQuery("#%(sliderid)s").slider({ |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
972 |
range: true, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
973 |
min: %(minvalue)s, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
974 |
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
|
975 |
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
|
976 |
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
|
977 |
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
|
978 |
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
|
979 |
}, |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
980 |
slide: function(event, ui) { |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
981 |
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
|
982 |
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
|
983 |
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
|
984 |
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
|
985 |
} |
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
|
986 |
}); |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
987 |
// 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
|
988 |
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
|
989 |
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
|
990 |
''' |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
991 |
#'# 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
|
992 |
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
|
993 |
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
|
994 |
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
|
995 |
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
|
996 |
|
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
|
997 |
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
|
998 |
facet = self.facet |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
999 |
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
|
1000 |
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
|
1001 |
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
|
1002 |
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
|
1003 |
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
|
1004 |
'sliderid': sliderid, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1005 |
'facetid': facetid, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1006 |
'minvalue': self.minvalue, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1007 |
'maxvalue': self.maxvalue, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1008 |
'formatter': self.formatter, |
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1009 |
}) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
1010 |
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
|
1011 |
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
|
1012 |
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
|
1013 |
(facetid, title)) |
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1014 |
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
|
1015 |
% (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
|
1016 |
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
|
1017 |
% (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
|
1018 |
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
|
1019 |
% (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
|
1020 |
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
|
1021 |
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
|
1022 |
|
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
|
1023 |
|
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
|
1024 |
class DateFacetRangeWidget(FacetRangeWidget): |
2149
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
1025 |
|
2020
fe437afc8fb7
[facet] refactor values formatting in RangeFacets
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2016
diff
changeset
|
1026 |
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
|
1027 |
|
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
1028 |
def round_max_value(self, d): |
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
1029 |
'round to upper value to avoid filtering out the max value' |
2152 | 1030 |
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
|
1031 |
|
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
|
1032 |
def __init__(self, facet, minvalue, maxvalue): |
2149
6a631ec5cc71
fix date rounding issue in DateFacetRangeWidget
Florent <florent@secondweb.fr>
parents:
2130
diff
changeset
|
1033 |
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
|
1034 |
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
|
1035 |
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
|
1036 |
datetime2ticks(maxvalue)) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
1037 |
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
|
1038 |
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
|
1039 |
|
c594323929cb
[facet] provide a DateRangeFacet (still need to refactor, maybe create a CWWidget)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
1040 |
|
0 | 1041 |
class FacetItem(HTMLWidget): |
1042 |
||
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
|
1043 |
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
|
1044 |
unselected_img = "no-check-no-border.png" |
0 | 1045 |
|
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
|
1046 |
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
|
1047 |
self._cw = req |
0 | 1048 |
self.label = label |
1049 |
self.value = value |
|
1050 |
self.selected = selected |
|
1051 |
||
1052 |
def _render(self): |
|
1053 |
if self.selected: |
|
1054 |
cssclass = ' facetValueSelected' |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
1055 |
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
|
1056 |
imgalt = self._cw._('selected') |
0 | 1057 |
else: |
1058 |
cssclass = '' |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
1059 |
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
|
1060 |
imgalt = self._cw._('not selected') |
0 | 1061 |
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
|
1062 |
% (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
|
1063 |
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
|
1064 |
self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label)) |
0 | 1065 |
self.w(u'</div>') |
1066 |
||
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
1067 |
|
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1068 |
class CheckBoxFacetWidget(HTMLWidget): |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1069 |
selected_img = "black-check.png" |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1070 |
unselected_img = "black-uncheck.png" |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1071 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1072 |
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
|
1073 |
self._cw = req |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1074 |
self.facet = facet |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
1075 |
self.value = value |
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
1076 |
self.selected = selected |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1077 |
|
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1078 |
def _render(self): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2305
diff
changeset
|
1079 |
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
|
1080 |
facetid = xml_escape(self.facet.__regid__) |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
1081 |
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
|
1082 |
if self.selected: |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1083 |
cssclass = ' facetValueSelected' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
1084 |
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
|
1085 |
imgalt = self._cw._('selected') |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1086 |
else: |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1087 |
cssclass = '' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
1088 |
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
|
1089 |
imgalt = self._cw._('not selected') |
2014
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1090 |
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
|
1091 |
% (cssclass, xml_escape(unicode(self.value)))) |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
1092 |
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
|
1093 |
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
|
1094 |
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
|
1095 |
self.w(u'</div>\n') |
71a9ab77d9f8
[facet] provide hasRelationFacet (backport from CMHN)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2010
diff
changeset
|
1096 |
self.w(u'</div>\n') |
2016
a6638de6d4da
[views] reindent web/facet.py
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2014
diff
changeset
|
1097 |
self.w(u'</div>\n') |
0 | 1098 |
|
3329
8430f6c693a1
cleanup, smarter sort control to avoid it when not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3328
diff
changeset
|
1099 |
|
0 | 1100 |
class FacetSeparator(HTMLWidget): |
1101 |
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
|
1102 |
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
|
1103 |
|
0 | 1104 |
def _render(self): |
1105 |
pass |
|
6120
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1106 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1107 |
# other classes ################################################################ |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1108 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1109 |
class FilterRQLBuilder(object): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1110 |
"""called by javascript to get a rql string from filter form""" |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1111 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1112 |
def __init__(self, req): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1113 |
self._cw = req |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1114 |
|
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1115 |
def build_rql(self):#, tablefilter=False): |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1116 |
form = self._cw.form |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1117 |
facetids = form['facets'].split(',') |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1118 |
# XXX Union unsupported yet |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1119 |
select = self._cw.vreg.parse(self._cw, form['baserql']).children[0] |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1120 |
mainvar = filtered_variable(select) |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1121 |
toupdate = [] |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1122 |
for facetid in facetids: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1123 |
facet = get_facet(self._cw, facetid, select, mainvar) |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1124 |
facet.add_rql_restrictions() |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1125 |
if facet.needs_update: |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1126 |
toupdate.append(facetid) |
c000e41316ec
[book] some more documentation and cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6119
diff
changeset
|
1127 |
return select.as_string(), toupdate |