req.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 28 Apr 2010 10:06:01 +0200
branchstable
changeset 5421 8167de96c523
parent 5290 15846058378b
child 5423 e15abfdcce38
child 5424 8ecbcbff9777
permissions -rw-r--r--
proper licensing information (LGPL-2.1). Hope I get it right this time.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    10
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    11
# logilab-common is distributed in the hope that it will be useful, but WITHOUT
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5290
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
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
"""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
    19
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
"""
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
    21
__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
    22
4933
433174d9394f add missing warn imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4899
diff changeset
    23
from warnings import warn
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    24
from urlparse import urlsplit, urlunsplit
2808
497424219fb0 fix urlquote imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2798
diff changeset
    25
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
    26
from datetime import time, datetime, timedelta
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    27
from cgi import parse_qs, 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
    28
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
    29
from logilab.common.decorators import cached
3659
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
    30
from logilab.common.deprecation import deprecated
4466
8b0ca7904820 moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4459
diff changeset
    31
from logilab.common.date import ustrftime, strptime, todate, todatetime
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
    32
3655
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3629
diff changeset
    33
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
    34
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
    35
2796
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    36
ONESECOND = timedelta(0, 1, 0)
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    37
CACHE_REGISTRY = {}
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    38
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    39
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    40
def _check_cw_unsafe(kwargs):
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    41
    if kwargs.pop('_cw_unsafe', False):
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    42
        warn('[3.7] _cw_unsafe argument is deprecated, now unsafe by '
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    43
             'default, control it using cw_[read|write]_security.',
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    44
             DeprecationWarning, stacklevel=3)
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
    45
2796
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    46
class Cache(dict):
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    47
    def __init__(self):
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    48
        super(Cache, self).__init__()
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    49
        _now = datetime.now()
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    50
        self.cache_creation_date = _now
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    51
        self.latest_cache_lookup = _now
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    52
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
    53
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
    54
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
    55
    """base class containing stuff shared by server session and web request
2825
87ac03aed941 doc update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2823
diff changeset
    56
87ac03aed941 doc update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2823
diff changeset
    57
    request/session is the main resources accessor, mainly through it's vreg
87ac03aed941 doc update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2823
diff changeset
    58
    attribute:
5290
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
    59
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
    60
    :attribute vreg: the instance's registry
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
    61
    :attribute vreg.schema: the instance's schema
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
    62
    :attribute vreg.config: 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
    63
    """
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
    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
    65
        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
    66
        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
    67
            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
    68
        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
    69
            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
    70
        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
    71
        # 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
    72
        # 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
    73
        # 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
    74
        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
    75
        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
    76
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
    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
    78
        """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
    79
        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
    80
        """
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 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
    82
            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
    83
        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
    84
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
    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
    86
        """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
    87
        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
    88
                         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
    89
        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
    90
            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
    91
        rset.get_entity = get_entity
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    92
        rset.req = self
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    93
        return rset
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
    94
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
    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
    96
        """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
    97
        (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
    98
        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
    99
        """
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
        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
   101
        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
   102
            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
   103
        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
   104
                         [(etype,)])
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   105
        rset.req = self
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   106
        return rset
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
   107
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
    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
   109
        """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
   110
        (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
   111
        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
   112
        """
4850
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   113
        rset = ResultSet([], 'Any X WHERE X eid -1')
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   114
        rset.req = self
bd640b137f50 [refactor] drop rset.vreg attribute, vreg should be accessed through rset.req. Also kill decorate_rset, simply set rset.req where we were calling this method.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   115
        return rset
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
   116
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
   117
    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
   118
        """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
   119
        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
   120
            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
   121
        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
   122
            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
   123
            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
   124
            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
   125
            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
   126
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
   127
    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
   128
        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
   129
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
   130
    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
   131
        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
   132
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   133
    def create_entity(self, etype, **kwargs):
3629
559cad62c786 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   134
        """add a new entity of the given type
559cad62c786 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   135
559cad62c786 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   136
        Example (in a shell session):
559cad62c786 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   137
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   138
        >>> c = create_entity('Company', name=u'Logilab')
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   139
        >>> create_entity('Person', firstname=u'John', lastname=u'Doe',
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   140
        ...               works_for=c)
3674
387d51af966d backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3659
diff changeset
   141
3629
559cad62c786 backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   142
        """
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   143
        _check_cw_unsafe(kwargs)
5199
ebb50479d2ab [entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4933
diff changeset
   144
        cls = self.vreg['etypes'].etype_class(etype)
ebb50479d2ab [entity] move req.create_entity content to a cw_instantiate factory method on entity classes, so cube developpers will be able to easily customize instance creation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4933
diff changeset
   145
        return cls.cw_instantiate(self.execute, **kwargs)
2968
0e3460341023 somewhat painful backport of 3.5 branch, should mostly be ok
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2825
diff changeset
   146
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
   147
    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
   148
        """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
   149
        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
   150
        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
   151
            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
   152
2796
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   153
    def get_cache(self, cachename):
5290
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
   154
        """cachename should be dotted names as in :
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5199
diff changeset
   155
2796
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   156
        - cubicweb.mycache
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   157
        - cubes.blog.mycache
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   158
        - etc.
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   159
        """
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   160
        if cachename in CACHE_REGISTRY:
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   161
            cache = CACHE_REGISTRY[cachename]
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   162
        else:
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   163
            cache = CACHE_REGISTRY[cachename] = Cache()
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   164
        _now = datetime.now()
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   165
        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
   166
            ecache = self.execute(
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   167
                '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
   168
                {'name':cachename}).get_entity(0,0)
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   169
            cache.latest_cache_lookup = _now
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   170
            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
   171
                cache.clear()
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   172
                cache.cache_creation_date = _now
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   173
        return cache
14d2c69e12c4 move get_cache to base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   174
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
   175
    # 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
   176
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
   177
    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
   178
        """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
   179
        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
   180
        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
   181
        """
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
   182
        # 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
   183
        # 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
   184
        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
   185
            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
   186
            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
   187
        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
   188
            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
   189
        # 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
   190
        #     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
   191
        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
   192
            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
   193
                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
   194
            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
   195
                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
   196
        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
   197
        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
   198
            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
   199
        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
   200
            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
   201
            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
   202
        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
   203
            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
   204
        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
   205
            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
   206
        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
   207
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
   208
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
   209
    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
   210
        """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
   211
        args = []
4892
7ee8f128be9e [cleanup] use iteritems
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4891
diff changeset
   212
        for param, values in kwargs.iteritems():
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
   213
            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
   214
                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
   215
            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
   216
                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
   217
        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
   218
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
    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
   220
        """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
   221
        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
   222
        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
   223
        """
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
        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
   225
            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
   226
            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
   227
        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
   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
    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
   230
        """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
   231
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
        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
   233
        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
   234
        """
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
        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
   236
            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
   237
        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
   238
            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
   239
        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
   240
            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
   241
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
   242
    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
   243
        """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
   244
        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
   245
            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
   246
        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
   247
            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
   248
                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
   249
            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
   250
                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
   251
4891
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   252
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   253
    def rebuild_url(self, url, **newparams):
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   254
        """return the given url with newparams inserted. If any new params
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   255
        is already specified in the url, it's overriden by the new value
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   256
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   257
        newparams may only be mono-valued.
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   258
        """
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   259
        if isinstance(url, unicode):
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   260
            url = url.encode(self.encoding)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   261
        schema, netloc, path, query, fragment = urlsplit(url)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   262
        query = parse_qs(query)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   263
        # sort for testing predictability
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   264
        for key, val in sorted(newparams.iteritems()):
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   265
            query[key] = (self.url_quote(val),)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   266
        query = '&'.join(u'%s=%s' % (param, value)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   267
                         for param, values in query.items()
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   268
                         for value in values)
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   269
        return urlunsplit((schema, netloc, path, query, fragment))
90203ec7b3e3 [url] new rebuild_url method on base request
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   270
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
   271
    # 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
   272
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
    @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
   274
    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
   275
        """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
   276
        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
   277
        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
   278
            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
   279
            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
   280
            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
   281
            return userinfo
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4466
diff changeset
   282
        user = self.user
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
   283
        userinfo['login'] = user.login
3163
edfe43ceaa35 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   284
        userinfo['name'] = user.name()
edfe43ceaa35 backport 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2968
diff changeset
   285
        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
   286
        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
   287
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
    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
   289
        """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
   290
        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
   291
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
    # 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
   293
2823
a4e3e9b1a9cd backported from appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2808
diff changeset
   294
    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
   295
             initargs=None, **kwargs):
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   296
        """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
   297
        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
   298
        `__fallback_oid` is specified.
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   299
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   300
        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
   301
        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
   302
        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
   303
        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
   304
        object's call or cell_call method..
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   305
        """
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   306
        if initargs is None:
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   307
            initargs = kwargs
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   308
        else:
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   309
            initargs.update(kwargs)
3655
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3629
diff changeset
   310
        try:
4070
6e9f7abee4ef fix initargs handling
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   311
            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
   312
        except RegistryException:
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3629
diff changeset
   313
            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
   314
                                                 rset=rset, **initargs)
3655
af86ab65a282 3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3629
diff changeset
   315
        return view.render(**kwargs)
2823
a4e3e9b1a9cd backported from appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2808
diff changeset
   316
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
   317
    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
   318
        """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
   319
        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
   320
        """
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
        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
   322
            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
   323
                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
   324
                    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
   325
                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
   326
                    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
   327
            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
   328
        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
   329
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
    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
   331
        """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
   332
        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
   333
        """
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
        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
   335
            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
   336
        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
   337
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
    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
   339
        """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
   340
        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
   341
        """
4459
f628abfb3a6c backport stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4307
diff changeset
   342
        if num is not None:
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
   343
            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
   344
        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
   345
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
    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
   347
        """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
   348
        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
   349
        """
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
        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
   351
        # 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
   352
        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
   353
            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
   354
            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
   355
                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
   356
            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
   357
                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
   358
        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
   359
            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
   360
            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
   361
                # (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
   362
                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
   363
                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
   364
            except ValueError:
4222
5667f493c505 return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4070
diff changeset
   365
                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
   366
                                 % {'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
   367
        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
   368
            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
   369
            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
   370
            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
   371
                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
   372
            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
   373
        except ValueError:
4222
5667f493c505 return localized message on parse_datetime errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4070
diff changeset
   374
            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
   375
                             % {'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
   376
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
   377
    # 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
   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
    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
   380
        """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
   381
        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
   382
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
    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
   384
        """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
   385
        raise NotImplementedError
3659
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   386
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   387
    @property
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   388
    @deprecated('[3.6] use _cw.vreg.config')
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   389
    def config(self):
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   390
        return self.vreg.config
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   391
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   392
    @property
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   393
    @deprecated('[3.6] use _cw.vreg.schema')
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   394
    def schema(self):
993997b4b41d 3.6 update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3655
diff changeset
   395
        return self.vreg.schema