cubicweb/web/application.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 05 Apr 2019 17:58:19 +0200
changeset 12567 26744ad37953
parent 12542 85194bd49119
child 12681 42afaaf708d8
permissions -rw-r--r--
Drop python2 support This mostly consists in removing the dependency on "six" and updating the code to use only Python3 idioms. Notice that we previously used TemporaryDirectory from cubicweb.devtools.testlib for compatibility with Python2. We now directly import it from tempfile.
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
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12542
diff changeset
    22
import http.client as http_client
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    23
import json
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
import sys
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
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
    27
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
from rql import BadRQLQuery
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    30
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
    31
from cubicweb import (
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    32
    CW_EVENT_MANAGER, ValidationError, Unauthorized, Forbidden,
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    33
    AuthenticationError, NoSelectableObject)
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    34
from cubicweb.repoapi import anonymous_cnx
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
    35
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
    36
from cubicweb.web import (
12542
85194bd49119 Drop more deprecated code
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12503
diff changeset
    37
    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
    38
    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
    39
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
# 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
    41
# print information about web session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
SESSION_MANAGER = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    44
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    45
@contextmanager
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    46
def anonymized_request(req):
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    47
    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
    48
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    49
    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
    50
    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
    51
    try:
10355
60b8204fcca3 [session] all cnx._session become cnx.session
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 10354
diff changeset
    52
        with anon_cnx:
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    53
            # 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
    54
            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
    55
            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
    56
            yield req
7876
df15d194a134 [views] implement json / jsonp export views (closes #1942658)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7855
diff changeset
    57
    finally:
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
    58
        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
    59
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
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
    62
    """a session handler using a cookie to store the session identifier"""
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    63
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    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
    65
        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
    66
        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
    67
        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
    68
                                                            repo=self.repo)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        global SESSION_MANAGER
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        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
    71
        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
    72
            # 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
    73
            # 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
    74
            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
    75
                                  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
    76
09baf5175196 [web session] proper reloading of the session manager on vreg update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2705
diff changeset
    77
    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
    78
        data = self.session_manager.dump_data()
10740
59e89db3a67d [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10604
diff changeset
    79
        self.session_manager = self.vreg['sessions'].select('sessionmanager',
59e89db3a67d [web] unicode → six.text_type
Julien Cristau <julien.cristau@logilab.fr>
parents: 10604
diff changeset
    80
                                                            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
    81
        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
    82
        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
    83
        SESSION_MANAGER = self.session_manager
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
5325
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    85
    @property
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    86
    def clean_sessions_interval(self):
f1c660e1169e [web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5283
diff changeset
    87
        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
    88
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    def clean_sessions(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        """cleanup sessions which has not been unused since a given amount of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        time
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
        self.session_manager.clean_sessions()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    94
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
    95
    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
    96
        """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
    97
        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
    98
        """
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
        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
   100
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   101
    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
   102
        """Return a session object corresponding to credentials held by the req
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        Session id is searched from :
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        - # form variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        - cookie
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   108
        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
   109
        found in the request.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   111
        Raises AuthenticationError if no session can be found or created.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        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
   114
        sessioncookie = self.session_cookie(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        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
   116
            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
   117
            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
   118
        except (KeyError, InvalidSession):  # no valid session cookie
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
            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
   120
        return session
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
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
    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
   123
        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
   124
        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
   125
        return session
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   126
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   127
    def open_session(self, req):
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   128
        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
   129
        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
   130
        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
   131
        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
   132
                       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
   133
        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
   134
            self.session_manager.postlogin(req, session)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        return session
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
4911
898c35be5873 #750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4709
diff changeset
   137
    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
   138
        """logout from the instance by cleaning the session and raising
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        `AuthenticationError`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
        """
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
   141
        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
   142
        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
   143
        raise LogOut(url=goto_url)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   145
    # 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
   146
    # 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
   147
    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
   148
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
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
   151
    """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
   152
    to publish HTTP request.
8997
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   153
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   154
    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
   155
525915f8bc1c [web/application] add some minimal documentation
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8753
diff changeset
   156
    .. 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
   157
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   158
    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
   159
    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
   160
    """
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   161
9031
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   162
    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
   163
        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
   164
        self.repo = repo
6ff29f2879da web/application: instantiate the repository outside of CubicWebPublisher
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9030
diff changeset
   165
        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
   166
        # 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
   167
        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
   168
            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
   169
            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
   170
            self.vreg.set_schema(self.repo.get_schema())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        # set the correct publish method
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        if config['query-log-file']:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
            from threading import Lock
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
            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
   175
            self.handle_request = self.log_handle_request
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   176
            self._logfile_lock = Lock()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
            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
   179
            self.handle_request = self.main_handle_request
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
        # instantiate session and url resolving helpers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
        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
   182
        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
   183
        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
   184
0518ca8f63e3 [autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2666
diff changeset
   185
    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
   186
        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
   187
                                                           vreg=self.vreg)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   188
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   189
    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
   190
        """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
   191
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   192
        May raise AuthenticationError.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        """
9019
e08f9c55dab5 [application] call req.set_session in application.main_handle_request
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9018
diff changeset
   194
        return self.session_handler.get_session(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    # publish methods #########################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   197
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   198
    def log_handle_request(self, req):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        """wrapper around _publish to log all queries executed for a given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
        accessed path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        """
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   202
        def wrap_set_cnx(func):
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
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_execute(cnx):
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   205
                orig_execute = cnx.execute
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   206
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   207
                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
   208
                    tstart, cstart = time(), clock()
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   209
                    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
   210
                    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
   211
                    return rset
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   212
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   213
                return execute
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
            def set_cnx(cnx):
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   216
                func(cnx)
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   217
                cnx.execute = wrap_execute(cnx)
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   218
                cnx.executed_queries = []
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   219
9876
ff98039cb4cd [web] restore query logging functionality (closes #3972561)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9659
diff changeset
   220
            return set_cnx
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
        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
   223
        tstart, cstart = time(), clock()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        try:
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   225
            return self.main_handle_request(req)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
        finally:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
            cnx = req.cnx
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   228
            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
   229
                with self._logfile_lock:
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   230
                    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
   231
                    try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   232
                        result = ['\n' + '*' * 80]
11872
a997e5451737 [web] Enhance query log file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11800
diff changeset
   233
                        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
   234
                            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
   235
                        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
   236
                                   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
   237
                        cnx.executed_queries = []
11163
141e96f93c4d [web/application] fix query log handling on python3
Julien Cristau <julien.cristau@logilab.fr>
parents: 11057
diff changeset
   238
                        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
   239
                        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
   240
                    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
   241
                        self.exception('error while logging queries')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   243
    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
   244
        """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
   245
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   246
        :type req: `web.Request`
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   247
        :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
   248
9175
a7412e884d7b fix typos in docstring, doc and comments
Julien Cristau <julien.cristau@logilab.fr>
parents: 8997
diff changeset
   249
        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
   250
        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
   251
        """
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   252
        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
   253
            # 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
   254
            realm = self.vreg.config['realm']
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   255
            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
   256
        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
   257
        try:
9017
aa709bc6b6c1 [application/connect] simplify connection logic
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8997
diff changeset
   258
            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
   259
                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
   260
                cnx = session.new_cnx()
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   261
                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
   262
                    req.set_cnx(cnx)
b48020a80dc3 Store user groups and properties as session data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11348
diff changeset
   263
                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
   264
            except AuthenticationError:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   265
                # 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
   266
                # 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
   267
                # 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
   268
                # 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
   269
                @contextlib.contextmanager
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   270
                def dummy():
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   271
                    yield
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9066
diff changeset
   272
                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
   273
            # 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
   274
            # handler
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   275
            try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   276
                # 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
   277
                with cnx:
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   278
                    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
   279
            # 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
   280
            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
   281
                # 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
   282
                # 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
   283
                # 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
   284
                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
   285
                    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
   286
                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
   287
                    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
   288
                # 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
   289
                # 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
   290
                # 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
   291
                else:
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   292
                    # 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
   293
                    # 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
   294
                    # 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
   295
                    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
   296
                    # 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
   297
                    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
   298
        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
   299
            # 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
   300
            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
   301
        # 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
   302
        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
   303
            # 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
   304
            # 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
   305
            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
   306
                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
   307
            # 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
   308
            # 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
   309
            # 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
   310
            else:
11913
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11872
diff changeset
   311
                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
   312
            # 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
   313
            # 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
   314
            # 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
   315
            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
   316
                content = self.need_login_content(req)
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12542
diff changeset
   317
        assert isinstance(content, bytes)
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   318
        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
   319
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   320
    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
   321
        """method called by the main publisher to process <req> relative path
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   322
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
        should return a string containing the resulting page or raise a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
        `NotFound` exception
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
        :type req: `web.Request`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
        :param req: the request object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
        :rtype: str
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
        :return: the result of the pusblished url
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
        """
11725
904ee9cd0cf9 [web/application] remove `path` argument from CubicwebPublisher methods
Laura Médioni <laura.medioni@logilab.fr>
parents: 11699
diff changeset
   332
        path = req.relative_path(False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
        # 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
   334
        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
   335
                   req.session.sessionid, list(req.form))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
        # remove user callbacks on a new request (except for json controllers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
        # to avoid callbacks being unregistered before they could be called)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
        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
   339
        commited = False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
        try:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   341
            # standard processing of the request
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
            try:
9571
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   343
                # apply CORS sanity checks
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   344
                cors.process_request(req, self.vreg.config)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
                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
   346
                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
   347
                    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
   348
                                                                 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
   349
                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
   350
                    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
   351
                req.update_search_state()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
                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
   353
            except cors.CORSPreflight:
aaf83cc07eed [web] implement cross origin resource sharing (CORS) (closes #2491768)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 9478
diff changeset
   354
                # 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
   355
                req.status_out = 200
10744
c5dc40988e6e [web] assert that CubicWebPublisher.handle_request returns bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10740
diff changeset
   356
                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
   357
            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
   358
                # 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
   359
                # 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
   360
                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
   361
            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
   362
                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
   363
                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
   364
                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
   365
                    req.data['last_undoable_transaction'] = txuuid
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   366
        # 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
   367
        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
   368
            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
   369
            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
   370
        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
   371
            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
   372
        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
   373
            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
   374
        except Unauthorized as ex:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   375
            req.data['errmsg'] = req._(
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   376
                '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
   377
                '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
   378
            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
   379
            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
   380
        except Forbidden as ex:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   381
            req.data['errmsg'] = req._(
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   382
                'This action is forbidden. '
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   383
                '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
   384
            req.status_out = http_client.FORBIDDEN
8602
d066ba3bb07d [web] add a Forbidden exception
David Douard <david.douard@logilab.fr>
parents: 8470
diff changeset
   385
            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
   386
        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
   387
            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
   388
        # 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
   389
        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
   390
            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
   391
                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
   392
            raise
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   393
        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
   394
            # 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
   395
            raise
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   396
        # 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
   397
        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
   398
            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
   399
            result = self.error_handler(req, ex, tb=True)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        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
   401
            if req.cnx and not commited:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
                    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
   404
                except Exception:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   405
                    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
   406
        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
   407
        self.debug('query %s executed in %s sec', path, clock() - tstart)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
        return result
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
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
   410
    # 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
   411
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
    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
   413
        """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
   414
        - 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
   415
        - 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
   416
        - 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
   417
        """
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
        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
   419
        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
   420
        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
   421
        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
   422
        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
   423
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
    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
   425
        ex.translate(req._)  # translate messages using ui language
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
        if '__errorurl' in req.form:
4224
5998df006968 refactor form error handling:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3408
diff changeset
   427
            forminfo = {'error': ex,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
                        'values': req.form,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
                        'eidmap': req.data.get('eidmap', {})
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
                        }
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
   431
            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
   432
            # 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
   433
            # 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
   434
            # 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
   435
            # 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
   436
            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
   437
            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
   438
            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
   439
            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
   440
        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
   441
        return self.error_handler(req, ex, tb=False)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   442
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
    def error_handler(self, req, ex, tb=False):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   444
        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
   445
        if tb:
92c668170ef9 [web app] only log tb when explicitly asked
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8443
diff changeset
   446
            self.exception(repr(ex))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
        req.set_header('Cache-Control', 'no-cache')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
        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
   449
        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
   450
        req.reset_message()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        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
   452
        if req.ajax_request:
8709
8dc0b8d92694 [web application] Fix missing self. in error_handler
Vincent Michel <vincent.michel@logilab.fr>
parents: 8602
diff changeset
   453
            return self.ajax_error_handler(req, ex)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
            req.data['ex'] = ex
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
            if tb:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
                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
   458
            errview = self.vreg['views'].select('error', req)
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   459
            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
   460
            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
   461
        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
   462
            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
   463
        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
   464
            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
   465
        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
   466
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   467
    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
   468
        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
   469
        if txuuid is not None:
11348
70337ad23145 pep8 + docstrings and comments improvments
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11163
diff changeset
   470
            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
   471
                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
   472
            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
   473
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   474
    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
   475
        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
   476
        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
   477
        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
   478
            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
   479
        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
   480
            # 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
   481
            req.status_out = status
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12542
diff changeset
   482
        json_dumper = getattr(ex, 'dumps', lambda: json.dumps({'reason': str(ex)}))
10777
eabacd3c6f16 [web] CubicWebPublisher.ajax_error_handler must return bytes
Julien Cristau <julien.cristau@logilab.fr>
parents: 10744
diff changeset
   483
        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
   484
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   485
    # special case handling
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   486
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
    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
   488
        return self.vreg['views'].main_template(req, 'login')
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   489
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
    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
   491
        return self.vreg['views'].main_template(req, 'loggedout')
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   492
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
    def notfound_content(self, req):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
        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
   495
        view = self.vreg['views'].select('404', req)
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   496
        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
   497
        return self.vreg['views'].main_template(req, template, view=view)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8311
diff changeset
   499
    # 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
   500
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   501
    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
   502
        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
   503
        if template not in self.vreg['views']:
882
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   504
            template = 'main-template'
75488a2a875e fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents: 871
diff changeset
   505
        return template
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
   506
7083
b8e35cde46e9 help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6791
diff changeset
   507
    # 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
   508
    # 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
   509
    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
   510
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
set_log_methods(CubicWebPublisher, LOGGER)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
set_log_methods(CookieSessionHandler, LOGGER)