author | Sylvain Thenault <sylvain.thenault@logilab.fr> |
Fri, 19 Dec 2008 15:16:09 +0100 | |
changeset 237 | 3df2e0ae2eba |
parent 236 | 8ab9e72dd8f1 |
child 247 | 3417739305a1 |
permissions | -rw-r--r-- |
0 | 1 |
"""This file contains some basic selectors required by application objects. |
2 |
||
3 |
A selector is responsible to score how well an object may be used with a |
|
4 |
given result set (publishing time selection) |
|
5 |
||
6 |
:organization: Logilab |
|
7 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
8 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
9 |
""" |
|
10 |
||
11 |
__docformat__ = "restructuredtext en" |
|
12 |
||
13 |
from logilab.common.compat import all |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
14 |
from logilab.common.deprecation import deprecated_function |
0 | 15 |
|
230
6e71adc5ccdd
new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
215
diff
changeset
|
16 |
from cubicweb import Unauthorized, NoSelectableObject, role |
0 | 17 |
from cubicweb.cwvreg import DummyCursorError |
149 | 18 |
from cubicweb.vregistry import chainall, chainfirst, NoSelectableObject |
0 | 19 |
from cubicweb.cwconfig import CubicWebConfiguration |
20 |
from cubicweb.schema import split_expression |
|
21 |
||
22 |
||
23 |
def lltrace(selector): |
|
24 |
# don't wrap selectors if not in development mode |
|
25 |
if CubicWebConfiguration.mode == 'installed': |
|
26 |
return selector |
|
27 |
def traced(cls, *args, **kwargs): |
|
28 |
ret = selector(cls, *args, **kwargs) |
|
215 | 29 |
cls.lldebug('selector %s returned %s for %s', selector.__name__, ret, cls) |
0 | 30 |
return ret |
31 |
return traced |
|
32 |
||
33 |
# very basic selectors ######################################################## |
|
34 |
||
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
35 |
def yes(cls, *args, **kwargs): |
0 | 36 |
"""accept everything""" |
37 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
38 |
yes_selector = deprecated_function(yes) |
0 | 39 |
|
40 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
41 |
def none_rset(cls, req, rset, *args, **kwargs): |
0 | 42 |
"""accept no result set""" |
43 |
if rset is None: |
|
44 |
return 1 |
|
45 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
46 |
norset_selector = deprecated_function(none_rset) |
0 | 47 |
|
48 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
49 |
def rset(cls, req, rset, *args, **kwargs): |
0 | 50 |
"""accept result set, whatever the number of result""" |
51 |
if rset is not None: |
|
52 |
return 1 |
|
53 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
54 |
rset_selector = deprecated_function(rset) |
0 | 55 |
|
56 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
57 |
def nonempty_rset(cls, req, rset, *args, **kwargs): |
0 | 58 |
"""accept any non empty result set""" |
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
59 |
if rset is not None and rset.rowcount: |
0 | 60 |
return 1 |
61 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
62 |
anyrset_selector = deprecated_function(nonempty_rset) |
0 | 63 |
|
64 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
65 |
def empty_rset(cls, req, rset, *args, **kwargs): |
0 | 66 |
"""accept empty result set""" |
67 |
if rset is not None and rset.rowcount == 0: |
|
68 |
return 1 |
|
69 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
70 |
emptyrset_selector = deprecated_function(empty_rset) |
0 | 71 |
|
72 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
73 |
def one_line_rset(cls, req, rset, row=None, *args, **kwargs): |
0 | 74 |
"""accept result set with a single line of result""" |
75 |
if rset is not None and (row is not None or rset.rowcount == 1): |
|
76 |
return 1 |
|
77 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
78 |
onelinerset_selector = deprecated_function(one_line_rset) |
0 | 79 |
|
80 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
81 |
def two_lines_rset(cls, req, rset, *args, **kwargs): |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
82 |
"""accept result set with *at least* two lines of result""" |
0 | 83 |
if rset is not None and rset.rowcount > 1: |
84 |
return 1 |
|
85 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
86 |
twolinerset_selector = deprecated_function(two_lines_rset) |
0 | 87 |
|
88 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
89 |
def two_cols_rset(cls, req, rset, *args, **kwargs): |
0 | 90 |
"""accept result set with at least one line and two columns of result""" |
91 |
if rset is not None and rset.rowcount > 0 and len(rset.rows[0]) > 1: |
|
92 |
return 1 |
|
93 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
94 |
twocolrset_selector = deprecated_function(two_cols_rset) |
0 | 95 |
|
96 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
97 |
def paginated_rset(cls, req, rset, *args, **kwargs): |
0 | 98 |
"""accept result sets with more rows than the page size |
99 |
""" |
|
100 |
if rset is None or len(rset) <= req.property_value('navigation.page-size'): |
|
101 |
return 0 |
|
102 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
103 |
largerset_selector = deprecated_function(paginated_rset) |
0 | 104 |
|
105 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
106 |
def sorted_rset(cls, req, rset, row=None, col=None): |
0 | 107 |
"""accept sorted result set""" |
108 |
rqlst = rset.syntax_tree() |
|
109 |
if len(rqlst.children) > 1 or not rqlst.children[0].orderby: |
|
110 |
return 0 |
|
111 |
return 2 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
112 |
sortedrset_selector = deprecated_function(sorted_rset) |
0 | 113 |
|
114 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
115 |
def one_etype_rset(cls, req, rset, *args, **kwargs): |
0 | 116 |
"""accept result set where entities in the first columns are all of the |
117 |
same type |
|
118 |
""" |
|
119 |
if len(rset.column_types(0)) != 1: |
|
120 |
return 0 |
|
121 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
122 |
oneetyperset_selector = deprecated_function(one_etype_rset) |
0 | 123 |
|
124 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
125 |
def two_etypes_rset(cls, req, rset, **kwargs): |
0 | 126 |
"""accepts resultsets containing several entity types""" |
127 |
if rset: |
|
128 |
etypes = rset.column_types(0) |
|
129 |
if len(etypes) > 1: |
|
130 |
return 1 |
|
131 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
132 |
multitype_selector = deprecated_function(two_etypes_rset) |
0 | 133 |
|
134 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
135 |
def match_search_state(cls, req, rset, row=None, col=None, **kwargs): |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
136 |
"""checks if the current search state is in a .search_states attribute of |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
137 |
the wrapped class |
0 | 138 |
|
139 |
search state should be either 'normal' or 'linksearch' (eg searching for an |
|
140 |
object to create a relation with another) |
|
141 |
""" |
|
142 |
try: |
|
143 |
if not req.search_state[0] in cls.search_states: |
|
144 |
return 0 |
|
145 |
except AttributeError: |
|
146 |
return 1 # class don't care about search state, accept it |
|
147 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
148 |
searchstate_selector = deprecated_function(match_search_state) |
0 | 149 |
|
150 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
151 |
def anonymous_user(cls, req, *args, **kwargs): |
0 | 152 |
"""accept if user is anonymous""" |
153 |
if req.cnx.anonymous_connection: |
|
154 |
return 1 |
|
155 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
156 |
anonymous_selector = deprecated_function(anonymous_user) |
0 | 157 |
|
158 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
159 |
def authenticated_user(cls, req, *args, **kwargs): |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
160 |
"""accept if user is authenticated""" |
0 | 161 |
return not anonymous_selector(cls, req, *args, **kwargs) |
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
162 |
not_anonymous_selector = deprecated_function(authenticated_user) |
0 | 163 |
|
164 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
165 |
def match_form_params(cls, req, *args, **kwargs): |
0 | 166 |
"""check if parameters specified by the form_params attribute on |
167 |
the wrapped class are specified in request form parameters |
|
168 |
""" |
|
169 |
score = 0 |
|
170 |
for param in cls.form_params: |
|
171 |
val = req.form.get(param) |
|
172 |
if not val: |
|
173 |
return 0 |
|
174 |
score += 1 |
|
175 |
return score + 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
176 |
req_form_params_selector = deprecated_function(match_form_params) |
0 | 177 |
|
178 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
179 |
def match_kwargs(cls, req, *args, **kwargs): |
0 | 180 |
"""check if arguments specified by the expected_kwargs attribute on |
181 |
the wrapped class are specified in given named parameters |
|
182 |
""" |
|
183 |
values = [] |
|
184 |
for arg in cls.expected_kwargs: |
|
185 |
if not arg in kwargs: |
|
186 |
return 0 |
|
187 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
188 |
kwargs_selector = deprecated_function(match_kwargs) |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
189 |
|
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
190 |
|
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
191 |
# not so basic selectors ###################################################### |
0 | 192 |
|
193 |
@lltrace |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
194 |
def accept_etype(cls, req, *args, **kwargs): |
0 | 195 |
"""check etype presence in request form *and* accepts conformance""" |
196 |
if 'etype' not in req.form and 'etype' not in kwargs: |
|
197 |
return 0 |
|
198 |
try: |
|
199 |
etype = req.form['etype'] |
|
200 |
except KeyError: |
|
201 |
etype = kwargs['etype'] |
|
202 |
# value is a list or a tuple if web request form received several |
|
203 |
# values for etype parameter |
|
204 |
assert isinstance(etype, basestring), "got multiple etype parameters in req.form" |
|
205 |
if 'Any' in cls.accepts: |
|
206 |
return 1 |
|
207 |
# no Any found, we *need* exact match |
|
208 |
if etype not in cls.accepts: |
|
209 |
return 0 |
|
210 |
# exact match must return a greater value than 'Any'-match |
|
211 |
return 2 |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
212 |
etype_form_selector = deprecated_function(accept_etype) |
0 | 213 |
|
214 |
@lltrace |
|
215 |
def _nfentity_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
216 |
"""accept non final entities |
|
217 |
if row is not specified, use the first one |
|
218 |
if col is not specified, use the first one |
|
219 |
""" |
|
220 |
etype = rset.description[row or 0][col or 0] |
|
221 |
if etype is None: # outer join |
|
222 |
return 0 |
|
223 |
if cls.schema.eschema(etype).is_final(): |
|
224 |
return 0 |
|
225 |
return 1 |
|
226 |
||
227 |
@lltrace |
|
228 |
def _rqlcondition_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
229 |
"""accept single entity result set if the entity match an rql condition |
|
230 |
""" |
|
231 |
if cls.condition: |
|
232 |
eid = rset[row or 0][col or 0] |
|
233 |
if 'U' in frozenset(split_expression(cls.condition)): |
|
234 |
rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % cls.condition |
|
235 |
else: |
|
236 |
rql = 'Any X WHERE X eid %%(x)s, %s' % cls.condition |
|
237 |
try: |
|
238 |
return len(req.execute(rql, {'x': eid, 'u': req.user.eid}, 'x')) |
|
239 |
except Unauthorized: |
|
240 |
return 0 |
|
241 |
||
242 |
return 1 |
|
243 |
||
244 |
@lltrace |
|
245 |
def _interface_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
246 |
"""accept uniform result sets, and apply the following rules: |
|
247 |
||
248 |
* wrapped class must have a accepts_interfaces attribute listing the |
|
249 |
accepted ORed interfaces |
|
250 |
* if row is None, return the sum of values returned by the method |
|
251 |
for each entity's class in the result set. If any score is 0, |
|
252 |
return 0. |
|
253 |
* if row is specified, return the value returned by the method with |
|
254 |
the entity's class of this row |
|
255 |
""" |
|
184
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
256 |
# XXX this selector can be refactored : extract the code testing |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
257 |
# for entity schema / interface compliance |
0 | 258 |
score = 0 |
259 |
# check 'accepts' to give priority to more specific classes |
|
260 |
if row is None: |
|
261 |
for etype in rset.column_types(col or 0): |
|
262 |
eclass = cls.vreg.etype_class(etype) |
|
263 |
escore = 0 |
|
264 |
for iface in cls.accepts_interfaces: |
|
265 |
escore += iface.is_implemented_by(eclass) |
|
266 |
if not escore: |
|
267 |
return 0 |
|
268 |
score += escore |
|
184
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
269 |
accepts = set(getattr(cls, 'accepts', ())) |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
270 |
# if accepts is defined on the vobject, eclass must match |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
271 |
if accepts: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
272 |
eschema = eclass.e_schema |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
273 |
etypes = set([eschema] + eschema.ancestors()) |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
274 |
if accepts & etypes: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
275 |
score += 2 |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
276 |
elif 'Any' not in accepts: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
277 |
return 0 |
0 | 278 |
return score + 1 |
279 |
etype = rset.description[row][col or 0] |
|
280 |
if etype is None: # outer join |
|
281 |
return 0 |
|
282 |
eclass = cls.vreg.etype_class(etype) |
|
283 |
for iface in cls.accepts_interfaces: |
|
284 |
score += iface.is_implemented_by(eclass) |
|
285 |
if score: |
|
184
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
286 |
accepts = set(getattr(cls, 'accepts', ())) |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
287 |
# if accepts is defined on the vobject, eclass must match |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
288 |
if accepts: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
289 |
eschema = eclass.e_schema |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
290 |
etypes = set([eschema] + eschema.ancestors()) |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
291 |
if accepts & etypes: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
292 |
score += 1 |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
293 |
elif 'Any' not in accepts: |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
294 |
return 0 |
92aebc6b533c
fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
162
diff
changeset
|
295 |
score += 1 |
0 | 296 |
return score |
297 |
||
298 |
@lltrace |
|
299 |
def score_entity_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
300 |
if row is None: |
|
301 |
rows = xrange(rset.rowcount) |
|
302 |
else: |
|
303 |
rows = (row,) |
|
304 |
for row in rows: |
|
305 |
try: |
|
306 |
score = cls.score_entity(rset.get_entity(row, col or 0)) |
|
307 |
except DummyCursorError: |
|
308 |
# get a dummy cursor error, that means we are currently |
|
309 |
# using a dummy rset to list possible views for an entity |
|
310 |
# type, not for an actual result set. In that case, we |
|
311 |
# don't care of the value, consider the object as selectable |
|
312 |
return 1 |
|
313 |
if not score: |
|
314 |
return 0 |
|
315 |
return 1 |
|
316 |
||
317 |
@lltrace |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
318 |
def accept_rset(cls, req, rset, row=None, col=None, **kwargs): |
0 | 319 |
"""simply delegate to cls.accept_rset method""" |
320 |
return cls.accept_rset(req, rset, row=row, col=col) |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
321 |
accept_rset_selector = deprecated_function(accept_rset) |
0 | 322 |
|
323 |
@lltrace |
|
324 |
def but_etype_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
325 |
"""restrict the searchstate_accept_one_selector to exclude entity's type |
|
326 |
refered by the .etype attribute |
|
327 |
""" |
|
328 |
if rset.description[row or 0][col or 0] == cls.etype: |
|
329 |
return 0 |
|
330 |
return 1 |
|
331 |
||
332 |
@lltrace |
|
333 |
def etype_rtype_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
334 |
"""only check if the user has read access on the entity's type refered |
|
335 |
by the .etype attribute and on the relations's type refered by the |
|
336 |
.rtype attribute if set. |
|
337 |
""" |
|
338 |
schema = cls.schema |
|
339 |
perm = getattr(cls, 'require_permission', 'read') |
|
340 |
if hasattr(cls, 'etype'): |
|
341 |
eschema = schema.eschema(cls.etype) |
|
342 |
if not (eschema.has_perm(req, perm) or eschema.has_local_role(perm)): |
|
343 |
return 0 |
|
344 |
if hasattr(cls, 'rtype'): |
|
345 |
if not schema.rschema(cls.rtype).has_perm(req, perm): |
|
346 |
return 0 |
|
347 |
return 1 |
|
348 |
||
349 |
@lltrace |
|
350 |
def accept_rtype_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
351 |
if hasattr(cls, 'rtype'): |
|
352 |
if row is None: |
|
353 |
for etype in rset.column_types(col or 0): |
|
354 |
if not cls.relation_possible(etype): |
|
355 |
return 0 |
|
356 |
elif not cls.relation_possible(rset.description[row][col or 0]): |
|
357 |
return 0 |
|
358 |
return 1 |
|
359 |
||
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
360 |
@lltrace |
230
6e71adc5ccdd
new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
215
diff
changeset
|
361 |
def has_related_entities(cls, req, rset, row=None, col=None, **kwargs): |
6e71adc5ccdd
new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
215
diff
changeset
|
362 |
assert row is not None |
6e71adc5ccdd
new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
215
diff
changeset
|
363 |
return bool(rset.get_entity(row, col).related(cls.rtype, role(cls))) |
6e71adc5ccdd
new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
215
diff
changeset
|
364 |
|
0 | 365 |
@lltrace |
366 |
def one_has_relation_selector(cls, req, rset, row=None, col=None, **kwargs): |
|
367 |
"""check if the user has read access on the relations's type refered by the |
|
368 |
.rtype attribute of the class, and if at least one entity type in the |
|
369 |
result set has this relation. |
|
370 |
""" |
|
371 |
schema = cls.schema |
|
372 |
perm = getattr(cls, 'require_permission', 'read') |
|
373 |
if not schema.rschema(cls.rtype).has_perm(req, perm): |
|
374 |
return 0 |
|
375 |
if row is None: |
|
376 |
for etype in rset.column_types(col or 0): |
|
377 |
if cls.relation_possible(etype): |
|
378 |
return 1 |
|
379 |
elif cls.relation_possible(rset.description[row][col or 0]): |
|
380 |
return 1 |
|
381 |
return 0 |
|
382 |
||
383 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
384 |
def match_user_group(cls, req, rset=None, row=None, col=None, **kwargs): |
0 | 385 |
"""select according to user's groups""" |
386 |
if not cls.require_groups: |
|
387 |
return 1 |
|
388 |
user = req.user |
|
389 |
if user is None: |
|
390 |
return int('guests' in cls.require_groups) |
|
391 |
score = 0 |
|
392 |
if 'owners' in cls.require_groups and rset: |
|
393 |
if row is not None: |
|
394 |
eid = rset[row][col or 0] |
|
395 |
if user.owns(eid): |
|
396 |
score = 1 |
|
397 |
else: |
|
398 |
score = all(user.owns(r[col or 0]) for r in rset) |
|
399 |
score += user.matching_groups(cls.require_groups) |
|
400 |
if score: |
|
401 |
# add 1 so that an object with one matching group take priority |
|
402 |
# on an object without require_groups |
|
403 |
return score + 1 |
|
404 |
return 0 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
405 |
in_group_selector = deprecated_function(match_user_group) |
0 | 406 |
|
407 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
408 |
def user_can_add_etype(cls, req, rset, row=None, col=None, **kwargs): |
0 | 409 |
"""only check if the user has add access on the entity's type refered |
410 |
by the .etype attribute. |
|
411 |
""" |
|
412 |
if not cls.schema.eschema(cls.etype).has_perm(req, 'add'): |
|
413 |
return 0 |
|
414 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
415 |
add_etype_selector = deprecated_function(user_can_add_etype) |
0 | 416 |
|
417 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
418 |
def match_context_prop(cls, req, rset, row=None, col=None, context=None, |
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
419 |
**kwargs): |
0 | 420 |
propval = req.property_value('%s.%s.context' % (cls.__registry__, cls.id)) |
421 |
if not propval: |
|
422 |
propval = cls.context |
|
423 |
if context is not None and propval is not None and context != propval: |
|
424 |
return 0 |
|
425 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
426 |
contextprop_selector = deprecated_function(match_context_prop) |
0 | 427 |
|
428 |
@lltrace |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
429 |
def primary_view(cls, req, rset, row=None, col=None, view=None, |
0 | 430 |
**kwargs): |
431 |
if view is not None and not view.is_primary(): |
|
432 |
return 0 |
|
433 |
return 1 |
|
236
8ab9e72dd8f1
begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
230
diff
changeset
|
434 |
primaryview_selector = deprecated_function(primary_view) |
0 | 435 |
|
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
436 |
def appobject_selectable(registry, oid): |
161
328bc32b5701
repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
157
diff
changeset
|
437 |
"""return a selector that will have a positive score if an object for the |
328bc32b5701
repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
157
diff
changeset
|
438 |
given registry and object id is selectable for the input context |
328bc32b5701
repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
157
diff
changeset
|
439 |
""" |
142
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
440 |
@lltrace |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
441 |
def selector(cls, req, rset, *args, **kwargs): |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
442 |
try: |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
443 |
cls.vreg.select_object(registry, oid, req, rset, *args, **kwargs) |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
444 |
return 1 |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
445 |
except NoSelectableObject: |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
446 |
return 0 |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
447 |
return selector |
0425ee84cfa6
add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
448 |
|
0 | 449 |
|
450 |
# compound selectors ########################################################## |
|
451 |
||
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
452 |
nfentity_selector = chainall(nonempty_rset, _nfentity_selector) |
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
453 |
|
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
454 |
implement_interface = chainall(nfentity_selector, _interface_selector) |
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
455 |
interface_selector = deprecated_function(implement_interface) |
0 | 456 |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
457 |
accept = chainall(nfentity_selector, accept_rset_selector) |
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
458 |
accept_selector = deprecated_function(accept) |
0 | 459 |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
460 |
accept_one_selector = chainall(one_line_rset, accept_selector) |
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
461 |
|
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
462 |
rqlcondition_selector = chainall(nfentity_selector, one_line_rset, |
0 | 463 |
_rqlcondition_selector) |
464 |
||
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
465 |
searchstate_accept_selector = chainall(nonempty_rset, searchstate_selector, |
0 | 466 |
accept_selector) |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
467 |
searchstate_accept_one_selector = chainall(nonempty_rset, searchstate_selector, |
0 | 468 |
accept_selector, rqlcondition_selector) |
469 |
searchstate_accept_one_but_etype_selector = chainall(searchstate_accept_one_selector, |
|
470 |
but_etype_selector) |
|
471 |
||
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
472 |
#__all__ = [name for name in globals().keys() if name.endswith('selector')] |
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
236
diff
changeset
|
473 |
#__all__ += ['chainall', 'chainfirst'] |