pyramid_cubicweb/core.py
author Christophe de Vienne <christophe@unlish.com>
Mon, 11 May 2015 15:22:24 +0200
changeset 11570 9957c610a047
parent 11568 a9940c6cd693
child 11575 97110b4af42f
permissions -rw-r--r--
Set version 0.3.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
     1
import itertools
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
     2
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     3
from contextlib import contextmanager
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
from warnings import warn
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
     5
from cgi import FieldStorage
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
import rql
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
from cubicweb.web.request import CubicWebRequestBase
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
from cubicweb import repoapi
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
import cubicweb
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
import cubicweb.web
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    14
from cubicweb.server import session as cwsession
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    15
11493
00e5cb9771c5 Put the login view in a separate module.
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    16
from pyramid import httpexceptions
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
11560
1a816189ceee [auth] remove dead code (closes #5230746)
David Douard <david.douard@logilab.fr>
parents: 11556
diff changeset
    18
from pyramid_cubicweb import tools
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
import logging
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
log = logging.getLogger(__name__)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    25
class Connection(cwsession.Connection):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    26
    """ A specialised Connection that access the session data through a
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    27
    property.
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    28
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    29
    This behavior makes sure the actual session data is not loaded until
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    30
    actually accessed.
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    31
    """
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    32
    def __init__(self, session, *args, **kw):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    33
        super(Connection, self).__init__(session, *args, **kw)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    34
        self._session = session
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    35
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    36
    def _get_session_data(self):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    37
        return self._session.data
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    38
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    39
    def _set_session_data(self, data):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    40
        pass
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    41
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    42
    _session_data = property(_get_session_data, _set_session_data)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    43
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    44
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    45
class Session(cwsession.Session):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    46
    """ A Session that access the session data through a property.
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    47
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    48
    Along with :class:`Connection`, it avoid any load of the pyramid session
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    49
    data until it is actually accessed.
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    50
    """
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    51
    def __init__(self, pyramid_request, user, repo):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    52
        super(Session, self).__init__(user, repo)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    53
        self._pyramid_request = pyramid_request
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    54
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    55
    def get_data(self):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    56
        if not getattr(self, '_protect_data_access', False):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    57
            self._data_accessed = True
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    58
            return self._pyramid_request.session
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    59
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    60
    def set_data(self, data):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    61
        if getattr(self, '_data_accessed', False):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    62
            self._pyramid_request.session.clear()
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    63
            self._pyramid_request.session.update(data)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    64
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    65
    data = property(get_data, set_data)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    66
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    67
    def new_cnx(self):
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    68
        self._protect_data_access = True
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    69
        try:
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    70
            return Connection(self)
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    71
        finally:
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    72
            self._protect_data_access = False
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    73
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
    74
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    75
def cw_headers(request):
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    76
    return itertools.chain(
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    77
        *[[(k, item) for item in v]
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    78
          for k, v in request.cw_request.headers_out.getAllRawHeaders()])
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    79
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    80
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
@contextmanager
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
def cw_to_pyramid(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
    83
    """ Context manager to wrap a call to the cubicweb API.
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    84
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    85
    All CW exceptions will be transformed into their pyramid equivalent.
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    86
    When needed, some CW reponse bits may be converted too (mainly headers)"""
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    87
    try:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    88
        yield
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    89
    except cubicweb.web.Redirect as ex:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
        assert 300 <= ex.status < 400
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    91
        raise httpexceptions.status_map[ex.status](
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
    92
            ex.location, headers=cw_headers(request))
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    93
    except cubicweb.web.StatusResponse as ex:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    94
        warn('[3.16] StatusResponse is deprecated use req.status_out',
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    95
             DeprecationWarning, stacklevel=2)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    96
        request.body = ex.content
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    97
        request.status_int = ex.status
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    98
    except cubicweb.web.Unauthorized as ex:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    99
        raise httpexceptions.HTTPForbidden(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   100
            request.cw_request._(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   101
                'You\'re not authorized to access this page. '
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   102
                'If you think you should, please contact the site '
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
   103
                'administrator.'),
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
   104
            headers=cw_headers(request))
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   105
    except cubicweb.web.Forbidden:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   106
        raise httpexceptions.HTTPForbidden(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   107
            request.cw_request._(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   108
                'This action is forbidden. '
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   109
                'If you think it should be allowed, please contact the site '
11556
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
   110
                'administrator.'),
1eeba41a2e95 On exceptions from CW, copy headers
Christophe de Vienne <christophe@unlish.com>
parents: 11553
diff changeset
   111
            headers=cw_headers(request))
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   112
    except (rql.BadRQLQuery, cubicweb.web.RequestError) as ex:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   113
        raise
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   114
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   115
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   116
class CubicWebPyramidRequest(CubicWebRequestBase):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   117
    """ A CubicWeb request that only wraps a pyramid request.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   118
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   119
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   120
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   121
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   122
    def __init__(self, request):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   123
        self._request = request
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   124
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   125
        self.path = request.upath_info
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   126
11498
a8aaff87c1fc Use registry['cubicweb.registry'] instead of registry['cubicweb.appli'].vreg because the application may not be present.
Christophe de Vienne <christophe@unlish.com>
parents: 11493
diff changeset
   127
        vreg = request.registry['cubicweb.registry']
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   128
        https = request.scheme == 'https'
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   129
11504
8701caf9edf0 Correctly pass the multiple parameters to the cubicweb request
Christophe de Vienne <christophe@unlish.com>
parents: 11502
diff changeset
   130
        post = request.params.mixed()
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   131
        headers_in = request.headers
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   132
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   133
        super(CubicWebPyramidRequest, self).__init__(vreg, https, post,
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   134
                                                     headers=headers_in)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   135
11508
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   136
        self.content = request.body_file_seekable
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   137
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   138
    def setup_params(self, params):
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   139
        self.form = {}
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   140
        for param, val in params.iteritems():
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   141
            if param in self.no_script_form_params and val:
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   142
                val = self.no_script_form_param(param, val)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   143
            if isinstance(val, FieldStorage) and val.file:
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   144
                val = (val.filename, val.file)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   145
            if param == '_cwmsgid':
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   146
                self.set_message_id(val)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   147
            elif param == '__message':
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   148
                warn('[3.13] __message in request parameter is deprecated '
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   149
                     '(may only be given to .build_url). Seeing this message '
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   150
                     'usualy means your application hold some <form> where '
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   151
                     'you should replace use of __message hidden input by '
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   152
                     'form.set_message, so new _cwmsgid mechanism is properly '
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   153
                     'used',
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   154
                     DeprecationWarning)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   155
                self.set_message(val)
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   156
            else:
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   157
                self.form[param] = val
ef8b9021b47b Fix POST handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11504
diff changeset
   158
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   159
    def is_secure(self):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   160
        return self._request.scheme == 'https'
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   161
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   162
    def relative_path(self, includeparams=True):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   163
        path = self._request.path[1:]
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   164
        if includeparams and self._request.query_string:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   165
            return '%s?%s' % (path, self._request.query_string)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   166
        return path
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   167
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   168
    def instance_uri(self):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   169
        return self._request.application_url
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   170
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   171
    def get_full_path(self):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   172
        path = self._request.path
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   173
        if self._request.query_string:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   174
            return '%s?%s' % (path, self._request.query_string)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   175
        return path
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   176
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   177
    def http_method(self):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   178
        return self._request.method
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   179
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   180
    def _set_status_out(self, value):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   181
        self._request.response.status_int = value
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   182
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   183
    def _get_status_out(self):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   184
        return self._request.response.status_int
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   185
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   186
    status_out = property(_get_status_out, _set_status_out)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   187
11566
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   188
    @property
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   189
    def message(self):
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   190
        """Returns a '<br>' joined list of the cubicweb current message and the
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   191
        default pyramid flash queue messages.
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   192
        """
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   193
        return u'\n<br>\n'.join(
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   194
            self._request.session.pop_flash()
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   195
            + self._request.session.pop_flash('cubicweb'))
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   196
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   197
    def set_message(self, msg):
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   198
        self.reset_message()
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   199
        self._request.session.flash(msg, 'cubicweb')
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   200
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   201
    def set_message_id(self, msgid):
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   202
        self.reset_message()
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   203
        self.set_message(
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   204
            self._request.session.pop(msgid, u''))
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   205
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   206
    def reset_message(self):
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   207
        self._request.session.pop_flash('cubicweb')
59548227ecc9 Use pyramid flash queue for messages
Christophe de Vienne <christophe@unlish.com>
parents: 11560
diff changeset
   208
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   209
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   210
def render_view(request, vid, **kwargs):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   211
    """ Helper function to render a CubicWeb view.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   212
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   213
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   214
    :param vid: A CubicWeb view id
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   215
    :param **kwargs: Keyword arguments to select and instanciate the view
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   216
    :returns: The rendered view content
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   217
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   218
    vreg = request.registry['cubicweb.registry']
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   219
    # XXX The select() function could, know how to handle a pyramid
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   220
    # request, and feed it directly to the views that supports it.
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   221
    # On the other hand, we could refine the View concept and decide it works
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   222
    # with a cnx, and never with a WebRequest
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   223
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   224
    with cw_to_pyramid(request):
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   225
        view = vreg['views'].select(vid, request.cw_request, **kwargs)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   226
        view.set_stream()
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   227
        view.render()
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   228
        return view._stream.getvalue()
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   229
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   230
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   231
def _cw_cnx(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   232
    """ Obtains a cw session from a pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   233
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   234
    The connection will be commited or rolled-back in a request finish
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   235
    callback (this is temporary, we should make use of the transaction manager
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   236
    in a later version).
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   237
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   238
    Not meant for direct use, use ``request.cw_cnx`` instead.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   239
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   240
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   241
    :returns type: :class:`cubicweb.server.session.Connection`
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   242
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   243
    cnx = repoapi.ClientConnection(request.cw_session)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   244
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   245
    def cleanup(request):
11524
54c83bfda277 Don't rollback if exception is HTTPSuccessful or HTTPRedirection
Christophe de Vienne <christophe@unlish.com>
parents: 11513
diff changeset
   246
        if (request.exception is not None and not isinstance(
54c83bfda277 Don't rollback if exception is HTTPSuccessful or HTTPRedirection
Christophe de Vienne <christophe@unlish.com>
parents: 11513
diff changeset
   247
            request.exception, (
54c83bfda277 Don't rollback if exception is HTTPSuccessful or HTTPRedirection
Christophe de Vienne <christophe@unlish.com>
parents: 11513
diff changeset
   248
                httpexceptions.HTTPSuccessful,
54c83bfda277 Don't rollback if exception is HTTPSuccessful or HTTPRedirection
Christophe de Vienne <christophe@unlish.com>
parents: 11513
diff changeset
   249
                httpexceptions.HTTPRedirection))):
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   250
            cnx.rollback()
11568
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11566
diff changeset
   251
        elif cnx._cnx.commit_state == 'uncommitable':
a9940c6cd693 Rollback 'uncommitable' cnx
Christophe de Vienne <christophe@unlish.com>
parents: 11566
diff changeset
   252
            cnx.rollback()
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   253
        else:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   254
            cnx.commit()
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   255
        cnx.__exit__(None, None, None)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   256
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   257
    request.add_finished_callback(cleanup)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   258
    cnx.__enter__()
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   259
    return cnx
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   260
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   261
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
   262
def repo_connect(request, repo, eid):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   263
    """A lightweight version of
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   264
    :meth:`cubicweb.server.repository.Repository.connect` that does not keep
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   265
    track of opened sessions, removing the need of closing them"""
11552
d92a608c1a16 [core] Use tools.cached_user_build for better performances
Christophe de Vienne <christophe@unlish.com>
parents: 11537
diff changeset
   266
    user = tools.cached_build_user(repo, eid)
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
   267
    session = Session(request, user, repo)
11552
d92a608c1a16 [core] Use tools.cached_user_build for better performances
Christophe de Vienne <christophe@unlish.com>
parents: 11537
diff changeset
   268
    tools.cnx_attach_entity(session, user)
11512
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   269
    # Calling the hooks should be done only once, disabling it completely for
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   270
    # now
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   271
    #with session.new_cnx() as cnx:
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   272
        #repo.hm.call_hooks('session_open', cnx)
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   273
        #cnx.commit()
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   274
    # repo._sessions[session.sessionid] = session
bb548010b390 Use lightweight sessions
Christophe de Vienne <christophe@unlish.com>
parents: 11508
diff changeset
   275
    return session
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   276
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   277
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   278
def _cw_session(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   279
    """Obtains a cw session from a pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   280
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   281
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   282
    :returns type: :class:`cubicweb.server.session.Session`
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   283
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   284
    Not meant for direct use, use ``request.cw_session`` instead.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   285
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   286
    repo = request.registry['cubicweb.repository']
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   287
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   288
    if not request.authenticated_userid:
11513
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   289
        session = repo_connect(
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
   290
            request, repo, eid=request.registry['cubicweb.anonymous_eid'])
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   291
    else:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   292
        session = request._cw_cached_session
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   293
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   294
    return session
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   295
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   296
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   297
def _cw_request(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   298
    """ Obtains a CubicWeb request wrapper for the pyramid request.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   299
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   300
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   301
    :return: A CubicWeb request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   302
    :returns type: :class:`CubicWebPyramidRequest`
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   303
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   304
    Not meant for direct use, use ``request.cw_request`` instead.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   305
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   306
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   307
    req = CubicWebPyramidRequest(request)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   308
    req.set_cnx(request.cw_cnx)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   309
    return req
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   310
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   311
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   312
def get_principals(login, request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   313
    """ Returns the group names of the authenticated user.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   314
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   315
    This function is meant to be used as an authentication policy callback.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   316
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   317
    It also pre-open the cubicweb session and put it in
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   318
    request._cw_cached_session for later usage by :func:`_cw_session`.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   319
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   320
    .. note::
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   321
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   322
        It the default authentication policy is not used, make sure this
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   323
        function gets called by the active authentication policy.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   324
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   325
    :param login: A cubicweb user eid
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   326
    :param request: A pyramid request
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   327
    :returns: A list of group names
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   328
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   329
    repo = request.registry['cubicweb.repository']
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   330
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   331
    try:
11553
a322a02ca301 [core] Protect session data from unwanted loading.
Christophe de Vienne <christophe@unlish.com>
parents: 11552
diff changeset
   332
        session = repo_connect(request, repo, eid=login)
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   333
        request._cw_cached_session = session
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   334
    except:
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   335
        log.exception("Failed")
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   336
        raise
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   337
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   338
    return session.user.groups
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   339
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   340
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   341
def includeme(config):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   342
    """ Enables the core features of Pyramid CubicWeb.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   343
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   344
    Automatically called by the 'pyramid' command, or via
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   345
    ``config.include('pyramid_cubicweb.code')``. In the later case,
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   346
    the following registry entries must be defined first:
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   347
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   348
    'cubicweb.config'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   349
        A cubicweb 'config' instance.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   350
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   351
    'cubicweb.repository'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   352
        The correponding cubicweb repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   353
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   354
    'cubicweb.registry'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   355
        The vreg.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11536
diff changeset
   356
    """
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   357
    repo = config.registry['cubicweb.repository']
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   358
11513
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   359
    with repo.internal_cnx() as cnx:
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   360
        login = config.registry['cubicweb.config'].anonymous_user()[0]
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   361
        config.registry['cubicweb.anonymous_eid'] = cnx.find(
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   362
            'CWUser', login=login).one().eid
0170f8a55620 Optimise repo_connect by skipping authenticate_user
Christophe de Vienne <christophe@unlish.com>
parents: 11512
diff changeset
   363
11492
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   364
    config.add_request_method(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   365
        _cw_session, name='cw_session', property=True, reify=True)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   366
    config.add_request_method(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   367
        _cw_cnx, name='cw_cnx', property=True, reify=True)
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   368
    config.add_request_method(
b0b8942cdb80 Separate into 4 modules
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
   369
        _cw_request, name='cw_request', property=True, reify=True)
11502
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   370
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   371
    cwcfg = config.registry['cubicweb.config']
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   372
    for cube in cwcfg.cubes():
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   373
        pkgname = 'cubes.' + cube
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   374
        mod = __import__(pkgname)
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   375
        mod = getattr(mod, cube)
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   376
        if hasattr(mod, 'includeme'):
e4682c567e86 If any cube has a 'includeme' attribute, call config.include on it
Christophe de Vienne <christophe@unlish.com>
parents: 11500
diff changeset
   377
            config.include('cubes.' + cube)