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