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