author | sylvain.thenault@logilab.fr |
Fri, 17 Apr 2009 16:55:37 +0200 | |
branch | tls-sprint |
changeset 1398 | 5fe84a5f7035 |
parent 1263 | 01152fffd593 |
child 1498 | 2c6eec0b46b9 |
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 |
|
1022
15d3e1b3a27d
copyright / license update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
333
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
|
25 |
||
26 |
def set_log_methods(cls, logger): |
|
27 |
"""bind standart logger's methods as static methods on the class |
|
28 |
""" |
|
29 |
cls._logger = logger |
|
333
c65eccf85895
simplify selectors debugging
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
231
diff
changeset
|
30 |
for attr in ('debug', 'info', 'warning', 'error', 'critical', 'exception'): |
0 | 31 |
setattr(cls, attr, getattr(logger, attr)) |
32 |
||
33 |
if os.environ.get('APYCOT_ROOT'): |
|
34 |
logging.basicConfig(level=logging.CRITICAL) |
|
35 |
else: |
|
36 |
logging.basicConfig() |
|
37 |
||
38 |
||
39 |
set_log_methods(sys.modules[__name__], logging.getLogger('cubicweb')) |
|
40 |
||
41 |
# make all exceptions accessible from the package |
|
42 |
from cubicweb._exceptions import * |
|
43 |
||
44 |
# convert eid to the right type, raise ValueError if it's not a valid eid |
|
45 |
typed_eid = int |
|
46 |
||
47 |
||
48 |
#def log_thread(f, w, a): |
|
49 |
# print f.f_code.co_filename, f.f_code.co_name |
|
50 |
#import threading |
|
51 |
#threading.settrace(log_thread) |
|
52 |
||
53 |
class Binary(StringIO): |
|
54 |
"""customize StringIO to make sure we don't use unicode""" |
|
55 |
def __init__(self, buf= ''): |
|
56 |
assert isinstance(buf, (str, buffer)), \ |
|
57 |
"Binary objects must use raw strings, not %s" % buf.__class__ |
|
58 |
StringIO.__init__(self, buf) |
|
59 |
||
60 |
def write(self, data): |
|
61 |
assert isinstance(data, (str, buffer)), \ |
|
62 |
"Binary objects must use raw strings, not %s" % data.__class__ |
|
63 |
StringIO.write(self, data) |
|
64 |
||
65 |
||
66 |
class RequestSessionMixIn(object): |
|
67 |
"""mixin class containing stuff shared by server session and web request |
|
68 |
""" |
|
69 |
def __init__(self, vreg): |
|
70 |
self.vreg = vreg |
|
71 |
try: |
|
72 |
encoding = vreg.property_value('ui.encoding') |
|
73 |
except: # no vreg or property not registered |
|
74 |
encoding = 'utf-8' |
|
75 |
self.encoding = encoding |
|
76 |
# cache result of execution for (rql expr / eids), |
|
77 |
# should be emptied on commit/rollback of the server session / web |
|
78 |
# connection |
|
79 |
self.local_perm_cache = {} |
|
80 |
||
81 |
def property_value(self, key): |
|
82 |
if self.user: |
|
83 |
return self.user.property_value(key) |
|
84 |
return self.vreg.property_value(key) |
|
85 |
||
86 |
def etype_rset(self, etype, size=1): |
|
87 |
"""return a fake result set for a particular entity type""" |
|
88 |
from cubicweb.rset import ResultSet |
|
89 |
rset = ResultSet([('A',)]*size, '%s X' % etype, |
|
90 |
description=[(etype,)]*size) |
|
1132 | 91 |
def get_entity(row, col=0, etype=etype, req=self, rset=rset): |
92 |
return req.vreg.etype_class(etype)(req, rset, row, col) |
|
0 | 93 |
rset.get_entity = get_entity |
94 |
return self.decorate_rset(rset) |
|
95 |
||
96 |
def eid_rset(self, eid, etype=None): |
|
97 |
"""return a result set for the given eid without doing actual query |
|
98 |
(we have the eid, we can suppose it exists and user has access to the |
|
99 |
entity) |
|
100 |
""" |
|
101 |
from cubicweb.rset import ResultSet |
|
102 |
eid = typed_eid(eid) |
|
103 |
if etype is None: |
|
104 |
etype = self.describe(eid)[0] |
|
105 |
rset = ResultSet([(eid,)], 'Any X WHERE X eid %(x)s', {'x': eid}, |
|
106 |
[(etype,)]) |
|
107 |
return self.decorate_rset(rset) |
|
108 |
||
109 |
def entity_from_eid(self, eid, etype=None): |
|
110 |
rset = self.eid_rset(eid, etype) |
|
111 |
if rset: |
|
112 |
return rset.get_entity(0, 0) |
|
113 |
else: |
|
114 |
return None |
|
115 |
||
116 |
# url generation methods ################################################## |
|
117 |
||
118 |
def build_url(self, method, base_url=None, **kwargs): |
|
119 |
"""return an absolute URL using params dictionary key/values as URL |
|
120 |
parameters. Values are automatically URL quoted, and the |
|
121 |
publishing method to use may be specified or will be guessed. |
|
122 |
""" |
|
123 |
if base_url is None: |
|
124 |
base_url = self.base_url() |
|
125 |
if '_restpath' in kwargs: |
|
126 |
assert method == 'view', method |
|
127 |
path = kwargs.pop('_restpath') |
|
128 |
else: |
|
129 |
path = method |
|
130 |
if not kwargs: |
|
131 |
return u'%s%s' % (base_url, path) |
|
132 |
return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
|
133 |
||
134 |
||
135 |
def build_url_params(self, **kwargs): |
|
136 |
"""return encoded params to incorporate them in an URL""" |
|
137 |
args = [] |
|
138 |
for param, values in kwargs.items(): |
|
139 |
if not isinstance(values, (list, tuple)): |
|
140 |
values = (values,) |
|
141 |
for value in values: |
|
142 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
|
143 |
return '&'.join(args) |
|
144 |
||
145 |
def url_quote(self, value, safe=''): |
|
146 |
"""urllib.quote is not unicode safe, use this method to do the |
|
147 |
necessary encoding / decoding. Also it's designed to quote each |
|
148 |
part of a url path and so the '/' character will be encoded as well. |
|
149 |
""" |
|
150 |
if isinstance(value, unicode): |
|
151 |
quoted = urlquote(value.encode(self.encoding), safe=safe) |
|
152 |
return unicode(quoted, self.encoding) |
|
153 |
return urlquote(str(value), safe=safe) |
|
154 |
||
155 |
def url_unquote(self, quoted): |
|
156 |
"""returns a unicode unquoted string |
|
157 |
|
|
158 |
decoding is based on `self.encoding` which is the encoding |
|
159 |
used in `url_quote` |
|
160 |
""" |
|
161 |
if isinstance(quoted, unicode): |
|
162 |
quoted = quoted.encode(self.encoding) |
|
163 |
try: |
|
164 |
return unicode(urlunquote(quoted), self.encoding) |
|
165 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
|
166 |
return unicode(urlunquote(quoted), 'iso-8859-1') |
|
167 |
||
168 |
||
169 |
# session's user related methods ##################################### |
|
170 |
||
171 |
@cached |
|
172 |
def user_data(self): |
|
173 |
"""returns a dictionnary with this user's information""" |
|
174 |
userinfo = {} |
|
175 |
if self.is_internal_session: |
|
176 |
userinfo['login'] = "cubicweb" |
|
177 |
userinfo['name'] = "cubicweb" |
|
178 |
userinfo['email'] = "" |
|
179 |
return userinfo |
|
180 |
user = self.actual_session().user |
|
181 |
rql = "Any F,S,A where U eid %(x)s, U firstname F, U surname S, U primary_email E, E address A" |
|
182 |
try: |
|
183 |
firstname, lastname, email = self.execute(rql, {'x': user.eid}, 'x')[0] |
|
184 |
if firstname is None and lastname is None: |
|
185 |
userinfo['name'] = '' |
|
186 |
else: |
|
187 |
userinfo['name'] = ("%s %s" % (firstname, lastname)) |
|
188 |
userinfo['email'] = email |
|
189 |
except IndexError: |
|
190 |
userinfo['name'] = None |
|
191 |
userinfo['email'] = None |
|
192 |
userinfo['login'] = user.login |
|
193 |
return userinfo |
|
194 |
||
195 |
def is_internal_session(self): |
|
196 |
"""overrided on the server-side""" |
|
197 |
return False |
|
198 |
||
199 |
# abstract methods to override according to the web front-end ############# |
|
200 |
||
201 |
def base_url(self): |
|
202 |
"""return the root url of the application""" |
|
203 |
raise NotImplementedError |
|
204 |
||
205 |
def decorate_rset(self, rset): |
|
206 |
"""add vreg/req (at least) attributes to the given result set """ |
|
207 |
raise NotImplementedError |
|
208 |
||
209 |
def describe(self, eid): |
|
210 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
|
211 |
raise NotImplementedError |
|
212 |
||
213 |
||
214 |
# 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
|
215 |
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
|
216 |
'ECache': 'CWCache', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
217 |
'EUser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
218 |
'EGroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
219 |
'EProperty': 'CWProperty', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
220 |
'EFRDef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
221 |
'ENFRDef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
222 |
'ERType': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
223 |
'EEType': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
224 |
'EConstraintType': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
225 |
'EConstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
226 |
'EPermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
227 |
# 2.45 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
228 |
'Eetype': 'CWEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
229 |
'Ertype': 'CWRType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
230 |
'Efrdef': 'CWAttribute', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
231 |
'Enfrdef': 'CWRelation', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
232 |
'Econstraint': 'CWConstraint', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
233 |
'Econstrainttype': 'CWConstraintType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
234 |
'Epermission': 'CWPermission', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
235 |
'Egroup': 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
236 |
'Euser': 'CWUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
237 |
'Eproperty': 'CWProperty', |
0 | 238 |
'Emailaddress': 'EmailAddress', |
239 |
'Rqlexpression': 'RQLExpression', |
|
240 |
'Trinfo': 'TrInfo', |
|
241 |
} |
|
242 |
||
243 |
||
244 |
||
245 |
# XXX cubic web cube migration map |
|
246 |
CW_MIGRATION_MAP = {'erudi': 'cubicweb', |
|
247 |
||
248 |
'eaddressbook': 'addressbook', |
|
249 |
'ebasket': 'basket', |
|
250 |
'eblog': 'blog', |
|
251 |
'ebook': 'book', |
|
252 |
'ecomment': 'comment', |
|
253 |
'ecompany': 'company', |
|
254 |
'econference': 'conference', |
|
255 |
'eemail': 'email', |
|
256 |
'eevent': 'event', |
|
257 |
'eexpense': 'expense', |
|
258 |
'efile': 'file', |
|
259 |
'einvoice': 'invoice', |
|
260 |
'elink': 'link', |
|
261 |
'emailinglist': 'mailinglist', |
|
262 |
'eperson': 'person', |
|
263 |
'eshopcart': 'shopcart', |
|
264 |
'eskillmat': 'skillmat', |
|
265 |
'etask': 'task', |
|
266 |
'eworkcase': 'workcase', |
|
267 |
'eworkorder': 'workorder', |
|
268 |
'ezone': 'zone', |
|
269 |
'i18ncontent': 'i18ncontent', |
|
270 |
'svnfile': 'vcsfile', |
|
271 |
||
272 |
'eclassschemes': 'keyword', |
|
273 |
'eclassfolders': 'folder', |
|
274 |
'eclasstags': 'tag', |
|
275 |
||
276 |
'jpl': 'jpl', |
|
277 |
'jplintra': 'jplintra', |
|
278 |
'jplextra': 'jplextra', |
|
279 |
'jplorg': 'jplorg', |
|
280 |
'jplrecia': 'jplrecia', |
|
281 |
'crm': 'crm', |
|
282 |
'agueol': 'agueol', |
|
283 |
'docaster': 'docaster', |
|
284 |
'asteretud': 'asteretud', |
|
285 |
} |
|
231
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
286 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
287 |
def neg_role(role): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
288 |
if role == 'subject': |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
289 |
return 'object' |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
290 |
return 'subject' |
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 role(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.role |
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.target) |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
297 |
|
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
298 |
def target(obj): |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
299 |
try: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
300 |
return obj.target |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
301 |
except AttributeError: |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
302 |
return neg_role(obj.role) |
d740f5f55d30
some mini function to ease role (subject/object) manipulation
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
303 |