author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 20 Aug 2009 17:33:05 +0200 | |
branch | 3.5 |
changeset 2919 | 662f35236d1c |
parent 2705 | 30bcdbd92820 |
child 2968 | 0e3460341023 |
child 3111 | 7b405bb305ab |
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 |
rql = "Any F,S,A where U eid %(x)s, U firstname F, U surname S, U primary_email E, E address A" |
|
216 |
try: |
|
217 |
firstname, lastname, email = self.execute(rql, {'x': user.eid}, 'x')[0] |
|
218 |
if firstname is None and lastname is None: |
|
219 |
userinfo['name'] = '' |
|
220 |
else: |
|
221 |
userinfo['name'] = ("%s %s" % (firstname, lastname)) |
|
222 |
userinfo['email'] = email |
|
223 |
except IndexError: |
|
224 |
userinfo['name'] = None |
|
225 |
userinfo['email'] = None |
|
226 |
userinfo['login'] = user.login |
|
227 |
return userinfo |
|
228 |
||
229 |
def is_internal_session(self): |
|
230 |
"""overrided on the server-side""" |
|
231 |
return False |
|
232 |
||
233 |
# 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
|
234 |
|
0 | 235 |
def base_url(self): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
236 |
"""return the root url of the instance""" |
0 | 237 |
raise NotImplementedError |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
238 |
|
0 | 239 |
def decorate_rset(self, rset): |
240 |
"""add vreg/req (at least) attributes to the given result set """ |
|
241 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
242 |
|
0 | 243 |
def describe(self, eid): |
244 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
245 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
246 |
|
0 | 247 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
248 |
# 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
|
249 |
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
|
250 |
'ECache': 'CWCache', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
251 |
'EUser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
252 |
'EGroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
253 |
'EProperty': 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
254 |
'EFRDef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
255 |
'ENFRDef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
256 |
'ERType': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
257 |
'EEType': 'CWEType', |
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 |
'EConstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
260 |
'EPermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
261 |
# 2.45 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
262 |
'Eetype': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
263 |
'Ertype': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
264 |
'Efrdef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
265 |
'Enfrdef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
266 |
'Econstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
267 |
'Econstrainttype': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
268 |
'Epermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
269 |
'Egroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
270 |
'Euser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
271 |
'Eproperty': 'CWProperty', |
0 | 272 |
'Emailaddress': 'EmailAddress', |
273 |
'Rqlexpression': 'RQLExpression', |
|
274 |
'Trinfo': 'TrInfo', |
|
275 |
} |
|
276 |
||
277 |
||
278 |
||
279 |
# XXX cubic web cube migration map |
|
280 |
CW_MIGRATION_MAP = {'erudi': 'cubicweb', |
|
281 |
||
282 |
'eaddressbook': 'addressbook', |
|
283 |
'ebasket': 'basket', |
|
284 |
'eblog': 'blog', |
|
285 |
'ebook': 'book', |
|
286 |
'ecomment': 'comment', |
|
287 |
'ecompany': 'company', |
|
288 |
'econference': 'conference', |
|
289 |
'eemail': 'email', |
|
290 |
'eevent': 'event', |
|
291 |
'eexpense': 'expense', |
|
292 |
'efile': 'file', |
|
293 |
'einvoice': 'invoice', |
|
294 |
'elink': 'link', |
|
295 |
'emailinglist': 'mailinglist', |
|
296 |
'eperson': 'person', |
|
297 |
'eshopcart': 'shopcart', |
|
298 |
'eskillmat': 'skillmat', |
|
299 |
'etask': 'task', |
|
300 |
'eworkcase': 'workcase', |
|
301 |
'eworkorder': 'workorder', |
|
302 |
'ezone': 'zone', |
|
303 |
'i18ncontent': 'i18ncontent', |
|
304 |
'svnfile': 'vcsfile', |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
305 |
|
0 | 306 |
'eclassschemes': 'keyword', |
307 |
'eclassfolders': 'folder', |
|
308 |
'eclasstags': 'tag', |
|
309 |
||
310 |
'jpl': 'jpl', |
|
311 |
'jplintra': 'jplintra', |
|
312 |
'jplextra': 'jplextra', |
|
313 |
'jplorg': 'jplorg', |
|
314 |
'jplrecia': 'jplrecia', |
|
315 |
'crm': 'crm', |
|
316 |
'agueol': 'agueol', |
|
317 |
'docaster': 'docaster', |
|
318 |
'asteretud': 'asteretud', |
|
319 |
} |
|
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
320 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
321 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
322 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
323 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
324 |
return 'subject' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
325 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
326 |
def role(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
327 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
328 |
return obj.role |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
329 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
330 |
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
|
331 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
332 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
333 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
334 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
335 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
336 |
return neg_role(obj.role) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
337 |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
338 |
def underline_title(title, car='-'): |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
339 |
return title+'\n'+(car*len(title)) |
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
340 |
|
2683
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
341 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
342 |
class CubicWebEventManager(object): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
343 |
"""simple event / callback manager. |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
344 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
345 |
Typical usage to register a callback:: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
346 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
347 |
>>> 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
|
348 |
>>> 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
|
349 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
350 |
Typical usage to emit an event:: |
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 |
>>> 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
|
353 |
>>> CW_EVENT_MANAGER.emit('after-registry-reload') |
2683
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 |
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
|
356 |
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
|
357 |
""" |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
358 |
def __init__(self): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
359 |
self.callbacks = {} |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
360 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
361 |
def bind(self, event, callback, *args, **kwargs): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
362 |
self.callbacks.setdefault(event, []).append( (callback, args, kwargs) ) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
363 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
364 |
def emit(self, event, context=None): |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
365 |
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
|
366 |
if context is None: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
367 |
callback(*args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
368 |
else: |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
369 |
callback(context, *args, **kwargs) |
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
370 |
|
52b1a86c1913
introduce a new CubicwebEventManager
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2647
diff
changeset
|
371 |
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
|
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 |
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
|
374 |
"""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
|
375 |
|
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
376 |
>>> 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
|
377 |
>>> @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
|
378 |
... 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
|
379 |
... 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
|
380 |
... |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
381 |
>>> |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
382 |
""" |
44f041222d0f
[autoreload] handle uicfg reloading properly with the new event / callback mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2683
diff
changeset
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
return _decorator |