author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 18:21:37 +0200 | |
branch | stable |
changeset 3442 | 1df034d5b6ec |
parent 3111 | 7b405bb305ab |
child 3163 | edfe43ceaa35 |
child 3601 | d77025be6f06 |
permissions | -rw-r--r-- |
0 | 1 |
"""CubicWeb is a generic framework to quickly build applications which describes |
2 |
relations between entitites. |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1954
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1030
2ead2111cfab
update license, specify which the version of LGPL under which the code is provided
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1022
diff
changeset
|
7 |
:license: Library General Public License version 2 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
from cubicweb.__pkginfo__ import version as __version__ |
|
11 |
||
12 |
import __builtin__ |
|
13 |
# '_' is available in builtins to mark internationalized string but should |
|
14 |
# not be used to do the actual translation |
|
15 |
if not hasattr(__builtin__, '_'): |
|
16 |
__builtin__._ = unicode |
|
17 |
||
18 |
CW_SOFTWARE_ROOT = __path__[0] |
|
19 |
||
20 |
import sys, os, logging |
|
21 |
from StringIO import StringIO |
|
22 |
from urllib import quote as urlquote, unquote as urlunquote |
|
23 |
||
24 |
from logilab.common.decorators import cached |
|
1741 | 25 |
from logilab.common.logging_ext import set_log_methods |
0 | 26 |
|
27 |
if os.environ.get('APYCOT_ROOT'): |
|
28 |
logging.basicConfig(level=logging.CRITICAL) |
|
29 |
else: |
|
30 |
logging.basicConfig() |
|
31 |
||
32 |
||
33 |
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb')) |
|
34 |
||
35 |
# make all exceptions accessible from the package |
|
36 |
from cubicweb._exceptions import * |
|
37 |
||
38 |
# convert eid to the right type, raise ValueError if it's not a valid eid |
|
39 |
typed_eid = int |
|
40 |
||
41 |
||
42 |
#def log_thread(f, w, a): |
|
43 |
# print f.f_code.co_filename, f.f_code.co_name |
|
44 |
#import threading |
|
45 |
#threading.settrace(log_thread) |
|
46 |
||
47 |
class Binary(StringIO): |
|
48 |
"""customize StringIO to make sure we don't use unicode""" |
|
1954 | 49 |
def __init__(self, buf=''): |
0 | 50 |
assert isinstance(buf, (str, buffer)), \ |
51 |
"Binary objects must use raw strings, not %s" % buf.__class__ |
|
52 |
StringIO.__init__(self, buf) |
|
53 |
||
54 |
def write(self, data): |
|
55 |
assert isinstance(data, (str, buffer)), \ |
|
56 |
"Binary objects must use raw strings, not %s" % data.__class__ |
|
57 |
StringIO.write(self, data) |
|
58 |
||
59 |
||
60 |
class RequestSessionMixIn(object): |
|
61 |
"""mixin class containing stuff shared by server session and web request |
|
62 |
""" |
|
63 |
def __init__(self, vreg): |
|
64 |
self.vreg = vreg |
|
65 |
try: |
|
66 |
encoding = vreg.property_value('ui.encoding') |
|
67 |
except: # no vreg or property not registered |
|
68 |
encoding = 'utf-8' |
|
69 |
self.encoding = encoding |
|
70 |
# cache result of execution for (rql expr / eids), |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
71 |
# should be emptied on commit/rollback of the server session / web |
0 | 72 |
# connection |
73 |
self.local_perm_cache = {} |
|
74 |
||
75 |
def property_value(self, key): |
|
76 |
if self.user: |
|
77 |
return self.user.property_value(key) |
|
78 |
return self.vreg.property_value(key) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
79 |
|
0 | 80 |
def etype_rset(self, etype, size=1): |
81 |
"""return a fake result set for a particular entity type""" |
|
82 |
from cubicweb.rset import ResultSet |
|
83 |
rset = ResultSet([('A',)]*size, '%s X' % etype, |
|
84 |
description=[(etype,)]*size) |
|
1132 | 85 |
def get_entity(row, col=0, etype=etype, req=self, rset=rset): |
86 |
return req.vreg.etype_class(etype)(req, rset, row, col) |
|
0 | 87 |
rset.get_entity = get_entity |
88 |
return self.decorate_rset(rset) |
|
89 |
||
90 |
def eid_rset(self, eid, etype=None): |
|
91 |
"""return a result set for the given eid without doing actual query |
|
92 |
(we have the eid, we can suppose it exists and user has access to the |
|
93 |
entity) |
|
94 |
""" |
|
95 |
from cubicweb.rset import ResultSet |
|
96 |
eid = typed_eid(eid) |
|
97 |
if etype is None: |
|
98 |
etype = self.describe(eid)[0] |
|
99 |
rset = ResultSet([(eid,)], 'Any X WHERE X eid %(x)s', {'x': eid}, |
|
100 |
[(etype,)]) |
|
101 |
return self.decorate_rset(rset) |
|
102 |
||
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
103 |
def empty_rset(self): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
104 |
"""return a result set for the given eid without doing actual query |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
105 |
(we have the eid, we can suppose it exists and user has access to the |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
106 |
entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
107 |
""" |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
108 |
from cubicweb.rset import ResultSet |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
109 |
return self.decorate_rset(ResultSet([], 'Any X WHERE X eid -1')) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
110 |
|
0 | 111 |
def entity_from_eid(self, eid, etype=None): |
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
112 |
try: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
113 |
return self.entity_cache(eid) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
114 |
except KeyError: |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
115 |
rset = self.eid_rset(eid, etype) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
116 |
entity = rset.get_entity(0, 0) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
117 |
self.set_entity_cache(entity) |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
118 |
return entity |
0 | 119 |
|
2647
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
120 |
def entity_cache(self, eid): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
121 |
raise KeyError |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
122 |
def set_entity_cache(self, entity): |
b0a2e779845c
enable server side entity caching, 25% speedup on codenaf insertion. ALL CW TESTS OK
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
123 |
pass |
2919
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
124 |
|
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
125 |
def create_entity(self, etype, *args, **kwargs): |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
126 |
"""add a new entity of the given type""" |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
127 |
rql = 'INSERT %s X' % etype |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
128 |
relations = [] |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
129 |
restrictions = [] |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
130 |
cachekey = [] |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
131 |
for rtype, rvar in args: |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
132 |
relations.append('X %s %s' % (rtype, rvar)) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
133 |
restrictions.append('%s eid %%(%s)s' % (rvar, rvar)) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
134 |
cachekey.append(rvar) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
135 |
for attr in kwargs: |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
136 |
if attr in cachekey: |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
137 |
continue |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
138 |
relations.append('X %s %%(%s)s' % (attr, attr)) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
139 |
if relations: |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
140 |
rql = '%s: %s' % (rql, ', '.join(relations)) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
141 |
if restrictions: |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
142 |
rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
143 |
return self.execute(rql, kwargs, cachekey).get_entity(0, 0) |
662f35236d1c
new create_entity method, temporarily on base request (later on cw entity manager or as factory method)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
144 |
|
0 | 145 |
# url generation methods ################################################## |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
146 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
147 |
def build_url(self, *args, **kwargs): |
0 | 148 |
"""return an absolute URL using params dictionary key/values as URL |
149 |
parameters. Values are automatically URL quoted, and the |
|
150 |
publishing method to use may be specified or will be guessed. |
|
151 |
""" |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
152 |
# use *args since we don't want first argument to be "anonymous" to |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
153 |
# avoid potential clash with kwargs |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
154 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
155 |
method = args[0] |
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
156 |
base_url = kwargs.pop('base_url', None) |
0 | 157 |
if base_url is None: |
158 |
base_url = self.base_url() |
|
159 |
if '_restpath' in kwargs: |
|
160 |
assert method == 'view', method |
|
161 |
path = kwargs.pop('_restpath') |
|
162 |
else: |
|
163 |
path = method |
|
164 |
if not kwargs: |
|
165 |
return u'%s%s' % (base_url, path) |
|
166 |
return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
167 |
|
0 | 168 |
|
169 |
def build_url_params(self, **kwargs): |
|
170 |
"""return encoded params to incorporate them in an URL""" |
|
171 |
args = [] |
|
172 |
for param, values in kwargs.items(): |
|
173 |
if not isinstance(values, (list, tuple)): |
|
174 |
values = (values,) |
|
175 |
for value in values: |
|
176 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
|
177 |
return '&'.join(args) |
|
178 |
||
179 |
def url_quote(self, value, safe=''): |
|
180 |
"""urllib.quote is not unicode safe, use this method to do the |
|
181 |
necessary encoding / decoding. Also it's designed to quote each |
|
182 |
part of a url path and so the '/' character will be encoded as well. |
|
183 |
""" |
|
184 |
if isinstance(value, unicode): |
|
185 |
quoted = urlquote(value.encode(self.encoding), safe=safe) |
|
186 |
return unicode(quoted, self.encoding) |
|
187 |
return urlquote(str(value), safe=safe) |
|
188 |
||
189 |
def url_unquote(self, quoted): |
|
190 |
"""returns a unicode unquoted string |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
191 |
|
0 | 192 |
decoding is based on `self.encoding` which is the encoding |
193 |
used in `url_quote` |
|
194 |
""" |
|
195 |
if isinstance(quoted, unicode): |
|
196 |
quoted = quoted.encode(self.encoding) |
|
197 |
try: |
|
198 |
return unicode(urlunquote(quoted), self.encoding) |
|
199 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
|
200 |
return unicode(urlunquote(quoted), 'iso-8859-1') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
201 |
|
0 | 202 |
|
203 |
# session's user related methods ##################################### |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
204 |
|
0 | 205 |
@cached |
206 |
def user_data(self): |
|
207 |
"""returns a dictionnary with this user's information""" |
|
208 |
userinfo = {} |
|
209 |
if self.is_internal_session: |
|
210 |
userinfo['login'] = "cubicweb" |
|
211 |
userinfo['name'] = "cubicweb" |
|
212 |
userinfo['email'] = "" |
|
213 |
return userinfo |
|
214 |
user = self.actual_session().user |
|
215 |
userinfo['login'] = user.login |
|
3111
7b405bb305ab
[notification] can't see valid reason for this...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2919
diff
changeset
|
216 |
userinfo['name'] = user.name() |
7b405bb305ab
[notification] can't see valid reason for this...
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2919
diff
changeset
|
217 |
userinfo['email'] = user.get_email() |
0 | 218 |
return userinfo |
219 |
||
220 |
def is_internal_session(self): |
|
221 |
"""overrided on the server-side""" |
|
222 |
return False |
|
223 |
||
224 |
# abstract methods to override according to the web front-end ############# |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
225 |
|
0 | 226 |
def base_url(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
227 |
"""return the root url of the instance""" |
0 | 228 |
raise NotImplementedError |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
229 |
|
0 | 230 |
def decorate_rset(self, rset): |
231 |
"""add vreg/req (at least) attributes to the given result set """ |
|
232 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
233 |
|
0 | 234 |
def describe(self, eid): |
235 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
236 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
237 |
|
0 | 238 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
239 |
# XXX 2.45 is allowing nicer entity type names, use this map for bw compat |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
240 |
ETYPE_NAME_MAP = {# 3.2 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
241 |
'ECache': 'CWCache', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
242 |
'EUser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
243 |
'EGroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
244 |
'EProperty': 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
245 |
'EFRDef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
246 |
'ENFRDef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
247 |
'ERType': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
248 |
'EEType': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
249 |
'EConstraintType': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
250 |
'EConstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
251 |
'EPermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
252 |
# 2.45 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
253 |
'Eetype': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
254 |
'Ertype': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
255 |
'Efrdef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
256 |
'Enfrdef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
257 |
'Econstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
258 |
'Econstrainttype': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
259 |
'Epermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
260 |
'Egroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
261 |
'Euser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
262 |
'Eproperty': 'CWProperty', |
0 | 263 |
'Emailaddress': 'EmailAddress', |
264 |
'Rqlexpression': 'RQLExpression', |
|
265 |
'Trinfo': 'TrInfo', |
|
266 |
} |
|
267 |
||
268 |
||
269 |
||
270 |
# XXX cubic web cube migration map |
|
271 |
CW_MIGRATION_MAP = {'erudi': 'cubicweb', |
|
272 |
||
273 |
'eaddressbook': 'addressbook', |
|
274 |
'ebasket': 'basket', |
|
275 |
'eblog': 'blog', |
|
276 |
'ebook': 'book', |
|
277 |
'ecomment': 'comment', |
|
278 |
'ecompany': 'company', |
|
279 |
'econference': 'conference', |
|
280 |
'eemail': 'email', |
|
281 |
'eevent': 'event', |
|
282 |
'eexpense': 'expense', |
|
283 |
'efile': 'file', |
|
284 |
'einvoice': 'invoice', |
|
285 |
'elink': 'link', |
|
286 |
'emailinglist': 'mailinglist', |
|
287 |
'eperson': 'person', |
|
288 |
'eshopcart': 'shopcart', |
|
289 |
'eskillmat': 'skillmat', |
|
290 |
'etask': 'task', |
|
291 |
'eworkcase': 'workcase', |
|
292 |
'eworkorder': 'workorder', |
|
293 |
'ezone': 'zone', |
|
294 |
'i18ncontent': 'i18ncontent', |
|
295 |
'svnfile': 'vcsfile', |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
296 |
|
0 | 297 |
'eclassschemes': 'keyword', |
298 |
'eclassfolders': 'folder', |
|
299 |
'eclasstags': 'tag', |
|
300 |
||
301 |
'jpl': 'jpl', |
|
302 |
'jplintra': 'jplintra', |
|
303 |
'jplextra': 'jplextra', |
|
304 |
'jplorg': 'jplorg', |
|
305 |
'jplrecia': 'jplrecia', |
|
306 |
'crm': 'crm', |
|
307 |
'agueol': 'agueol', |
|
308 |
'docaster': 'docaster', |
|
309 |
'asteretud': 'asteretud', |
|
310 |
} |
|
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
311 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
312 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
313 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
314 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
315 |
return 'subject' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
316 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
317 |
def role(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
318 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
319 |
return obj.role |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
320 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
321 |
return neg_role(obj.target) |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
322 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
323 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
324 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
325 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
326 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
327 |
return neg_role(obj.role) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
328 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
329 |
def underline_title(title, car='-'): |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
330 |
return title+'\n'+(car*len(title)) |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
331 |
|
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
332 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
333 |
class CubicWebEventManager(object): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
334 |
"""simple event / callback manager. |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
335 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
336 |
Typical usage to register a callback:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
337 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
338 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
339 |
>>> CW_EVENT_MANAGER.bind('after-registry-reload', mycallback) |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
340 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
341 |
Typical usage to emit an event:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
342 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
343 |
>>> from cubicweb import CW_EVENT_MANAGER |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
344 |
>>> CW_EVENT_MANAGER.emit('after-registry-reload') |
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
345 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
346 |
emit() accepts an additional context parameter that will be passed |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
347 |
to the callback if specified (and only in that case) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
348 |
""" |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
349 |
def __init__(self): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
350 |
self.callbacks = {} |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
351 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
352 |
def bind(self, event, callback, *args, **kwargs): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
353 |
self.callbacks.setdefault(event, []).append( (callback, args, kwargs) ) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
354 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
355 |
def emit(self, event, context=None): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
356 |
for callback, args, kwargs in self.callbacks.get(event, ()): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
357 |
if context is None: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
358 |
callback(*args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
359 |
else: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
360 |
callback(context, *args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
361 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
362 |
CW_EVENT_MANAGER = CubicWebEventManager() |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
363 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
364 |
def onevent(event): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
365 |
"""decorator to ease event / callback binding |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
366 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
367 |
>>> from cubicweb import onevent |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2689
diff
changeset
|
368 |
>>> @onevent('before-registry-reload') |
2689
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
369 |
... def mycallback(): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
370 |
... print 'hello' |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
371 |
... |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
372 |
>>> |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
373 |
""" |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
374 |
def _decorator(func): |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
375 |
CW_EVENT_MANAGER.bind(event, func) |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
376 |
return func |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
377 |
return _decorator |