author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 23 Jul 2009 14:51:40 +0200 | |
changeset 2446 | 440cb4ea7e5c |
parent 2059 | af33833d7571 |
child 2458 | 4d114865098f |
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 |
||
103 |
def entity_from_eid(self, eid, etype=None): |
|
104 |
rset = self.eid_rset(eid, etype) |
|
105 |
if rset: |
|
106 |
return rset.get_entity(0, 0) |
|
107 |
else: |
|
108 |
return None |
|
109 |
||
110 |
# url generation methods ################################################## |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
111 |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
112 |
def build_url(self, *args, **kwargs): |
0 | 113 |
"""return an absolute URL using params dictionary key/values as URL |
114 |
parameters. Values are automatically URL quoted, and the |
|
115 |
publishing method to use may be specified or will be guessed. |
|
116 |
""" |
|
2059
af33833d7571
absolute_url / build_url refactoring to avoid potential name clash
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
117 |
# 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
|
118 |
# 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
|
119 |
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
|
120 |
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
|
121 |
base_url = kwargs.pop('base_url', None) |
0 | 122 |
if base_url is None: |
123 |
base_url = self.base_url() |
|
124 |
if '_restpath' in kwargs: |
|
125 |
assert method == 'view', method |
|
126 |
path = kwargs.pop('_restpath') |
|
127 |
else: |
|
128 |
path = method |
|
129 |
if not kwargs: |
|
130 |
return u'%s%s' % (base_url, path) |
|
131 |
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
|
132 |
|
0 | 133 |
|
134 |
def build_url_params(self, **kwargs): |
|
135 |
"""return encoded params to incorporate them in an URL""" |
|
136 |
args = [] |
|
137 |
for param, values in kwargs.items(): |
|
138 |
if not isinstance(values, (list, tuple)): |
|
139 |
values = (values,) |
|
140 |
for value in values: |
|
141 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
|
142 |
return '&'.join(args) |
|
143 |
||
144 |
def url_quote(self, value, safe=''): |
|
145 |
"""urllib.quote is not unicode safe, use this method to do the |
|
146 |
necessary encoding / decoding. Also it's designed to quote each |
|
147 |
part of a url path and so the '/' character will be encoded as well. |
|
148 |
""" |
|
149 |
if isinstance(value, unicode): |
|
150 |
quoted = urlquote(value.encode(self.encoding), safe=safe) |
|
151 |
return unicode(quoted, self.encoding) |
|
152 |
return urlquote(str(value), safe=safe) |
|
153 |
||
154 |
def url_unquote(self, quoted): |
|
155 |
"""returns a unicode unquoted string |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
156 |
|
0 | 157 |
decoding is based on `self.encoding` which is the encoding |
158 |
used in `url_quote` |
|
159 |
""" |
|
160 |
if isinstance(quoted, unicode): |
|
161 |
quoted = quoted.encode(self.encoding) |
|
162 |
try: |
|
163 |
return unicode(urlunquote(quoted), self.encoding) |
|
164 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
|
165 |
return unicode(urlunquote(quoted), 'iso-8859-1') |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
166 |
|
0 | 167 |
|
168 |
# session's user related methods ##################################### |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
169 |
|
0 | 170 |
@cached |
171 |
def user_data(self): |
|
172 |
"""returns a dictionnary with this user's information""" |
|
173 |
userinfo = {} |
|
174 |
if self.is_internal_session: |
|
175 |
userinfo['login'] = "cubicweb" |
|
176 |
userinfo['name'] = "cubicweb" |
|
177 |
userinfo['email'] = "" |
|
178 |
return userinfo |
|
179 |
user = self.actual_session().user |
|
180 |
rql = "Any F,S,A where U eid %(x)s, U firstname F, U surname S, U primary_email E, E address A" |
|
181 |
try: |
|
182 |
firstname, lastname, email = self.execute(rql, {'x': user.eid}, 'x')[0] |
|
183 |
if firstname is None and lastname is None: |
|
184 |
userinfo['name'] = '' |
|
185 |
else: |
|
186 |
userinfo['name'] = ("%s %s" % (firstname, lastname)) |
|
187 |
userinfo['email'] = email |
|
188 |
except IndexError: |
|
189 |
userinfo['name'] = None |
|
190 |
userinfo['email'] = None |
|
191 |
userinfo['login'] = user.login |
|
192 |
return userinfo |
|
193 |
||
194 |
def is_internal_session(self): |
|
195 |
"""overrided on the server-side""" |
|
196 |
return False |
|
197 |
||
198 |
# 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
|
199 |
|
0 | 200 |
def base_url(self): |
201 |
"""return the root url of the application""" |
|
202 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
203 |
|
0 | 204 |
def decorate_rset(self, rset): |
205 |
"""add vreg/req (at least) attributes to the given result set """ |
|
206 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
207 |
|
0 | 208 |
def describe(self, eid): |
209 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
210 |
raise NotImplementedError |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
211 |
|
0 | 212 |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
213 |
# 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
|
214 |
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
|
215 |
'ECache': 'CWCache', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
216 |
'EUser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
217 |
'EGroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
218 |
'EProperty': 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
219 |
'EFRDef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
220 |
'ENFRDef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
221 |
'ERType': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
222 |
'EEType': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
223 |
'EConstraintType': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
224 |
'EConstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
225 |
'EPermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
226 |
# 2.45 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
227 |
'Eetype': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
228 |
'Ertype': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
229 |
'Efrdef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
230 |
'Enfrdef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
231 |
'Econstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
232 |
'Econstrainttype': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
233 |
'Epermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
234 |
'Egroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
'Euser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
236 |
'Eproperty': 'CWProperty', |
0 | 237 |
'Emailaddress': 'EmailAddress', |
238 |
'Rqlexpression': 'RQLExpression', |
|
239 |
'Trinfo': 'TrInfo', |
|
240 |
} |
|
241 |
||
242 |
||
243 |
||
244 |
# XXX cubic web cube migration map |
|
245 |
CW_MIGRATION_MAP = {'erudi': 'cubicweb', |
|
246 |
||
247 |
'eaddressbook': 'addressbook', |
|
248 |
'ebasket': 'basket', |
|
249 |
'eblog': 'blog', |
|
250 |
'ebook': 'book', |
|
251 |
'ecomment': 'comment', |
|
252 |
'ecompany': 'company', |
|
253 |
'econference': 'conference', |
|
254 |
'eemail': 'email', |
|
255 |
'eevent': 'event', |
|
256 |
'eexpense': 'expense', |
|
257 |
'efile': 'file', |
|
258 |
'einvoice': 'invoice', |
|
259 |
'elink': 'link', |
|
260 |
'emailinglist': 'mailinglist', |
|
261 |
'eperson': 'person', |
|
262 |
'eshopcart': 'shopcart', |
|
263 |
'eskillmat': 'skillmat', |
|
264 |
'etask': 'task', |
|
265 |
'eworkcase': 'workcase', |
|
266 |
'eworkorder': 'workorder', |
|
267 |
'ezone': 'zone', |
|
268 |
'i18ncontent': 'i18ncontent', |
|
269 |
'svnfile': 'vcsfile', |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
270 |
|
0 | 271 |
'eclassschemes': 'keyword', |
272 |
'eclassfolders': 'folder', |
|
273 |
'eclasstags': 'tag', |
|
274 |
||
275 |
'jpl': 'jpl', |
|
276 |
'jplintra': 'jplintra', |
|
277 |
'jplextra': 'jplextra', |
|
278 |
'jplorg': 'jplorg', |
|
279 |
'jplrecia': 'jplrecia', |
|
280 |
'crm': 'crm', |
|
281 |
'agueol': 'agueol', |
|
282 |
'docaster': 'docaster', |
|
283 |
'asteretud': 'asteretud', |
|
284 |
} |
|
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
285 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
286 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
287 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
288 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
289 |
return 'subject' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
290 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
291 |
def role(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
292 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
293 |
return obj.role |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
294 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
295 |
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
|
296 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
297 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
298 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
299 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
300 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
301 |
return neg_role(obj.role) |
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
302 |