author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 Jan 2010 20:27:48 +0100 | |
changeset 4379 | 72a5a8e075e9 |
parent 4307 | 7fba9c34c88f |
child 4459 | f628abfb3a6c |
permissions | -rw-r--r-- |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
1 |
"""Base class for request/session |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
2 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
3 |
:organization: Logilab |
4307
7fba9c34c88f
update copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4300
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
6 |
:license: Library General Public License version 2 - http://www.gnu.org/licenses |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
7 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
9 |
|
2808
497424219fb0
fix urlquote imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2798
diff
changeset
|
10 |
from urllib import quote as urlquote, unquote as urlunquote |
2796
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
11 |
from datetime import time, datetime, timedelta |
4300
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
12 |
from cgi import parse_qsl |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
13 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
14 |
from logilab.common.decorators import cached |
3659 | 15 |
from logilab.common.deprecation import deprecated |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
16 |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
17 |
from cubicweb import Unauthorized, RegistryException, typed_eid |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
18 |
from cubicweb.rset import ResultSet |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
19 |
from cubicweb.utils import ustrftime, strptime, todate, todatetime |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
20 |
|
2796
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
21 |
ONESECOND = timedelta(0, 1, 0) |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
22 |
CACHE_REGISTRY = {} |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
23 |
|
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
24 |
|
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
25 |
class Cache(dict): |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
26 |
def __init__(self): |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
27 |
super(Cache, self).__init__() |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
28 |
_now = datetime.now() |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
29 |
self.cache_creation_date = _now |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
30 |
self.latest_cache_lookup = _now |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
31 |
|
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
32 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
33 |
class RequestSessionBase(object): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
34 |
"""base class containing stuff shared by server session and web request |
2825 | 35 |
|
36 |
request/session is the main resources accessor, mainly through it's vreg |
|
37 |
attribute: |
|
38 |
:vreg: |
|
39 |
the instance's registry |
|
40 |
:vreg.schema: |
|
41 |
the instance's schema |
|
42 |
:vreg.config: |
|
43 |
the instance's configuration |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
44 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
45 |
def __init__(self, vreg): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
46 |
self.vreg = vreg |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
47 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
48 |
encoding = vreg.property_value('ui.encoding') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
49 |
except: # no vreg or property not registered |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
50 |
encoding = 'utf-8' |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
51 |
self.encoding = encoding |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
52 |
# cache result of execution for (rql expr / eids), |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
53 |
# should be emptied on commit/rollback of the server session / web |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
54 |
# connection |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
55 |
self.local_perm_cache = {} |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
56 |
self._ = unicode |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
57 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
58 |
def property_value(self, key): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
59 |
"""return value of the property with the given key, giving priority to |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
60 |
user specific value if any, else using site value |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
61 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
62 |
if self.user: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
63 |
return self.user.property_value(key) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
64 |
return self.vreg.property_value(key) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
65 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
66 |
def etype_rset(self, etype, size=1): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
67 |
"""return a fake result set for a particular entity type""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
68 |
rset = ResultSet([('A',)]*size, '%s X' % etype, |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
69 |
description=[(etype,)]*size) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
70 |
def get_entity(row, col=0, etype=etype, req=self, rset=rset): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
71 |
return req.vreg.etype_class(etype)(req, rset, row, col) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
72 |
rset.get_entity = get_entity |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
73 |
return self.decorate_rset(rset) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
74 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
75 |
def eid_rset(self, eid, etype=None): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
76 |
"""return a result set for the given eid without doing actual query |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
77 |
(we have the eid, we can suppose it exists and user has access to the |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
78 |
entity) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
79 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
80 |
eid = typed_eid(eid) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
81 |
if etype is None: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
82 |
etype = self.describe(eid)[0] |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
83 |
rset = ResultSet([(eid,)], 'Any X WHERE X eid %(x)s', {'x': eid}, |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
84 |
[(etype,)]) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
85 |
return self.decorate_rset(rset) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
86 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
87 |
def empty_rset(self): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
88 |
"""return a result set for the given eid without doing actual query |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
89 |
(we have the eid, we can suppose it exists and user has access to the |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
90 |
entity) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
91 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
92 |
return self.decorate_rset(ResultSet([], 'Any X WHERE X eid -1')) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
93 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
94 |
def entity_from_eid(self, eid, etype=None): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
95 |
"""return an entity instance for the given eid. No query is done""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
96 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
97 |
return self.entity_cache(eid) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
98 |
except KeyError: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
99 |
rset = self.eid_rset(eid, etype) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
100 |
entity = rset.get_entity(0, 0) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
101 |
self.set_entity_cache(entity) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
102 |
return entity |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
103 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
104 |
def entity_cache(self, eid): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
105 |
raise KeyError |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
106 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
107 |
def set_entity_cache(self, entity): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
108 |
pass |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
109 |
|
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
110 |
# XXX move to CWEntityManager or even better as factory method (unclear |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
111 |
# where yet...) |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
112 |
|
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
113 |
def create_entity(self, etype, _cw_unsafe=False, **kwargs): |
3629
559cad62c786
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
114 |
"""add a new entity of the given type |
559cad62c786
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
115 |
|
559cad62c786
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
116 |
Example (in a shell session): |
559cad62c786
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
117 |
|
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
118 |
c = create_entity('Company', name=u'Logilab') |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
119 |
create_entity('Person', works_for=c, firstname=u'John', lastname=u'Doe') |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
120 |
|
3629
559cad62c786
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
121 |
""" |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
122 |
if _cw_unsafe: |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
123 |
execute = self.unsafe_execute |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
124 |
else: |
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
125 |
execute = self.execute |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
126 |
rql = 'INSERT %s X' % etype |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
127 |
relations = [] |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
128 |
restrictions = set() |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
129 |
cachekey = [] |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
130 |
pending_relations = [] |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
131 |
for attr, value in kwargs.items(): |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
132 |
if isinstance(value, (tuple, list, set, frozenset)): |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
133 |
if len(value) == 1: |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
134 |
value = iter(value).next() |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
135 |
else: |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
136 |
del kwargs[attr] |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
137 |
pending_relations.append( (attr, value) ) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
138 |
continue |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
139 |
if hasattr(value, 'eid'): # non final relation |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
140 |
rvar = attr.upper() |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
141 |
# XXX safer detection of object relation |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
142 |
if attr.startswith('reverse_'): |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
143 |
relations.append('%s %s X' % (rvar, attr[len('reverse_'):])) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
144 |
else: |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
145 |
relations.append('X %s %s' % (attr, rvar)) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
146 |
restriction = '%s eid %%(%s)s' % (rvar, attr) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
147 |
if not restriction in restrictions: |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
148 |
restrictions.add(restriction) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
149 |
cachekey.append(attr) |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
150 |
kwargs[attr] = value.eid |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
151 |
else: # attribute |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
152 |
relations.append('X %s %%(%s)s' % (attr, attr)) |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
153 |
if relations: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
154 |
rql = '%s: %s' % (rql, ', '.join(relations)) |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
155 |
if restrictions: |
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
156 |
rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
157 |
created = execute(rql, kwargs, cachekey).get_entity(0, 0) |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
158 |
for attr, values in pending_relations: |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
159 |
if attr.startswith('reverse_'): |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
160 |
restr = 'Y %s X' % attr[len('reverse_'):] |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
161 |
else: |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
162 |
restr = 'X %s Y' % attr |
3720
5376aaadd16b
backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3674
diff
changeset
|
163 |
execute('SET %s WHERE X eid %%(x)s, Y eid IN (%s)' % ( |
3674
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
164 |
restr, ','.join(str(r.eid) for r in values)), |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
165 |
{'x': created.eid}, 'x') |
387d51af966d
backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3659
diff
changeset
|
166 |
return created |
2968
0e3460341023
somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2825
diff
changeset
|
167 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
168 |
def ensure_ro_rql(self, rql): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
169 |
"""raise an exception if the given rql is not a select query""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
170 |
first = rql.split(' ', 1)[0].lower() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
171 |
if first in ('insert', 'set', 'delete'): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
172 |
raise Unauthorized(self._('only select queries are authorized')) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
173 |
|
2796
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
174 |
def get_cache(self, cachename): |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
175 |
""" |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
176 |
NOTE: cachename should be dotted names as in : |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
177 |
- cubicweb.mycache |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
178 |
- cubes.blog.mycache |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
179 |
- etc. |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
180 |
""" |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
181 |
if cachename in CACHE_REGISTRY: |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
182 |
cache = CACHE_REGISTRY[cachename] |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
183 |
else: |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
184 |
cache = CACHE_REGISTRY[cachename] = Cache() |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
185 |
_now = datetime.now() |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
186 |
if _now > cache.latest_cache_lookup + ONESECOND: |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
187 |
ecache = self.execute( |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
188 |
'Any C,T WHERE C is CWCache, C name %(name)s, C timestamp T', |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
189 |
{'name':cachename}).get_entity(0,0) |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
190 |
cache.latest_cache_lookup = _now |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
191 |
if not ecache.valid(cache.cache_creation_date): |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
192 |
cache.clear() |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
193 |
cache.cache_creation_date = _now |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
194 |
return cache |
14d2c69e12c4
move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
195 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
196 |
# url generation methods ################################################## |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
197 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
198 |
def build_url(self, *args, **kwargs): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
199 |
"""return an absolute URL using params dictionary key/values as URL |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
200 |
parameters. Values are automatically URL quoted, and the |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
201 |
publishing method to use may be specified or will be guessed. |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
202 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
203 |
# use *args since we don't want first argument to be "anonymous" to |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
204 |
# avoid potential clash with kwargs |
2798
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
205 |
if args: |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
206 |
assert len(args) == 1, 'only 0 or 1 non-named-argument expected' |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
207 |
method = args[0] |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
208 |
else: |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
209 |
method = None |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
210 |
# XXX I (adim) think that if method is passed explicitly, we should |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
211 |
# not try to process it and directly call req.build_url() |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
212 |
if method is None: |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
213 |
if self.from_controller() == 'view' and not '_restpath' in kwargs: |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
214 |
method = self.relative_path(includeparams=False) or 'view' |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
215 |
else: |
9c650701cb17
stop using controller class attribute, allowing to deprecate appobject.build_url in favor of req.build_url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2796
diff
changeset
|
216 |
method = 'view' |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
217 |
base_url = kwargs.pop('base_url', None) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
218 |
if base_url is None: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
219 |
base_url = self.base_url() |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
220 |
if '_restpath' in kwargs: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
221 |
assert method == 'view', method |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
222 |
path = kwargs.pop('_restpath') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
223 |
else: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
224 |
path = method |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
225 |
if not kwargs: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
226 |
return u'%s%s' % (base_url, path) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
227 |
return u'%s%s?%s' % (base_url, path, self.build_url_params(**kwargs)) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
228 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
229 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
230 |
def build_url_params(self, **kwargs): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
231 |
"""return encoded params to incorporate them in an URL""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
232 |
args = [] |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
233 |
for param, values in kwargs.items(): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
234 |
if not isinstance(values, (list, tuple)): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
235 |
values = (values,) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
236 |
for value in values: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
237 |
args.append(u'%s=%s' % (param, self.url_quote(value))) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
238 |
return '&'.join(args) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
239 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
240 |
def url_quote(self, value, safe=''): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
241 |
"""urllib.quote is not unicode safe, use this method to do the |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
242 |
necessary encoding / decoding. Also it's designed to quote each |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
243 |
part of a url path and so the '/' character will be encoded as well. |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
244 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
245 |
if isinstance(value, unicode): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
246 |
quoted = urlquote(value.encode(self.encoding), safe=safe) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
247 |
return unicode(quoted, self.encoding) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
248 |
return urlquote(str(value), safe=safe) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
249 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
250 |
def url_unquote(self, quoted): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
251 |
"""returns a unicode unquoted string |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
252 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
253 |
decoding is based on `self.encoding` which is the encoding |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
254 |
used in `url_quote` |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
255 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
256 |
if isinstance(quoted, unicode): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
257 |
quoted = quoted.encode(self.encoding) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
258 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
259 |
return unicode(urlunquote(quoted), self.encoding) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
260 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
261 |
return unicode(urlunquote(quoted), 'iso-8859-1') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
262 |
|
4300
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
263 |
def url_parse_qsl(self, querystring): |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
264 |
"""return a list of (key, val) found in the url quoted query string""" |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
265 |
if isinstance(querystring, unicode): |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
266 |
querystring = querystring.encode(self.encoding) |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
267 |
for key, val in parse_qsl(querystring): |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
268 |
try: |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
269 |
yield unicode(key, self.encoding), unicode(val, self.encoding) |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
270 |
except UnicodeDecodeError: # might occurs on manually typed URLs |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
271 |
yield unicode(key, 'iso-8859-1'), unicode(val, 'iso-8859-1') |
03430023ce82
new url_parse_qs method on base request to parse unicode url formatted query strings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4222
diff
changeset
|
272 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
273 |
# bound user related methods ############################################### |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
274 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
275 |
@cached |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
276 |
def user_data(self): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
277 |
"""returns a dictionnary with this user's information""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
278 |
userinfo = {} |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
279 |
if self.is_internal_session: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
280 |
userinfo['login'] = "cubicweb" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
281 |
userinfo['name'] = "cubicweb" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
282 |
userinfo['email'] = "" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
283 |
return userinfo |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
284 |
user = self.actual_session().user |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
285 |
userinfo['login'] = user.login |
3163
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
286 |
userinfo['name'] = user.name() |
edfe43ceaa35
backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2968
diff
changeset
|
287 |
userinfo['email'] = user.get_email() |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
288 |
return userinfo |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
289 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
290 |
def is_internal_session(self): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
291 |
"""overrided on the server-side""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
292 |
return False |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
293 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
294 |
# formating methods ####################################################### |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
295 |
|
2823
a4e3e9b1a9cd
backported from appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2808
diff
changeset
|
296 |
def view(self, __vid, rset=None, __fallback_oid=None, __registry='views', |
4070
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
297 |
initargs=None, **kwargs): |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
298 |
"""Select object with the given id (`__oid`) then render it. If the |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
299 |
object isn't selectable, try to select fallback object if |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
300 |
`__fallback_oid` is specified. |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
301 |
|
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
302 |
If specified `initargs` is expected to be a dictionnary containing |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
303 |
arguments that should be given to selection (hence to object's __init__ |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
304 |
as well), but not to render(). Other arbitrary keyword arguments will be |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
305 |
given to selection *and* to render(), and so should be handled by |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
306 |
object's call or cell_call method.. |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
307 |
""" |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
308 |
if initargs is None: |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
309 |
initargs = kwargs |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
310 |
else: |
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
311 |
initargs.update(kwargs) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
312 |
try: |
4070
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
313 |
view = self.vreg[__registry].select(__vid, self, rset=rset, **initargs) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
314 |
except RegistryException: |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
315 |
view = self.vreg[__registry].select(__fallback_oid, self, |
4070
6e9f7abee4ef
fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
3720
diff
changeset
|
316 |
rset=rset, **initargs) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3629
diff
changeset
|
317 |
return view.render(**kwargs) |
2823
a4e3e9b1a9cd
backported from appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2808
diff
changeset
|
318 |
|
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
319 |
def format_date(self, date, date_format=None, time=False): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
320 |
"""return a string for a date time according to instance's |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
321 |
configuration |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
322 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
323 |
if date: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
324 |
if date_format is None: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
325 |
if time: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
326 |
date_format = self.property_value('ui.datetime-format') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
327 |
else: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
328 |
date_format = self.property_value('ui.date-format') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
329 |
return ustrftime(date, date_format) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
330 |
return u'' |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
331 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
332 |
def format_time(self, time): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
333 |
"""return a string for a time according to instance's |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
334 |
configuration |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
335 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
336 |
if time: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
337 |
return ustrftime(time, self.property_value('ui.time-format')) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
338 |
return u'' |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
339 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
340 |
def format_float(self, num): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
341 |
"""return a string for floating point number according to instance's |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
342 |
configuration |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
343 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
344 |
if num: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
345 |
return self.property_value('ui.float-format') % num |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
346 |
return u'' |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
347 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
348 |
def parse_datetime(self, value, etype='Datetime'): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
349 |
"""get a datetime or time from a string (according to etype) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
350 |
Datetime formatted as Date are accepted |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
351 |
""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
352 |
assert etype in ('Datetime', 'Date', 'Time'), etype |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
353 |
# XXX raise proper validation error |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
354 |
if etype == 'Datetime': |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
355 |
format = self.property_value('ui.datetime-format') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
356 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
357 |
return todatetime(strptime(value, format)) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
358 |
except ValueError: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
359 |
pass |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
360 |
elif etype == 'Time': |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
361 |
format = self.property_value('ui.time-format') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
362 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
363 |
# (adim) I can't find a way to parse a Time with a custom format |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
364 |
date = strptime(value, format) # this returns a DateTime |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
365 |
return time(date.hour, date.minute, date.second) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
366 |
except ValueError: |
4222
5667f493c505
return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4070
diff
changeset
|
367 |
raise ValueError(self._('can\'t parse %(value)r (expected %(format)s)') |
5667f493c505
return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4070
diff
changeset
|
368 |
% {'value': value, 'format': format}) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
369 |
try: |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
370 |
format = self.property_value('ui.date-format') |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
371 |
dt = strptime(value, format) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
372 |
if etype == 'Datetime': |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
373 |
return todatetime(dt) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
374 |
return todate(dt) |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
375 |
except ValueError: |
4222
5667f493c505
return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4070
diff
changeset
|
376 |
raise ValueError(self._('can\'t parse %(value)r (expected %(format)s)') |
5667f493c505
return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4070
diff
changeset
|
377 |
% {'value': value, 'format': format}) |
2792
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
378 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
379 |
# abstract methods to override according to the web front-end ############# |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
380 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
381 |
def base_url(self): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
382 |
"""return the root url of the instance""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
383 |
raise NotImplementedError |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
384 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
385 |
def decorate_rset(self, rset): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
386 |
"""add vreg/req (at least) attributes to the given result set """ |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
387 |
raise NotImplementedError |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
388 |
|
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
389 |
def describe(self, eid): |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
390 |
"""return a tuple (type, sourceuri, extid) for the entity with id <eid>""" |
135580d15d42
rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff
changeset
|
391 |
raise NotImplementedError |
3659 | 392 |
|
393 |
@property |
|
394 |
@deprecated('[3.6] use _cw.vreg.config') |
|
395 |
def config(self): |
|
396 |
return self.vreg.config |
|
397 |
||
398 |
@property |
|
399 |
@deprecated('[3.6] use _cw.vreg.schema') |
|
400 |
def schema(self): |
|
401 |
return self.vreg.schema |