author | sylvain.thenault@logilab.fr |
Thu, 26 Mar 2009 18:59:56 +0100 | |
branch | tls-sprint |
changeset 1148 | 55a8238f8f7c |
parent 1138 | 22f634977c95 |
child 1178 | 8d9b705168d3 |
permissions | -rw-r--r-- |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1 |
"""This file contains some basic selectors required by application objects. |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
2 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
3 |
A selector is responsible to score how well an object may be used with a |
718
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
4 |
given context by returning a score. |
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
5 |
|
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
6 |
In CubicWeb Usually the context consists for a request object, a result set |
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
7 |
or None, a specific row/col in the result set, etc... |
f7011679437a
doc update, move yes_registerer here
sylvain.thenault@logilab.fr
parents:
697
diff
changeset
|
8 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
10 |
If you have trouble with selectors, especially if the objet (typically |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
11 |
a view or a component) you want to use is not selected and you want to |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
12 |
know which one(s) of its selectors fail (e.g. returns 0), you can use |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
13 |
`traced_selection` or even direclty `TRACED_OIDS`. |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
14 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
15 |
`TRACED_OIDS` is a tuple of traced object ids. The special value |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
16 |
'all' may be used to log selectors for all objects. |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
17 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
18 |
For instance, say that the following code yields a `NoSelectableObject` |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
19 |
exception:: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
20 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
21 |
self.view('calendar', myrset) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
23 |
You can log the selectors involved for *calendar* by replacing the line |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
24 |
above by:: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
25 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
26 |
# in Python2.5 |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
27 |
from cubicweb.selectors import traced_selection |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
28 |
with traced_selection(): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
29 |
self.view('calendar', myrset) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
30 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
31 |
# in Python2.4 |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
660
diff
changeset
|
32 |
from cubicweb import selectors |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
33 |
selectors.TRACED_OIDS = ('calendar',) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
34 |
self.view('calendar', myrset) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
35 |
selectors.TRACED_OIDS = () |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
36 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
37 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
38 |
:organization: Logilab |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
39 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
40 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
41 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
42 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
43 |
__docformat__ = "restructuredtext en" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
44 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
import logging |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
46 |
from warnings import warn |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
47 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
48 |
from logilab.common.compat import all |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
from logilab.common.deprecation import deprecated_function |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
50 |
from logilab.common.interface import implements as implements_iface |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
51 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
52 |
from yams import BASE_TYPES |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
53 |
|
810
154bf8cfd65b
missing import
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
789
diff
changeset
|
54 |
from cubicweb import Unauthorized, NoSelectableObject, NotAnEntity, role |
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
55 |
from cubicweb.vregistry import (NoSelectableObject, Selector, |
1132 | 56 |
chainall, objectify_selector) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
57 |
from cubicweb.cwconfig import CubicWebConfiguration |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
58 |
from cubicweb.schema import split_expression |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
# helpers for debugging selectors |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
61 |
SELECTOR_LOGGER = logging.getLogger('cubicweb.selectors') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
62 |
TRACED_OIDS = () |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
63 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
64 |
def lltrace(selector): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
65 |
# don't wrap selectors if not in development mode |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
66 |
if CubicWebConfiguration.mode == 'installed': |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
67 |
return selector |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
68 |
def traced(cls, *args, **kwargs): |
752
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
69 |
# /!\ lltrace decorates pure function or __call__ method, this |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
70 |
# means argument order may be different |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
71 |
if isinstance(cls, Selector): |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
72 |
selname = str(cls) |
752
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
73 |
vobj = args[0] |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
74 |
else: |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
75 |
selname = selector.__name__ |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
76 |
vobj = cls |
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
77 |
oid = vobj.id |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
78 |
ret = selector(cls, *args, **kwargs) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
if TRACED_OIDS == 'all' or oid in TRACED_OIDS: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
80 |
#SELECTOR_LOGGER.warning('selector %s returned %s for %s', selname, ret, cls) |
752
c0506c4a1e6c
fix lltrace to consider __call__ and pure functions decoration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
737
diff
changeset
|
81 |
print 'selector %s returned %s for %s' % (selname, ret, vobj) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
82 |
return ret |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
traced.__name__ = selector.__name__ |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
84 |
return traced |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
85 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
class traced_selection(object): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
"""selector debugging helper. |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
Typical usage is : |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
90 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
>>> with traced_selection(): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
92 |
... # some code in which you want to debug selectors |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
93 |
... # for all objects |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
94 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
95 |
or |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
96 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
97 |
>>> with traced_selection( ('oid1', 'oid2') ): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
98 |
... # some code in which you want to debug selectors |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
99 |
... # for objects with id 'oid1' and 'oid2' |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
100 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
101 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
102 |
def __init__(self, traced='all'): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
103 |
self.traced = traced |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
104 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
105 |
def __enter__(self): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
106 |
global TRACED_OIDS |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
107 |
TRACED_OIDS = self.traced |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
108 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
109 |
def __exit__(self, exctype, exc, traceback): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
110 |
global TRACED_OIDS |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
111 |
TRACED_OIDS = () |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
112 |
return traceback is None |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
113 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
114 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
115 |
# abstract selectors ########################################################## |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
116 |
class PartialSelectorMixIn(object): |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
117 |
"""convenience mix-in for selectors that will look into the containing |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
118 |
class to find missing information. |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
119 |
|
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
120 |
cf. `cubicweb.web.action.LinkToEntityAction` for instance |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
121 |
""" |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
122 |
def __call__(self, cls, *args, **kwargs): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
123 |
self.complete(cls) |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
124 |
return super(PartialSelectorMixIn, self).__call__(cls, *args, **kwargs) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
125 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
126 |
class EClassSelector(Selector): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
127 |
"""abstract class for selectors working on the entity classes of the result |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
128 |
set. Its __call__ method has the following behaviour: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
129 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
130 |
* if row is specified, return the score returned by the score_class method |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
131 |
called with the entity class found in the specified cell |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
132 |
* else return the sum of score returned by the score_class method for each |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
133 |
entity type found in the specified column, unless: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
134 |
- `once_is_enough` is True, in which case the first non-zero score is |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
135 |
returned |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
136 |
- `once_is_enough` is False, in which case if score_class return 0, 0 is |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
137 |
returned |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
""" |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
139 |
def __init__(self, once_is_enough=False): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
140 |
self.once_is_enough = once_is_enough |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
141 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
142 |
@lltrace |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
143 |
def __call__(self, cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
144 |
if not rset: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
146 |
score = 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
147 |
if row is None: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
148 |
for etype in rset.column_types(col): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
149 |
if etype is None: # outer join |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
150 |
continue |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
151 |
escore = self.score(cls, req, etype) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
152 |
if not escore and not self.once_is_enough: |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
153 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
154 |
elif self.once_is_enough: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
155 |
return escore |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
156 |
score += escore |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
157 |
else: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
158 |
etype = rset.description[row][col] |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
159 |
if etype is not None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
160 |
score = self.score(cls, req, etype) |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
161 |
return score |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
162 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
163 |
def score(self, cls, req, etype): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
164 |
if etype in BASE_TYPES: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
165 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
166 |
return self.score_class(cls.vreg.etype_class(etype), req) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
167 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
168 |
def score_class(self, eclass, req): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
169 |
raise NotImplementedError() |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
170 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
171 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
172 |
class EntitySelector(EClassSelector): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
173 |
"""abstract class for selectors working on the entity instances of the |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
174 |
result set. Its __call__ method has the following behaviour: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
175 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
176 |
* if row is specified, return the score returned by the score_entity method |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
177 |
called with the entity instance found in the specified cell |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
178 |
* else return the sum of score returned by the score_entity method for each |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
179 |
entity found in the specified column, unless: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
180 |
- `once_is_enough` is True, in which case the first non-zero score is |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
181 |
returned |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
182 |
- `once_is_enough` is False, in which case if score_class return 0, 0 is |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
183 |
returned |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
184 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
185 |
note: None values (resulting from some outer join in the query) are not |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
186 |
considered. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
187 |
""" |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
188 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
189 |
@lltrace |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
190 |
def __call__(self, cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
191 |
if not rset: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
192 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
193 |
score = 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
194 |
if row is None: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
195 |
for row, rowvalue in enumerate(rset.rows): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
196 |
if rowvalue[col] is None: # outer join |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
197 |
continue |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
198 |
escore = self.score(req, rset, row, col) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
199 |
if not escore and not self.once_is_enough: |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
200 |
return 0 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
201 |
elif self.once_is_enough: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
202 |
return escore |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
203 |
score += escore |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
204 |
else: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
205 |
etype = rset.description[row][col] |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
206 |
if etype is not None: # outer join |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
207 |
score = self.score(req, rset, row, col) |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
208 |
return score |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
209 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
210 |
def score(self, req, rset, row, col): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
211 |
try: |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
212 |
return self.score_entity(rset.get_entity(row, col)) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
213 |
except NotAnEntity: |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
214 |
return 0 |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
215 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
216 |
def score_entity(self, entity): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
217 |
raise NotImplementedError() |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
218 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
219 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
220 |
# very basic selectors ######################################################## |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
221 |
|
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
222 |
class yes(Selector): |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
223 |
"""return arbitrary score""" |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
224 |
def __init__(self, score=1): |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
225 |
self.score = score |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
226 |
def __call__(self, *args, **kwargs): |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
227 |
return self.score |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
228 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
229 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
230 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
231 |
def none_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
232 |
"""accept no result set (e.g. given rset is None)""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
233 |
if rset is None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
234 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
235 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
236 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
237 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
238 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
239 |
def any_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
240 |
"""accept result set, whatever the number of result it contains""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
241 |
if rset is not None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
242 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
243 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
244 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
245 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
246 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
247 |
def nonempty_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
248 |
"""accept any non empty result set""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
249 |
if rset is not None and rset.rowcount: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
250 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
251 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
252 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
253 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
254 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
255 |
def empty_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
256 |
"""accept empty result set""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
257 |
if rset is not None and rset.rowcount == 0: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
258 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
259 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
260 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
261 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
262 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
263 |
def one_line_rset(cls, req, rset, row=None, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
264 |
"""if row is specified, accept result set with a single line of result, |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
265 |
else accepts anyway |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
266 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
267 |
if rset is not None and (row is not None or rset.rowcount == 1): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
268 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
269 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
270 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
271 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
272 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
273 |
def two_lines_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
274 |
"""accept result set with *at least* two lines of result""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
275 |
if rset is not None and rset.rowcount > 1: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
276 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
277 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
278 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
279 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
280 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
281 |
def two_cols_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
282 |
"""accept result set with at least one line and two columns of result""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
283 |
if rset is not None and rset.rowcount and len(rset.rows[0]) > 1: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
284 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
285 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
286 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
287 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
288 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
289 |
def paginated_rset(cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
290 |
"""accept result set with more lines than the page size. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
291 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
292 |
Page size is searched in (respecting order): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
293 |
* a page_size argument |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
294 |
* a page_size form parameters |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
295 |
* the navigation.page-size property |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
296 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
297 |
page_size = kwargs.get('page_size') |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
298 |
if page_size is None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
299 |
page_size = req.form.get('page_size') |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
300 |
if page_size is None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
301 |
page_size = req.property_value('navigation.page-size') |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
302 |
else: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
303 |
page_size = int(page_size) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
304 |
if rset is None or rset.rowcount <= page_size: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
305 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
306 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
307 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
308 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
309 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
310 |
def sorted_rset(cls, req, rset, row=None, col=0, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
311 |
"""accept sorted result set""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
312 |
rqlst = rset.syntax_tree() |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
313 |
if len(rqlst.children) > 1 or not rqlst.children[0].orderby: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
314 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
315 |
return 2 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
316 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
317 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
318 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
319 |
def one_etype_rset(cls, req, rset, row=None, col=0, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
320 |
"""accept result set where entities in the specified column (or 0) are all |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
321 |
of the same type |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
322 |
""" |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
323 |
if rset is None: |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
324 |
return 0 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
325 |
if len(rset.column_types(col)) != 1: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
326 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
327 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
328 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
329 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
330 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
331 |
def two_etypes_rset(cls, req, rset, row=None, col=0, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
332 |
"""accept result set where entities in the specified column (or 0) are not |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
333 |
of the same type |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
334 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
335 |
if rset: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
336 |
etypes = rset.column_types(col) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
337 |
if len(etypes) > 1: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
338 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
339 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
340 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
341 |
class non_final_entity(EClassSelector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
342 |
"""accept if entity type found in the result set is non final. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
343 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
344 |
See `EClassSelector` documentation for behaviour when row is not specified. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
345 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
346 |
def score(self, cls, req, etype): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
347 |
if etype in BASE_TYPES: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
348 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
349 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
350 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
351 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
352 |
@lltrace |
828
394927376a01
two use cases for the 'not' selector
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
810
diff
changeset
|
353 |
def authenticated_user(cls, req, *args, **kwargs): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
354 |
"""accept if user is anonymous""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
355 |
if req.cnx.anonymous_connection: |
828
394927376a01
two use cases for the 'not' selector
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
810
diff
changeset
|
356 |
return 0 |
394927376a01
two use cases for the 'not' selector
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
810
diff
changeset
|
357 |
return 1 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
358 |
|
828
394927376a01
two use cases for the 'not' selector
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
810
diff
changeset
|
359 |
def anonymous_user(): |
394927376a01
two use cases for the 'not' selector
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
810
diff
changeset
|
360 |
return ~ authenticated_user() |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
361 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
362 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
363 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
364 |
def primary_view(cls, req, rset, row=None, col=0, view=None, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
365 |
"""accept if view given as named argument is a primary view, or if no view |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
366 |
is given |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
367 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
368 |
if view is not None and not view.is_primary(): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
369 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
370 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
371 |
|
697
06807984e610
provide objectify_selector decorator for very simple selectors
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
696
diff
changeset
|
372 |
@objectify_selector |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
373 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
374 |
def match_context_prop(cls, req, rset, row=None, col=0, context=None, |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
375 |
**kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
376 |
"""accept if: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
377 |
* no context given |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
378 |
* context (`basestring`) is matching the context property value for the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
379 |
given cls |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
380 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
381 |
propval = req.property_value('%s.%s.context' % (cls.__registry__, cls.id)) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
382 |
if not propval: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
383 |
propval = cls.context |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
384 |
if context is not None and propval and context != propval: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
385 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
386 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
387 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
388 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
389 |
class match_search_state(Selector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
390 |
"""accept if the current request search state is in one of the expected |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
391 |
states given to the initializer |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
392 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
393 |
:param expected: either 'normal' or 'linksearch' (eg searching for an |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
394 |
object to create a relation with another) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
395 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
396 |
def __init__(self, *expected): |
774
48cb1f42e79c
tell on which class the assertion's failed
sylvain.thenault@logilab.fr
parents:
770
diff
changeset
|
397 |
assert expected, self |
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
398 |
self.expected = frozenset(expected) |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
399 |
|
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
400 |
def __str__(self): |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
401 |
return '%s(%s)' % (self.__class__.__name__, |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
402 |
','.join(sorted(str(s) for s in self.expected))) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
403 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
404 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
405 |
def __call__(self, cls, req, rset, row=None, col=0, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
406 |
try: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
407 |
if not req.search_state[0] in self.expected: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
408 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
409 |
except AttributeError: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
410 |
return 1 # class doesn't care about search state, accept it |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
411 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
412 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
413 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
414 |
class match_form_params(match_search_state): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
415 |
"""accept if parameters specified as initializer arguments are specified |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
416 |
in request's form parameters |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
417 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
418 |
:param *expected: parameters (eg `basestring`) which are expected to be |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
419 |
found in request's form parameters |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
420 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
421 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
422 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
423 |
def __call__(self, cls, req, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
424 |
score = 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
425 |
for param in self.expected: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
426 |
val = req.form.get(param) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
427 |
if not val: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
428 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
429 |
score += 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
430 |
return len(self.expected) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
431 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
432 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
433 |
class match_kwargs(match_search_state): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
434 |
"""accept if parameters specified as initializer arguments are specified |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
435 |
in named arguments given to the selector |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
436 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
437 |
:param *expected: parameters (eg `basestring`) which are expected to be |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
438 |
found in named arguments (kwargs) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
439 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
440 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
441 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
442 |
def __call__(self, cls, req, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
443 |
for arg in self.expected: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
444 |
if not arg in kwargs: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
445 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
446 |
return len(self.expected) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
447 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
448 |
|
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
449 |
class match_user_groups(match_search_state): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
450 |
"""accept if logged users is in at least one of the given groups. Returned |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
451 |
score is the number of groups in which the user is. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
452 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
453 |
If the special 'owners' group is given: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
454 |
* if row is specified check the entity at the given row/col is owned by the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
455 |
logged user |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
456 |
* if row is not specified check all entities in col are owned by the logged |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
457 |
user |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
458 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
459 |
:param *required_groups: name of groups (`basestring`) in which the logged |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
460 |
user should be |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
461 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
462 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
463 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
464 |
def __call__(self, cls, req, rset=None, row=None, col=0, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
465 |
user = req.user |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
466 |
if user is None: |
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
467 |
return int('guests' in self.expected) |
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
468 |
score = user.matching_groups(self.expected) |
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
469 |
if not score and 'owners' in self.expected and rset: |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
470 |
if row is not None: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
471 |
if not user.owns(rset[row][col]): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
472 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
473 |
score = 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
474 |
else: |
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
475 |
score = all(user.owns(r[col]) for r in rset) |
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
476 |
return score |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
477 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
478 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
479 |
class appobject_selectable(Selector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
480 |
"""accept with another appobject is selectable using selector's input |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
481 |
context. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
482 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
483 |
:param registry: a registry name (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
484 |
:param oid: an object identifier (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
485 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
486 |
def __init__(self, registry, oid): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
487 |
self.registry = registry |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
488 |
self.oid = oid |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
489 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
490 |
def __call__(self, cls, req, rset, *args, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
491 |
try: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
492 |
cls.vreg.select_object(self.registry, self.oid, req, rset, *args, **kwargs) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
493 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
494 |
except NoSelectableObject: |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
495 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
496 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
497 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
498 |
# not so basic selectors ###################################################### |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
499 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
500 |
class implements(EClassSelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
501 |
"""accept if entity class found in the result set implements at least one |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
502 |
of the interfaces given as argument. Returned score is the number of |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
503 |
implemented interfaces. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
504 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
505 |
See `EClassSelector` documentation for behaviour when row is not specified. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
506 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
507 |
:param *expected_ifaces: expected interfaces. An interface may be a class |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
508 |
or an entity type (e.g. `basestring`) in which case |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
509 |
the associated class will be searched in the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
510 |
registry (at selection time) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
511 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
512 |
note: when interface is an entity class, the score will reflect class |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
513 |
proximity so the most specific object'll be selected |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
514 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
515 |
def __init__(self, *expected_ifaces): |
689 | 516 |
super(implements, self).__init__() |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
517 |
self.expected_ifaces = expected_ifaces |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
518 |
|
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
519 |
def __str__(self): |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
520 |
return '%s(%s)' % (self.__class__.__name__, |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
521 |
','.join(str(s) for s in self.expected_ifaces)) |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
522 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
523 |
def score_class(self, eclass, req): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
524 |
score = 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
525 |
for iface in self.expected_ifaces: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
526 |
if isinstance(iface, basestring): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
527 |
# entity type |
1037
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1011
diff
changeset
|
528 |
try: |
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1011
diff
changeset
|
529 |
iface = eclass.vreg.etype_class(iface) |
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1011
diff
changeset
|
530 |
except KeyError: |
1f3fae8d82b2
don't fail if vobjects reference an unexistant class
sylvain.thenault@logilab.fr
parents:
1011
diff
changeset
|
531 |
continue # entity type not in the schema |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
532 |
if implements_iface(eclass, iface): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
533 |
if getattr(iface, '__registry__', None) == 'etypes': |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
534 |
# adjust score if the interface is an entity class |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
535 |
if iface is eclass: |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
536 |
score += len(eclass.e_schema.ancestors()) + 4 |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
537 |
else: |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
538 |
parents = [e.type for e in eclass.e_schema.ancestors()] |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
539 |
for index, etype in enumerate(reversed(parents)): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
540 |
basecls = eclass.vreg.etype_class(etype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
541 |
if iface is basecls: |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
542 |
score += index + 3 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
543 |
break |
779
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
544 |
else: # Any |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
545 |
score += 1 |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
546 |
else: |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
547 |
# implenting an interface takes precedence other special Any |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
548 |
# interface |
8510e14335e1
implements some str, fix implements selector, test and fixes
sylvain.thenault@logilab.fr
parents:
774
diff
changeset
|
549 |
score += 2 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
550 |
return score |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
551 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
552 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
553 |
class specified_etype_implements(implements): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
554 |
"""accept if entity class specified using an 'etype' parameters in name |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
555 |
argument or request form implements at least one of the interfaces given as |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
556 |
argument. Returned score is the number of implemented interfaces. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
557 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
558 |
:param *expected_ifaces: expected interfaces. An interface may be a class |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
559 |
or an entity type (e.g. `basestring`) in which case |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
560 |
the associated class will be searched in the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
561 |
registry (at selection time) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
562 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
563 |
note: when interface is an entity class, the score will reflect class |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
564 |
proximity so the most specific object'll be selected |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
565 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
566 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
567 |
@lltrace |
789 | 568 |
def __call__(self, cls, req, *args, **kwargs): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
569 |
try: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
570 |
etype = req.form['etype'] |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
571 |
except KeyError: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
572 |
try: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
573 |
etype = kwargs['etype'] |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
574 |
except KeyError: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
575 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
576 |
return self.score_class(cls.vreg.etype_class(etype), req) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
577 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
578 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
579 |
class relation_possible(EClassSelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
580 |
"""accept if entity class found in the result set support the relation. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
581 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
582 |
See `EClassSelector` documentation for behaviour when row is not specified. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
583 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
584 |
:param rtype: a relation type (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
585 |
:param role: the role of the result set entity in the relation. 'subject' or |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
586 |
'object', default to 'subject'. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
587 |
:param target_type: if specified, check the relation's end may be of this |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
588 |
target type (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
589 |
:param action: a relation schema action (one of 'read', 'add', 'delete') |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
590 |
which must be granted to the logged user, else a 0 score will |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
591 |
be returned |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
592 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
593 |
def __init__(self, rtype, role='subject', target_etype=None, |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
594 |
action='read', once_is_enough=False): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
595 |
super(relation_possible, self).__init__(once_is_enough) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
596 |
self.rtype = rtype |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
597 |
self.role = role |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
598 |
self.target_etype = target_etype |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
599 |
self.action = action |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
600 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
601 |
@lltrace |
657
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
602 |
def __call__(self, cls, req, *args, **kwargs): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
603 |
rschema = cls.schema.rschema(self.rtype) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
604 |
if not (rschema.has_perm(req, self.action) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
605 |
or rschema.has_local_role(self.action)): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
606 |
return 0 |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
607 |
score = super(relation_possible, self).__call__(cls, req, *args, **kwargs) |
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
608 |
return score |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
609 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
610 |
def score_class(self, eclass, req): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
611 |
eschema = eclass.e_schema |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
612 |
try: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
613 |
if self.role == 'object': |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
614 |
rschema = eschema.object_relation(self.rtype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
615 |
else: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
616 |
rschema = eschema.subject_relation(self.rtype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
617 |
except KeyError: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
618 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
619 |
if self.target_etype is not None: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
620 |
try: |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
621 |
if self.role == 'subject': |
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
622 |
return int(self.target_etype in rschema.objects(eschema)) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
623 |
else: |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
624 |
return int(self.target_etype in rschema.subjects(eschema)) |
1132 | 625 |
except KeyError: |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
626 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
627 |
return 1 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
628 |
|
876
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
629 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
630 |
class partial_relation_possible(PartialSelectorMixIn, relation_possible): |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
631 |
"""partial version of the relation_possible selector |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
632 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
633 |
The selector will look for class attributes to find its missing |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
634 |
information. The list of attributes required on the class |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
635 |
for this selector are: |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
636 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
637 |
- `rtype`: same as `rtype` parameter of the `relation_possible` selector |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
638 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
639 |
- `role`: this attribute will be passed to the `cubicweb.role` function |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
640 |
to determine the role of class in the relation |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
641 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
642 |
- `etype` (optional): the entity type on the other side of the relation |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
643 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
644 |
:param action: a relation schema action (one of 'read', 'add', 'delete') |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
645 |
which must be granted to the logged user, else a 0 score will |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
646 |
be returned |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
647 |
""" |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
648 |
def __init__(self, action='read', once_is_enough=False): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
649 |
super(partial_relation_possible, self).__init__(None, None, None, |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
650 |
action, once_is_enough) |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
651 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
652 |
def complete(self, cls): |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
653 |
self.rtype = cls.rtype |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
654 |
self.role = role(cls) |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
655 |
self.target_etype = getattr(cls, 'etype', None) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
656 |
|
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
657 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
658 |
class has_editable_relation(EntitySelector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
659 |
"""accept if some relations for an entity found in the result set is |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
660 |
editable by the logged user. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
661 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
662 |
See `EntitySelector` documentation for behaviour when row is not specified. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
663 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
664 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
665 |
def score_entity(self, entity): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
666 |
# if user has no update right but it can modify some relation, |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
667 |
# display action anyway |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
668 |
for dummy in entity.srelations_by_category(('generic', 'metadata'), |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
669 |
'add'): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
670 |
return 1 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
671 |
for rschema, targetschemas, role in entity.relations_by_category( |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
672 |
('primary', 'secondary'), 'add'): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
673 |
if not rschema.is_final(): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
674 |
return 1 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
675 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
676 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
677 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
678 |
class may_add_relation(EntitySelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
679 |
"""accept if the relation can be added to an entity found in the result set |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
680 |
by the logged user. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
681 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
682 |
See `EntitySelector` documentation for behaviour when row is not specified. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
683 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
684 |
:param rtype: a relation type (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
685 |
:param role: the role of the result set entity in the relation. 'subject' or |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
686 |
'object', default to 'subject'. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
687 |
""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
688 |
|
770
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
689 |
def __init__(self, rtype, role='subject', once_is_enough=False): |
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
690 |
super(may_add_relation, self).__init__(once_is_enough) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
691 |
self.rtype = rtype |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
692 |
self.role = role |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
693 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
694 |
def score_entity(self, entity): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
695 |
rschema = entity.schema.rschema(self.rtype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
696 |
if self.role == 'subject': |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
697 |
if not rschema.has_perm(entity.req, 'add', fromeid=entity.eid): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
698 |
return 0 |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
699 |
elif not rschema.has_perm(entity.req, 'add', toeid=entity.eid): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
700 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
701 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
702 |
|
876
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
703 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
704 |
class partial_may_add_relation(PartialSelectorMixIn, may_add_relation): |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
705 |
"""partial version of the may_add_relation selector |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
706 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
707 |
The selector will look for class attributes to find its missing |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
708 |
information. The list of attributes required on the class |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
709 |
for this selector are: |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
710 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
711 |
- `rtype`: same as `rtype` parameter of the `relation_possible` selector |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
712 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
713 |
- `role`: this attribute will be passed to the `cubicweb.role` function |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
714 |
to determine the role of class in the relation. |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
715 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
716 |
:param action: a relation schema action (one of 'read', 'add', 'delete') |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
717 |
which must be granted to the logged user, else a 0 score will |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
718 |
be returned |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
719 |
""" |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
720 |
def __init__(self, once_is_enough=False): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
721 |
super(partial_may_add_relation, self).__init__(None, None, once_is_enough) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
722 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
723 |
def complete(self, cls): |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
724 |
self.rtype = cls.rtype |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
725 |
self.role = role(cls) |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
726 |
|
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
727 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
728 |
class has_related_entities(EntitySelector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
729 |
"""accept if entity found in the result set has some linked entities using |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
730 |
the specified relation (optionaly filtered according to the specified target |
876
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
731 |
type). Checks first if the relation is possible. |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
732 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
733 |
See `EntitySelector` documentation for behaviour when row is not specified. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
734 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
735 |
:param rtype: a relation type (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
736 |
:param role: the role of the result set entity in the relation. 'subject' or |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
737 |
'object', default to 'subject'. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
738 |
:param target_type: if specified, check the relation's end may be of this |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
739 |
target type (`basestring`) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
740 |
""" |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
741 |
def __init__(self, rtype, role='subject', target_etype=None, |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
742 |
once_is_enough=False): |
770
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
743 |
super(has_related_entities, self).__init__(once_is_enough) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
744 |
self.rtype = rtype |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
745 |
self.role = role |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
746 |
self.target_etype = target_etype |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
747 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
748 |
def score_entity(self, entity): |
876
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
749 |
relpossel = relation_possible(self.rtype, self.role, self.target_etype) |
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
750 |
if not relpossel.score_class(entity.__class__, entity.req): |
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
751 |
return 0 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
752 |
rset = entity.related(self.rtype, self.role) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
753 |
if self.target_etype: |
1011
22812cfe36b4
fix w/ rset with more than one column
sylvain.thenault@logilab.fr
parents:
876
diff
changeset
|
754 |
return any(r for r in rset.description if r[0] == self.target_etype) |
835
7dcb11dd443e
fix relation_possible, ensure we return int
sylvain.thenault@logilab.fr
parents:
833
diff
changeset
|
755 |
return rset and 1 or 0 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
756 |
|
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
757 |
|
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
758 |
class partial_has_related_entities(PartialSelectorMixIn, has_related_entities): |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
759 |
"""partial version of the has_related_entities selector |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
760 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
761 |
The selector will look for class attributes to find its missing |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
762 |
information. The list of attributes required on the class |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
763 |
for this selector are: |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
764 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
765 |
- `rtype`: same as `rtype` parameter of the `relation_possible` selector |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
766 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
767 |
- `role`: this attribute will be passed to the `cubicweb.role` function |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
768 |
to determine the role of class in the relation. |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
769 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
770 |
- `etype` (optional): the entity type on the other side of the relation |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
771 |
|
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
772 |
:param action: a relation schema action (one of 'read', 'add', 'delete') |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
773 |
which must be granted to the logged user, else a 0 score will |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
774 |
be returned |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
775 |
""" |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
776 |
def __init__(self, once_is_enough=False): |
838
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
777 |
super(partial_has_related_entities, self).__init__(None, None, |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
778 |
None, once_is_enough) |
f2c56312b03a
rename abstract_* selectors into partial_* + add docstrings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
835
diff
changeset
|
779 |
def complete(self, cls): |
782
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
780 |
self.rtype = cls.rtype |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
781 |
self.role = role(cls) |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
782 |
self.target_etype = getattr(cls, 'etype', None) |
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
783 |
|
01801a10c567
introduce abstract selectors to get rid of the my_selector() contagion
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
774
diff
changeset
|
784 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
785 |
class has_permission(EntitySelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
786 |
"""accept if user has the permission to do the requested action on a result |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
787 |
set entity. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
788 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
789 |
* if row is specified, return 1 if user has the permission on the entity |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
790 |
instance found in the specified cell |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
791 |
* else return a positive score if user has the permission for every entity |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
792 |
in the found in the specified column |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
793 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
794 |
note: None values (resulting from some outer join in the query) are not |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
795 |
considered. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
796 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
797 |
:param action: an entity schema action (eg 'read'/'add'/'delete'/'update') |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
798 |
""" |
770
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
799 |
def __init__(self, action, once_is_enough=False): |
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
800 |
super(has_permission, self).__init__(once_is_enough) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
801 |
self.action = action |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
802 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
803 |
@lltrace |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
804 |
def __call__(self, cls, req, rset, row=None, col=0, **kwargs): |
788 | 805 |
if rset is None: |
806 |
return 0 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
807 |
user = req.user |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
808 |
action = self.action |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
809 |
if row is None: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
810 |
score = 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
811 |
need_local_check = [] |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
812 |
geteschema = cls.schema.eschema |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
813 |
for etype in rset.column_types(0): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
814 |
if etype in BASE_TYPES: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
815 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
816 |
eschema = geteschema(etype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
817 |
if not user.matching_groups(eschema.get_groups(action)): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
818 |
if eschema.has_local_role(action): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
819 |
# have to ckeck local roles |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
820 |
need_local_check.append(eschema) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
821 |
continue |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
822 |
else: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
823 |
# even a local role won't be enough |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
824 |
return 0 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
825 |
score += 1 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
826 |
if need_local_check: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
827 |
# check local role for entities of necessary types |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
828 |
for i, row in enumerate(rset): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
829 |
if not rset.description[i][0] in need_local_check: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
830 |
continue |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
831 |
if not self.score(req, rset, i, col): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
832 |
return 0 |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
833 |
score += 1 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
834 |
return score |
876
f652660ae9a6
use relation_possible from within has_relation, fix another bug
sylvain.thenault@logilab.fr
parents:
839
diff
changeset
|
835 |
return self.score(req, rset, row, col) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
836 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
837 |
def score_entity(self, entity): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
838 |
if entity.has_perm(self.action): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
839 |
return 1 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
840 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
841 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
842 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
843 |
class has_add_permission(EClassSelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
844 |
"""accept if logged user has the add permission on entity class found in the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
845 |
result set, and class is not a strict subobject. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
846 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
847 |
See `EClassSelector` documentation for behaviour when row is not specified. |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
848 |
""" |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
849 |
def score(self, cls, req, etype): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
850 |
eschema = cls.schema.eschema(etype) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
851 |
if not (eschema.is_final() or eschema.is_subobject(strict=True)) \ |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
852 |
and eschema.has_perm(req, 'add'): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
853 |
return 1 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
854 |
return 0 |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
855 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
856 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
857 |
class rql_condition(EntitySelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
858 |
"""accept if an arbitrary rql return some results for an eid found in the |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
859 |
result set. Returned score is the number of items returned by the rql |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
860 |
condition. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
861 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
862 |
See `EntitySelector` documentation for behaviour when row is not specified. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
863 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
864 |
:param expression: basestring containing an rql expression, which should use |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
865 |
X variable to represent the context entity and may use U |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
866 |
to represent the logged user |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
867 |
|
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
868 |
return the sum of the number of items returned by the rql condition as score |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
869 |
or 0 at the first entity scoring to zero. |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
870 |
""" |
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
871 |
def __init__(self, expression, once_is_enough=False): |
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
872 |
super(rql_condition, self).__init__(once_is_enough) |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
873 |
if 'U' in frozenset(split_expression(expression)): |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
874 |
rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % expression |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
875 |
else: |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
876 |
rql = 'Any X WHERE X eid %%(x)s, %s' % expression |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
877 |
self.rql = rql |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
878 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
879 |
def score(self, req, rset, row, col): |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
880 |
try: |
764 | 881 |
return len(req.execute(self.rql, {'x': rset[row][col], |
882 |
'u': req.user.eid}, 'x')) |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
883 |
except Unauthorized: |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
884 |
return 0 |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
885 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
886 |
|
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
887 |
class but_etype(EntitySelector): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
888 |
"""accept if the given entity types are not found in the result set. |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
889 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
890 |
See `EntitySelector` documentation for behaviour when row is not specified. |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
891 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
892 |
:param *etypes: entity types (`basestring`) which should be refused |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
893 |
""" |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
894 |
def __init__(self, *etypes): |
770
193b7e981ea9
each subclass of EntitySelector should call its __init__ method
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
769
diff
changeset
|
895 |
super(but_etype, self).__init__() |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
896 |
self.but_etypes = etypes |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
897 |
|
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
898 |
def score(self, req, rset, row, col): |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
899 |
if rset.description[row][col] in self.but_etypes: |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
900 |
return 0 |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
901 |
return 1 |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
902 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
903 |
|
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
904 |
class score_entity(EntitySelector): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
905 |
"""accept if some arbitrary function return a positive score for an entity |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
906 |
found in the result set. |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
907 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
908 |
See `EntitySelector` documentation for behaviour when row is not specified. |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
909 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
910 |
:param scorefunc: callable expected to take an entity as argument and to |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
911 |
return a score >= 0 |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
912 |
""" |
764 | 913 |
def __init__(self, scorefunc, once_is_enough=False): |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
914 |
super(score_entity, self).__init__(once_is_enough) |
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
915 |
self.score_entity = scorefunc |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
916 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
917 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
918 |
# XXX DEPRECATED ############################################################## |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
919 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
920 |
yes_selector = deprecated_function(yes) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
921 |
norset_selector = deprecated_function(none_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
922 |
rset_selector = deprecated_function(any_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
923 |
anyrset_selector = deprecated_function(nonempty_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
924 |
emptyrset_selector = deprecated_function(empty_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
925 |
onelinerset_selector = deprecated_function(one_line_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
926 |
twolinerset_selector = deprecated_function(two_lines_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
927 |
twocolrset_selector = deprecated_function(two_cols_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
928 |
largerset_selector = deprecated_function(paginated_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
929 |
sortedrset_selector = deprecated_function(sorted_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
930 |
oneetyperset_selector = deprecated_function(one_etype_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
931 |
multitype_selector = deprecated_function(two_etypes_rset) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
932 |
anonymous_selector = deprecated_function(anonymous_user) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
933 |
not_anonymous_selector = deprecated_function(authenticated_user) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
934 |
primaryview_selector = deprecated_function(primary_view) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
935 |
contextprop_selector = deprecated_function(match_context_prop) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
936 |
|
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
937 |
def nfentity_selector(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
938 |
return non_final_entity()(cls, req, rset, row, col) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
939 |
nfentity_selector = deprecated_function(nfentity_selector) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
940 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
941 |
def implement_interface(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
942 |
return implements(*cls.accepts_interfaces)(cls, req, rset, row, col) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
943 |
_interface_selector = deprecated_function(implement_interface) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
944 |
interface_selector = deprecated_function(implement_interface) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
945 |
implement_interface = deprecated_function(implement_interface, 'use implements') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
946 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
947 |
def accept_etype(cls, req, *args, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
948 |
"""check etype presence in request form *and* accepts conformance""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
949 |
return specified_etype_implements(*cls.accepts)(cls, req, *args) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
950 |
etype_form_selector = deprecated_function(accept_etype) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
951 |
accept_etype = deprecated_function(accept_etype, 'use specified_etype_implements') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
952 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
953 |
def searchstate_selector(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
954 |
return match_search_state(cls.search_states)(cls, req, rset, row, col) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
955 |
searchstate_selector = deprecated_function(searchstate_selector) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
956 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
957 |
def match_user_group(cls, req, rset=None, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
958 |
return match_user_groups(*cls.require_groups)(cls, req, rset, row, col, **kwargs) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
959 |
in_group_selector = deprecated_function(match_user_group) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
960 |
match_user_group = deprecated_function(match_user_group) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
961 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
962 |
def has_relation(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
963 |
return relation_possible(cls.rtype, role(cls), cls.etype, |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
964 |
getattr(cls, 'require_permission', 'read'))(cls, req, rset, row, col, **kwargs) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
965 |
has_relation = deprecated_function(has_relation) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
966 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
967 |
def one_has_relation(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
968 |
return relation_possible(cls.rtype, role(cls), cls.etype, |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
969 |
getattr(cls, 'require_permission', 'read', |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
970 |
once_is_enough=True))(cls, req, rset, row, col, **kwargs) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
971 |
one_has_relation = deprecated_function(one_has_relation, 'use relation_possible selector') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
972 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
973 |
def accept_rset(cls, req, rset, row=None, col=0, **kwargs): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
974 |
"""simply delegate to cls.accept_rset method""" |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
975 |
return implements(*cls.accepts)(cls, req, rset, row=row, col=col) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
976 |
accept_rset_selector = deprecated_function(accept_rset) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
977 |
accept_rset = deprecated_function(accept_rset, 'use implements selector') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
978 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
979 |
accept = chainall(non_final_entity(), accept_rset, name='accept') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
980 |
accept_selector = deprecated_function(accept) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
981 |
accept = deprecated_function(accept, 'use implements selector') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
982 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
983 |
accept_one = deprecated_function(chainall(one_line_rset, accept, |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
984 |
name='accept_one')) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
985 |
accept_one_selector = deprecated_function(accept_one) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
986 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
987 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
988 |
def _rql_condition(cls, req, rset, row=None, col=0, **kwargs): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
989 |
if cls.condition: |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
990 |
return rql_condition(cls.condition)(cls, req, rset, row, col) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
991 |
return 1 |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
992 |
_rqlcondition_selector = deprecated_function(_rql_condition) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
993 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
994 |
rqlcondition_selector = deprecated_function(chainall(non_final_entity(), one_line_rset, _rql_condition, |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
995 |
name='rql_condition')) |
649
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
996 |
|
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
997 |
def but_etype_selector(cls, req, rset, row=None, col=0, **kwargs): |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
998 |
return but_etype(cls.etype)(cls, req, rset, row, col) |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
999 |
but_etype_selector = deprecated_function(but_etype_selector) |
e5956e9ebef1
more doc, upgrade but_etype and appobject_selectable selectors
sylvain.thenault@logilab.fr
parents:
640
diff
changeset
|
1000 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1001 |
@lltrace |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1002 |
def etype_rtype_selector(cls, req, rset, row=None, col=0, **kwargs): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1003 |
schema = cls.schema |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1004 |
perm = getattr(cls, 'require_permission', 'read') |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1005 |
if hasattr(cls, 'etype'): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1006 |
eschema = schema.eschema(cls.etype) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1007 |
if not (eschema.has_perm(req, perm) or eschema.has_local_role(perm)): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1008 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1009 |
if hasattr(cls, 'rtype'): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1010 |
rschema = schema.rschema(cls.rtype) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1011 |
if not (rschema.has_perm(req, perm) or rschema.has_local_role(perm)): |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1012 |
return 0 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1013 |
return 1 |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1014 |
etype_rtype_selector = deprecated_function(etype_rtype_selector) |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1015 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1016 |
#req_form_params_selector = deprecated_function(match_form_params) # form_params |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1017 |
#kwargs_selector = deprecated_function(match_kwargs) # expected_kwargs |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1018 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1019 |
# compound selectors ########################################################## |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1020 |
|
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
1021 |
searchstate_accept = chainall(nonempty_rset(), accept, |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1022 |
name='searchstate_accept') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1023 |
searchstate_accept_selector = deprecated_function(searchstate_accept) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1024 |
|
766
33ede72b22b8
fix match_user_groups, appobject_selectable, searchstate_accept* compat selectors
sylvain.thenault@logilab.fr
parents:
764
diff
changeset
|
1025 |
searchstate_accept_one = chainall(one_line_rset, accept, _rql_condition, |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1026 |
name='searchstate_accept_one') |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1027 |
searchstate_accept_one_selector = deprecated_function(searchstate_accept_one) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1028 |
|
650
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1029 |
searchstate_accept = deprecated_function(searchstate_accept) |
75b4c661f71b
proper documentation, some bug fixes, upgrade some selectors
sylvain.thenault@logilab.fr
parents:
649
diff
changeset
|
1030 |
searchstate_accept_one = deprecated_function(searchstate_accept_one) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1031 |
|
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1032 |
|
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1033 |
def unbind_method(selector): |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1034 |
def new_selector(registered): |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1035 |
# get the unbound method |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1036 |
if hasattr(registered, 'im_func'): |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1037 |
registered = registered.im_func |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1038 |
# don't rebind since it will be done automatically during |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1039 |
# the assignment, inside the destination class body |
657
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1040 |
return selector(registered) |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1041 |
new_selector.__name__ = selector.__name__ |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1042 |
return new_selector |
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1043 |
|
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1044 |
|
657
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1045 |
def deprecate(registered, msg): |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1046 |
# get the unbound method |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1047 |
if hasattr(registered, 'im_func'): |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1048 |
registered = registered.im_func |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1049 |
def _deprecate(cls, vreg): |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1050 |
warn(msg, DeprecationWarning) |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1051 |
return registered(cls, vreg) |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1052 |
return _deprecate |
fd019f41aa2f
work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
656
diff
changeset
|
1053 |
|
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1054 |
@unbind_method |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1055 |
def require_group_compat(registered): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1056 |
def plug_selector(cls, vreg): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1057 |
cls = registered(cls, vreg) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1058 |
if getattr(cls, 'require_groups', None): |
654
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1059 |
warn('use "match_user_groups(group1, group2)" instead of using require_groups', |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1060 |
DeprecationWarning) |
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1061 |
cls.__select__ &= match_user_groups(cls.require_groups) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1062 |
return cls |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1063 |
return plug_selector |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1064 |
|
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1065 |
@unbind_method |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1066 |
def accepts_compat(registered): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1067 |
def plug_selector(cls, vreg): |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1068 |
cls = registered(cls, vreg) |
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1069 |
if getattr(cls, 'accepts', None): |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1070 |
warn('use "implements("EntityType", IFace)" instead of using accepts', |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1071 |
DeprecationWarning) |
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1072 |
cls.__select__ &= implements(*cls.accepts) |
634
0badd061ce0f
move cubicweb.common.selectors to cubicweb.selectors
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1073 |
return cls |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1074 |
return plug_selector |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1075 |
|
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1076 |
@unbind_method |
833
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1077 |
def accepts_etype_compat(registered): |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1078 |
def plug_selector(cls, vreg): |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1079 |
cls = registered(cls, vreg) |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1080 |
if getattr(cls, 'accepts', None): |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1081 |
warn('use "specified_etype_implements("EntityType", IFace)" instead of using accepts', |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1082 |
DeprecationWarning) |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1083 |
cls.__select__ &= specified_etype_implements(*cls.accepts) |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1084 |
return cls |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1085 |
return plug_selector |
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1086 |
|
8c6bfd9158fb
bw compat for selection for creation forms
sylvain.thenault@logilab.fr
parents:
828
diff
changeset
|
1087 |
@unbind_method |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1088 |
def condition_compat(registered): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1089 |
def plug_selector(cls, vreg): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1090 |
cls = registered(cls, vreg) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1091 |
if getattr(cls, 'condition', None): |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1092 |
warn('use "use rql_condition(expression)" instead of using condition', |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1093 |
DeprecationWarning) |
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1094 |
cls.__select__ &= rql_condition(cls.condition) |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
634
diff
changeset
|
1095 |
return cls |
652
603c782dc092
various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
650
diff
changeset
|
1096 |
return plug_selector |
654
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1097 |
|
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1098 |
@unbind_method |
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1099 |
def has_relation_compat(registered): |
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1100 |
def plug_selector(cls, vreg): |
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1101 |
cls = registered(cls, vreg) |
837 | 1102 |
if getattr(cls, 'etype', None): |
654
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1103 |
warn('use relation_possible selector instead of using etype_rtype', |
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1104 |
DeprecationWarning) |
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1105 |
cls.__select__ &= relation_possible(cls.rtype, role(cls), |
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1106 |
getattr(cls, 'etype', None), |
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
723
diff
changeset
|
1107 |
action=getattr(cls, 'require_permission', 'read')) |
654
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1108 |
return cls |
36e87179e91d
has_relation selector compat, fix some deprecation msg
sylvain.thenault@logilab.fr
parents:
652
diff
changeset
|
1109 |
return plug_selector |