cubicweb/web/application.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 22 Mar 2019 17:42:36 +0100
changeset 12542 85194bd49119
parent 12503 b01dd0ef43aa
child 12567 26744ad37953
permissions -rw-r--r--
Drop more deprecated code This follows up on changeset a8c1ea390400, in which code deprecated using logilab.common.deprecation got dropped. Now we also drop code deprecated using stdlib's warn(<msg>, DeprecationWarning). Notice that, as a consequence of dropping old/new etypes aliases in cubicweb/schema.py, we drop the import ETYPE_NAME_MAP (no longer needed); but since other modules imported that name from cubicweb.schema, we need to update the import statement to use "cubicweb" directly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
     1
# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5377
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: 5377
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: 5377
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: 5377
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: 5377
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: 5377
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: 5377
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: 5377
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: 5377
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5377
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: 5377
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: 5377
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: 5377
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: 5377
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: 5377
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5655
diff changeset
    18
"""CubicWeb web client application object"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11725
diff changeset
    20
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    21
import contextlib
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    22
import json
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
from time import clock, time
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    25
from contextlib import contextmanager
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
    26
12503
b01dd0ef43aa Drop most deprecated code in cubicweb.web
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12207
diff changeset
    27
from six import text_type, binary_type
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
    28
from six.moves import http_client
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from rql import BadRQLQuery
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    32
from cubicweb import set_log_methods
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    33
from cubicweb import (
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    34
    CW_EVENT_MANAGER, ValidationError, Unauthorized, Forbidden,
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    35
    AuthenticationError, NoSelectableObject)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    36
from cubicweb.repoapi import anonymous_cnx
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    37
from cubicweb.web import cors
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    38
from cubicweb.web import (
12542
85194bd49119 Drop more deprecated code
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12503
diff changeset
    39
    LOGGER, DirectResponse, Redirect, NotFound, LogOut,
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9355
diff changeset
    40
    RemoteCallFailed, InvalidSession, RequestError, PublishException)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
    41
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
# make session manager available through a global variable so the debug view can
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
# print information about web session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
SESSION_MANAGER = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    46
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    47
@contextmanager
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    48
def anonymized_request(req):
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    49
    from cubicweb.web.views.authentication import Session
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    50
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    51
    orig_cnx = req.cnx
12207
2fc04786dd36 [web] fix session /cnx mix in anonymized_request
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 12050
diff changeset
    52
    anon_cnx = anonymous_cnx(orig_cnx.repo)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    53
    try:
10355
60b8204fcca3 [session] all cnx._session become cnx.session
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    54
        with anon_cnx:
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    55
            # web request expect a session attribute on cnx referencing the web session
12207
2fc04786dd36 [web] fix session /cnx mix in anonymized_request
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 12050
diff changeset
    56
            anon_cnx.session = Session(orig_cnx.repo, anon_cnx.user)
11699
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
    57
            req.set_cnx(anon_cnx)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    58
            yield req
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    59
    finally:
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    60
        req.set_cnx(orig_cnx)
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    61
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
class CookieSessionHandler(object):
6680
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    64
    """a session handler using a cookie to store the session identifier"""
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    65
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    def __init__(self, appli):
9032
629a8d49d6f5 [auth] pass `repo` instead of `vreg` to SessionManager and AuthenticationManager
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9031
diff changeset
    67
        self.repo = appli.repo
2706
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    68
        self.vreg = appli.vreg
10565
f5063eae939e [web/sessions] the session managers are definitely not components
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10411
diff changeset
    69
        self.session_manager = self.vreg['sessions'].select('sessionmanager',
f5063eae939e [web/sessions] the session managers are definitely not components
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10411
diff changeset
    70
                                                            repo=self.repo)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        global SESSION_MANAGER
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        SESSION_MANAGER = self.session_manager
5000
f1a10b41417a [test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4914
diff changeset
    73
        if self.vreg.config.mode != 'test':
f1a10b41417a [test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4914
diff changeset
    74
            # don't try to reset session manager during test, this leads to
f1a10b41417a [test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4914
diff changeset
    75
            # weird failures when running multiple tests
f1a10b41417a [test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4914
diff changeset
    76
            CW_EVENT_MANAGER.bind('after-registry-reload',
f1a10b41417a [test] don't try to reset session manager during test,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4914
diff changeset
    77
                                  self.reset_session_manager)
2706
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    78
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    79
    def reset_session_manager(self):
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    80
        data = self.session_manager.dump_data()
10740
59e89db3a67d [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10604
diff changeset
    81
        self.session_manager = self.vreg['sessions'].select('sessionmanager',
59e89db3a67d [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10604
diff changeset
    82
                                                            repo=self.repo)
2706
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    83
        self.session_manager.restore_data(data)
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    84
        global SESSION_MANAGER
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    85
        SESSION_MANAGER = self.session_manager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
5325
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    87
    @property
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    88
    def clean_sessions_interval(self):
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    89
        return self.session_manager.clean_sessions_interval
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    90
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    def clean_sessions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        """cleanup sessions which has not been unused since a given amount of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        time
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        self.session_manager.clean_sessions()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    96
6680
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    97
    def session_cookie(self, req):
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    98
        """return a string giving the name of the cookie used to store the
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
    99
        session identifier.
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   100
        """
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   101
        return '__%s_session' % self.vreg.config.appid
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   102
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   103
    def get_session(self, req):
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   104
        """Return a session object corresponding to credentials held by the req
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        Session id is searched from :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        - # form variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        - cookie
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   110
        If no session id is found, try opening a new session with credentials
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   111
        found in the request.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   113
        Raises AuthenticationError if no session can be found or created.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        cookie = req.get_cookie()
6680
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   116
        sessioncookie = self.session_cookie(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        try:
6680
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   118
            sessionid = str(cookie[sessioncookie].value)
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   119
            session = self.get_session_by_id(req, sessionid)
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   120
        except (KeyError, InvalidSession):  # no valid session cookie
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
            session = self.open_session(req)
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   122
        return session
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   124
    def get_session_by_id(self, req, sessionid):
7589
d3459fe041f0 [web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7428
diff changeset
   125
        session = self.session_manager.get_session(req, sessionid)
d3459fe041f0 [web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7428
diff changeset
   126
        session.mtime = time()
d3459fe041f0 [web session] fix bad cleanup of session without a connection set. Closes #1772125
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7428
diff changeset
   127
        return session
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   128
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   129
    def open_session(self, req):
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   130
        session = self.session_manager.open_session(req)
6680
b62ebf1d9388 [web session] fix '#1375582: sometime have to relogin since secure session cookie are used' by having different session cookie for http/https version. Also add application name to avoid potential pb when developping differents appls on the same port.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6582
diff changeset
   131
        sessioncookie = self.session_cookie(req)
11913
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   132
        secure = req.base_url().startswith('https://')
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   133
        req.set_cookie(sessioncookie, session.sessionid,
10001
1245357b3b3e [web] add support for HttpOnly cookie flag
Julien Cristau <julien.cristau@logilab.fr>
parents: 9897
diff changeset
   134
                       maxage=None, secure=secure, httponly=True)
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   135
        if not session.anonymous_session:
9018
9deb024a96c0 [session-handler] use session directly to update last usage
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9017
diff changeset
   136
            self.session_manager.postlogin(req, session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        return session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
4911
898c35be5873 #750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4709
diff changeset
   139
    def logout(self, req, goto_url):
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2293
diff changeset
   140
        """logout from the instance by cleaning the session and raising
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        `AuthenticationError`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        """
5494
f3bb53f1737c [web session] fix potential key error on logout (occurs once the session has been transparently reconnected, hence session.sessionid and session.cnx.sessionid differs)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   143
        self.session_manager.close_session(req.session)
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   144
        req.remove_cookie(self.session_cookie(req))
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   145
        raise LogOut(url=goto_url)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   147
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   148
    # only defining here to prevent pylint from complaining
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   149
    info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   150
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
class CubicWebPublisher(object):
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   153
    """the publisher is a singleton hold by the web frontend, and is responsible
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   154
    to publish HTTP request.
8997
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   155
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   156
    The http server will call its main entry point ``application.handle_request``.
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   157
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   158
    .. automethod:: cubicweb.web.application.CubicWebPublisher.main_handle_request
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   159
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   160
    You have to provide both a repository and web-server config at
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   161
    initialization. In all in one instance both config will be the same.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
    """
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   163
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   164
    def __init__(self, repo, config, session_handler_fact=CookieSessionHandler):
4484
d87989d91635 fix duplicated vregistry initialization during tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   165
        self.info('starting web instance from %s', config.apphome)
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   166
        self.repo = repo
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   167
        self.vreg = repo.vreg
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   168
        # get instance's schema
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   169
        if not self.vreg.initialized:
5650
86e874fe30ea [web] cleanup use of config in web application initialisation
Julien Jehannet <julien.jehannet@logilab.fr>
parents: 5587
diff changeset
   170
            config.init_cubes(self.repo.get_cubes())
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   171
            self.vreg.init_properties(self.repo.properties())
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   172
            self.vreg.set_schema(self.repo.get_schema())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        # set the correct publish method
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        if config['query-log-file']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
            from threading import Lock
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
            self._query_log = open(config['query-log-file'], 'a')
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   177
            self.handle_request = self.log_handle_request
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   178
            self._logfile_lock = Lock()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
            self._query_log = None
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   181
            self.handle_request = self.main_handle_request
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
        # instantiate session and url resolving helpers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        self.session_handler = session_handler_fact(self)
2685
0518ca8f63e3 [autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2666
diff changeset
   184
        self.set_urlresolver()
2705
30bcdbd92820 [events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2685
diff changeset
   185
        CW_EVENT_MANAGER.bind('after-registry-reload', self.set_urlresolver)
2685
0518ca8f63e3 [autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2666
diff changeset
   186
0518ca8f63e3 [autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2666
diff changeset
   187
    def set_urlresolver(self):
2887
1282dc6525c5 give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2867
diff changeset
   188
        self.url_resolver = self.vreg['components'].select('urlpublisher',
1282dc6525c5 give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2867
diff changeset
   189
                                                           vreg=self.vreg)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   190
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   191
    def get_session(self, req):
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   192
        """Return a session object corresponding to credentials held by the req
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   193
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   194
        May raise AuthenticationError.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        """
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   196
        return self.session_handler.get_session(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    # publish methods #########################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   199
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   200
    def log_handle_request(self, req):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        """wrapper around _publish to log all queries executed for a given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        accessed path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
        """
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   204
        def wrap_set_cnx(func):
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   205
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   206
            def wrap_execute(cnx):
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   207
                orig_execute = cnx.execute
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   208
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   209
                def execute(rql, kwargs=None, build_descr=True):
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   210
                    tstart, cstart = time(), clock()
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   211
                    rset = orig_execute(rql, kwargs, build_descr=build_descr)
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   212
                    cnx.executed_queries.append((rql, kwargs, time() - tstart, clock() - cstart))
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   213
                    return rset
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   214
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   215
                return execute
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   216
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   217
            def set_cnx(cnx):
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   218
                func(cnx)
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   219
                cnx.execute = wrap_execute(cnx)
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   220
                cnx.executed_queries = []
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   221
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   222
            return set_cnx
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   223
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   224
        req.set_cnx = wrap_set_cnx(req.set_cnx)
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   225
        tstart, cstart = time(), clock()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
        try:
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   227
            return self.main_handle_request(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        finally:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
            cnx = req.cnx
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   230
            if cnx and cnx.executed_queries:
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   231
                with self._logfile_lock:
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   232
                    tend, cend = time(), clock()
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   233
                    try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   234
                        result = ['\n' + '*' * 80]
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   235
                        result.append('%s -- (%.3f sec, %.3f CPU sec)' % (
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   236
                            req.url(), tend - tstart, cend - cstart))
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   237
                        result += ['%s %s -- (%.3f sec, %.3f CPU sec)' % q
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   238
                                   for q in cnx.executed_queries]
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   239
                        cnx.executed_queries = []
11163
141e96f93c4d [web/application] fix query log handling on python3
Julien Cristau <julien.cristau@logilab.fr>
parents: 11057
diff changeset
   240
                        self._query_log.write('\n'.join(result))
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   241
                        self._query_log.flush()
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   242
                    except Exception:
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   243
                        self.exception('error while logging queries')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   245
    def main_handle_request(self, req):
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   246
        """Process an HTTP request `req`
8997
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   247
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   248
        :type req: `web.Request`
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   249
        :param req: the request object
8997
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   250
9175
a7412e884d7b fix typos in docstring, doc and comments
Julien Cristau <julien.cristau@logilab.fr>
parents: 8997
diff changeset
   251
        It returns the content of the http response. HTTP header and status are
a7412e884d7b fix typos in docstring, doc and comments
Julien Cristau <julien.cristau@logilab.fr>
parents: 8997
diff changeset
   252
        set on the Request object.
8997
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   253
        """
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   254
        if req.authmode == 'http':
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   255
            # activate realm-based auth
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   256
            realm = self.vreg.config['realm']
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   257
            req.set_header('WWW-Authenticate', [('Basic', {'realm': realm})], raw=False)
10744
c5dc40988e6e [web] assert that CubicWebPublisher.handle_request returns bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10740
diff changeset
   258
        content = b''
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   259
        try:
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   260
            try:
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   261
                session = self.get_session(req)
11699
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   262
                cnx = session.new_cnx()
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   263
                with cnx:  # may need an open connection to access to e.g. properties
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   264
                    req.set_cnx(cnx)
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   265
                cnx._open = None  # XXX needed to reuse it a few line later :'(
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   266
            except AuthenticationError:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   267
                # Keep the dummy session set at initialisation.  such session will work to some
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   268
                # extend but raise an AuthenticationError on any database access.
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   269
                # XXX We want to clean up this approach in the future. But several cubes like
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   270
                # registration or forgotten password rely on this principle.
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   271
                @contextlib.contextmanager
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   272
                def dummy():
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   273
                    yield
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   274
                cnx = dummy()
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   275
            # nested try to allow LogOut to delegate logic to AuthenticationError
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   276
            # handler
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   277
            try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   278
                # Try to generate the actual request content
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   279
                with cnx:
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   280
                    content = self.core_handle(req)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   281
            # Handle user log-out
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   282
            except LogOut as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   283
                # When authentification is handled by cookie the code that
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   284
                # raised LogOut must has invalidated the cookie. We can just
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   285
                # reload the original url without authentification
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   286
                if self.vreg.config['auth-mode'] == 'cookie' and ex.url:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   287
                    req.headers_out.setHeader('location', str(ex.url))
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   288
                if ex.status is not None:
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   289
                    req.status_out = http_client.SEE_OTHER
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   290
                # When the authentification is handled by http we must
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   291
                # explicitly ask for authentification to flush current http
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   292
                # authentification information
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   293
                else:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   294
                    # Render "logged out" content.
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   295
                    # assignement to ``content`` prevent standard
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   296
                    # AuthenticationError code to overwrite it.
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   297
                    content = self.loggedout_content(req)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   298
                    # let the explicitly reset http credential
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   299
                    raise AuthenticationError()
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   300
        except Redirect as ex:
8470
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   301
            # authentication needs redirection (eg openid)
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   302
            content = self.redirect_handler(req, ex)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   303
        # Wrong, absent or Reseted credential
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   304
        except AuthenticationError:
11913
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   305
            # We assume here that in http auth mode the user *May* provide
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   306
            # Authentification Credential if asked kindly.
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   307
            if self.vreg.config['auth-mode'] == 'http':
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   308
                req.status_out = http_client.UNAUTHORIZED
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   309
            # In the other case (coky auth) we assume that there is no way
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   310
            # for the user to provide them...
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   311
            # XXX But WHY ?
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   312
            else:
11913
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   313
                req.status_out = http_client.FORBIDDEN
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   314
            # If previous error handling already generated a custom content
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   315
            # do not overwrite it. This is used by LogOut Except
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   316
            # XXX ensure we don't actually serve content
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   317
            if not content:
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   318
                content = self.need_login_content(req)
10744
c5dc40988e6e [web] assert that CubicWebPublisher.handle_request returns bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10740
diff changeset
   319
        assert isinstance(content, binary_type)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   320
        return content
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   321
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   322
    def core_handle(self, req):
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   323
        """method called by the main publisher to process <req> relative path
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   324
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
        should return a string containing the resulting page or raise a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
        `NotFound` exception
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
        :type req: `web.Request`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
        :param req: the request object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
        :rtype: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
        :return: the result of the pusblished url
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
        """
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   334
        path = req.relative_path(False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
        # don't log form values they may contains sensitive information
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   336
        self.debug('publish "%s" (%s, form params: %s)', path,
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   337
                   req.session.sessionid, list(req.form))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
        # remove user callbacks on a new request (except for json controllers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
        # to avoid callbacks being unregistered before they could be called)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
        tstart = clock()
5865
af414723598d [publisher] avoid useless rollback after successful commit, which clutters debug logs and may also not be cost-free
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5721
diff changeset
   341
        commited = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
        try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   343
            # standard processing of the request
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
            try:
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   345
                # apply CORS sanity checks
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   346
                cors.process_request(req, self.vreg.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
                ctrlid, rset = self.url_resolver.process(req, path)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   348
                try:
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   349
                    controller = self.vreg['controllers'].select(ctrlid, req,
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   350
                                                                 appli=self)
2058
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   351
                except NoSelectableObject:
7ef12c03447c nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
   352
                    raise Unauthorized(req._('not authorized'))
581
09f87f2c535e update_search_state in the publisher since it should be done whatever the controller
sylvain.thenault@logilab.fr
parents: 168
diff changeset
   353
                req.update_search_state()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
                result = controller.publish(rset=rset)
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   355
            except cors.CORSPreflight:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   356
                # Return directly an empty 200
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   357
                req.status_out = 200
10744
c5dc40988e6e [web] assert that CubicWebPublisher.handle_request returns bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10740
diff changeset
   358
                result = b''
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   359
            except Redirect as ex:
8470
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   360
                # Redirect may be raised by edit controller when everything went
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   361
                # fine, so attempt to commit
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   362
                result = self.redirect_handler(req, ex)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   363
            if req.cnx:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   364
                txuuid = req.cnx.commit()
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   365
                commited = True
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   366
                if txuuid is not None:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   367
                    req.data['last_undoable_transaction'] = txuuid
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   368
        # error case
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   369
        except NotFound as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   370
            result = self.notfound_content(req)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   371
            req.status_out = ex.status
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   372
        except ValidationError as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   373
            result = self.validation_error_handler(req, ex)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   374
        except RemoteCallFailed as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   375
            result = self.ajax_error_handler(req, ex)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   376
        except Unauthorized as ex:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   377
            req.data['errmsg'] = req._(
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   378
                'You\'re not authorized to access this page. '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   379
                'If you think you should, please contact the site administrator.')
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   380
            req.status_out = http_client.FORBIDDEN
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   381
            result = self.error_handler(req, ex, tb=False)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   382
        except Forbidden as ex:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   383
            req.data['errmsg'] = req._(
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   384
                'This action is forbidden. '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   385
                'If you think it should be allowed, please contact the site administrator.')
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   386
            req.status_out = http_client.FORBIDDEN
8602
d066ba3bb07d [web] add a Forbidden exception
David Douard <david.douard@logilab.fr>
parents: 8470
diff changeset
   387
            result = self.error_handler(req, ex, tb=False)
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   388
        except (BadRQLQuery, RequestError) as ex:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   389
            result = self.error_handler(req, ex, tb=False)
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   390
        # pass through exception
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   391
        except DirectResponse:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   392
            if req.cnx:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   393
                req.cnx.commit()
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   394
            raise
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   395
        except (AuthenticationError, LogOut):
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   396
            # the rollback is handled in the finally
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   397
            raise
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   398
        # Last defense line
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8694
diff changeset
   399
        except BaseException as ex:
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   400
            req.status_out = http_client.INTERNAL_SERVER_ERROR
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   401
            result = self.error_handler(req, ex, tb=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
        finally:
5865
af414723598d [publisher] avoid useless rollback after successful commit, which clutters debug logs and may also not be cost-free
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5721
diff changeset
   403
            if req.cnx and not commited:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
                    req.cnx.rollback()
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7589
diff changeset
   406
                except Exception:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   407
                    pass  # ignore rollback error at this point
8267
486386d9f836 [web] Exposes the undo feature to user through a undo-history view (closes #893940)
Anthony Truchet <anthony.truchet@logilab.fr>
parents: 8190
diff changeset
   408
        self.add_undo_link_to_msg(req)
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   409
        self.debug('query %s executed in %s sec', path, clock() - tstart)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   410
        return result
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   411
8470
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   412
    # Error handlers
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   413
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   414
    def redirect_handler(self, req, ex):
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   415
        """handle redirect
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   416
        - comply to ex status
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   417
        - set header field
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   418
        - return empty content
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   419
        """
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   420
        self.debug('redirecting to %s', str(ex.location))
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   421
        req.headers_out.setHeader('location', str(ex.location))
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   422
        assert 300 <= ex.status < 400
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   423
        req.status_out = ex.status
10820
fef0ab4c4f94 [web/application] return bytes from redirect_handler and validation_error_handler
Julien Cristau <julien.cristau@logilab.fr>
parents: 10777
diff changeset
   424
        return b''
8470
a85e107947e6 [web app] handle Redirect raised during session establishment, as this may be used by eg openid authentication. Closes #2430018
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8466
diff changeset
   425
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
    def validation_error_handler(self, req, ex):
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   427
        ex.translate(req._)  # translate messages using ui language
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
        if '__errorurl' in req.form:
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3408
diff changeset
   429
            forminfo = {'error': ex,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
                        'values': req.form,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   431
                        'eidmap': req.data.get('eidmap', {})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
                        }
5223
6abd6e3599f4 #773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5174
diff changeset
   433
            req.session.data[req.form['__errorurl']] = forminfo
4679
d8ad65dab3e9 remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4639
diff changeset
   434
            # XXX form session key / __error_url should be differentiated:
d8ad65dab3e9 remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4639
diff changeset
   435
            # session key is 'url + #<form dom id', though we usually don't want
d8ad65dab3e9 remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4639
diff changeset
   436
            # the browser to move to the form since it hides the global
d8ad65dab3e9 remove #<formid> from url used to redirect after a validation error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4639
diff changeset
   437
            # messages.
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   438
            location = req.form['__errorurl'].rsplit('#', 1)[0]
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   439
            req.headers_out.setHeader('location', str(location))
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   440
            req.status_out = http_client.SEE_OTHER
10820
fef0ab4c4f94 [web/application] return bytes from redirect_handler and validation_error_handler
Julien Cristau <julien.cristau@logilab.fr>
parents: 10777
diff changeset
   441
            return b''
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   442
        req.status_out = http_client.CONFLICT
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   443
        return self.error_handler(req, ex, tb=False)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   444
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   445
    def error_handler(self, req, ex, tb=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   446
        excinfo = sys.exc_info()
8466
92c668170ef9 [web app] only log tb when explicitly asked
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8443
diff changeset
   447
        if tb:
92c668170ef9 [web app] only log tb when explicitly asked
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8443
diff changeset
   448
            self.exception(repr(ex))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
        req.set_header('Cache-Control', 'no-cache')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
        req.remove_header('Etag')
8753
b61a7c6352f2 [web publish] in case of error, ensure proper http status is set and Content-disposition header is reseted. Closes #2553066
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8711
diff changeset
   451
        req.remove_header('Content-disposition')
4897
e402e0b32075 [web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4709
diff changeset
   452
        req.reset_message()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
        req.reset_headers()
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   454
        if req.ajax_request:
8709
8dc0b8d92694 [web application] Fix missing self. in error_handler
Vincent Michel <vincent.michel@logilab.fr>
parents: 8602
diff changeset
   455
            return self.ajax_error_handler(req, ex)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
            req.data['ex'] = ex
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
            if tb:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
                req.data['excinfo'] = excinfo
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   460
            errview = self.vreg['views'].select('error', req)
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   461
            template = self.main_template_id(req)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   462
            content = self.vreg['views'].main_template(req, template, view=errview)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7589
diff changeset
   463
        except Exception:
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   464
            content = self.vreg['views'].main_template(req, 'error-template')
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9355
diff changeset
   465
        if isinstance(ex, PublishException) and ex.status is not None:
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   466
            req.status_out = ex.status
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   467
        return content
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   468
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   469
    def add_undo_link_to_msg(self, req):
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   470
        txuuid = req.data.get('last_undoable_transaction')
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   471
        if txuuid is not None:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   472
            msg = u'<span class="undo">[<a href="%s">%s</a>]</span>' % (
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   473
                req.build_url('undo', txuuid=txuuid), req._('undo'))
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   474
            req.append_to_redirect_message(msg)
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   475
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   476
    def ajax_error_handler(self, req, ex):
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   477
        req.set_header('content-type', 'application/json')
10604
d4bf85db41f2 [py3k] import HTTP client constants and exceptions using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10565
diff changeset
   478
        status = http_client.INTERNAL_SERVER_ERROR
9428
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9355
diff changeset
   479
        if isinstance(ex, PublishException) and ex.status is not None:
946aded614f3 [web error] exception may not have a 'status' attribute, generating an AttributeError that hides the original error. Closes #3381670
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9355
diff changeset
   480
            status = ex.status
9727
33f6ad4c75a5 [web/ajax] don't override any status code with 500 in ajax_error_handler
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   481
        if req.status_out < 400:
33f6ad4c75a5 [web/ajax] don't override any status code with 500 in ajax_error_handler
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   482
            # don't overwrite it if it's already set
33f6ad4c75a5 [web/ajax] don't override any status code with 500 in ajax_error_handler
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   483
            req.status_out = status
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   484
        json_dumper = getattr(ex, 'dumps', lambda: json.dumps({'reason': text_type(ex)}))
10777
eabacd3c6f16 [web] CubicWebPublisher.ajax_error_handler must return bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10744
diff changeset
   485
        return json_dumper().encode('utf-8')
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   486
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   487
    # special case handling
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   488
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
    def need_login_content(self, req):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   490
        return self.vreg['views'].main_template(req, 'login')
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   491
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
    def loggedout_content(self, req):
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   493
        return self.vreg['views'].main_template(req, 'loggedout')
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   494
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
    def notfound_content(self, req):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
        req.form['vid'] = '404'
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   497
        view = self.vreg['views'].select('404', req)
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   498
        template = self.main_template_id(req)
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   499
        return self.vreg['views'].main_template(req, template, view=view)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   501
    # template stuff
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   502
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   503
    def main_template_id(self, req):
2263
1f59cd5b710f accept a __template parameter that specifies a different (main) template
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1977
diff changeset
   504
        template = req.form.get('__template', req.property_value('ui.main-template'))
2650
18aec79ec3a3 R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2613
diff changeset
   505
        if template not in self.vreg['views']:
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   506
            template = 'main-template'
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   507
        return template
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   508
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   509
    # these are overridden by set_log_methods below
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   510
    # only defining here to prevent pylint from complaining
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   511
    info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   512
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
set_log_methods(CubicWebPublisher, LOGGER)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
set_log_methods(CookieSessionHandler, LOGGER)