web/request.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 23 Jan 2012 12:39:21 +0100
changeset 8179 e52a084e955c
parent 8155 c075950cc929
parent 8177 e4ab883f07d9
child 8190 2a3c1b787688
permissions -rw-r--r--
backport stable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7411
238da9684f99 cleanup and update some copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7374
diff changeset
     1
# copyright 2003-2011 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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
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: 5389
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5423
e15abfdcce38 backport default into stable: stable is now cw 3.8
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5408 5421
diff changeset
    18
"""abstract class for http request"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
import time
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
import random
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
import base64
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7872
diff changeset
    25
from hashlib import sha1 # pylint: disable=E0611
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
    26
from Cookie import SimpleCookie
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
    27
from calendar import timegm
3653
ef71abb1e77b [req] new expires argument to set_cookie
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3293
diff changeset
    28
from datetime import date
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
from urlparse import urlsplit
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
from itertools import count
6078
c08e74b0609e fix missing import in web.request
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6064
diff changeset
    31
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
from rql.utils import rqlvar_maker
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
from logilab.common.decorators import cached
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2559
diff changeset
    36
from logilab.common.deprecation import deprecated
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2258
diff changeset
    37
from logilab.mtconverter import xml_escape
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
    38
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
from cubicweb.dbapi import DBAPIRequest
4023
eae23c40627a drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4004
diff changeset
    40
from cubicweb.mail import header
6063
17a53f6dea42 [request] js is defined in uilib, not utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6062
diff changeset
    41
from cubicweb.uilib import remove_html_tags, js
17a53f6dea42 [request] js is defined in uilib, not utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6062
diff changeset
    42
from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
3094
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
    43
from cubicweb.view import STRICT_DOCTYPE, TRANSITIONAL_DOCTYPE_NOEXT
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
    44
from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
5940
0e3ae19b181a [uilib] refactor json_dumps code organization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5890
diff changeset
    45
                          RequestError, StatusResponse)
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
    46
from cubicweb.web.httpcache import GMTOFFSET
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
    47
from cubicweb.web.http_headers import Headers, Cookie
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
662
6f867ab70e3d move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents: 610
diff changeset
    49
_MARKER = object()
6f867ab70e3d move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents: 610
diff changeset
    50
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
    51
def build_cb_uid(seed):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7872
diff changeset
    52
    sha = sha1('%s%s%s' % (time.time(), seed, random.random()))
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
    53
    return 'cb_%s' % (sha.hexdigest())
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
    54
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
def list_form_param(form, param, pop=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    """get param from form parameters and return its value as a list,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    skipping internal markers if any
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    * if the parameter isn't defined, return an empty list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    * if the parameter is a single (unicode) value, return a list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
      containing that value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
    * if the parameter is already a list or tuple, just skip internal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
      markers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    if pop is True, the parameter is removed from the form dictionnary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
    if pop:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
            value = form.pop(param)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
            return []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        value = form.get(param, ())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    if value is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        value = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    elif not isinstance(value, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        value = [value]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    return [v for v in value if v != INTERNAL_FIELD_VALUE]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
class CubicWebRequestBase(DBAPIRequest):
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
    84
    """abstract HTTP request, should be extended according to the HTTP backend"""
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
    85
    ajax_request = False # to be set to True by ajax controllers
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
    86
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    def __init__(self, vreg, https, form=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
        super(CubicWebRequestBase, self).__init__(vreg)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
        self.https = https
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    90
        if https:
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    91
            self.uiprops = vreg.config.https_uiprops
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    92
            self.datadir_url = vreg.config.https_datadir_url
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    93
        else:
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    94
            self.uiprops = vreg.config.uiprops
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
    95
            self.datadir_url = vreg.config.datadir_url
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        # raw html headers that can be added from any view
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7660
diff changeset
    97
        self.html_headers = HTMLHead(self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        # form parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
        self.setup_params(form)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        # dictionnary that may be used to store request data that has to be
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        # shared among various components used to publish the request (views,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        # controller, application...)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        self.data = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        # search state: 'normal' or 'linksearch' (eg searching for an object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        # to create a relation with another)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   106
        self.search_state = ('normal',)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        # page id, set by htmlheader template
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        self.pageid = None
3816
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   109
        self._set_pageid()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   110
        # prepare output header
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   111
        self.headers_out = Headers()
3816
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   112
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   113
    def _set_pageid(self):
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   114
        """initialize self.pageid
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   115
        if req.form provides a specific pageid, use it, otherwise build a
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   116
        new one.
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   117
        """
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   118
        pid = self.form.get('pageid')
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   119
        if pid is None:
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   120
            pid = make_uid(id(self))
7374
3c9850d929e5 [web request] don't define pageid js variable when one is found in form parameters (eg, page ajax call)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7268
diff changeset
   121
            self.html_headers.define_var('pageid', pid, override=False)
3816
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   122
        self.pageid = pid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
8155
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   124
    def _get_json_request(self):
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
   125
        warn('[3.15] self._cw.json_request is deprecated, use self._cw.ajax_request instead',
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
   126
             DeprecationWarning, stacklevel=2)
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
   127
        return self.ajax_request
8155
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   128
    def _set_json_request(self, value):
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   129
        warn('[3.15] self._cw.json_request is deprecated, use self._cw.ajax_request instead',
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   130
             DeprecationWarning, stacklevel=2)
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   131
        self.ajax_request = value
c075950cc929 [bw compat] json_request setter is necessary for some cubes which has json_request (eg preview)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8128
diff changeset
   132
    json_request = property(_get_json_request, _set_json_request)
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
   133
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   134
    @property
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   135
    def authmode(self):
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   136
        return self.vreg.config['auth-mode']
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   137
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   138
    @property
2801
7ef4c1c9266b fix syntax error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   139
    def varmaker(self):
4366
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   140
        """the rql varmaker is exposed both as a property and as the
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   141
        set_varmaker function since we've two use cases:
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   142
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   143
        * accessing the req.varmaker property to get a new variable name
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   144
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   145
        * calling req.set_varmaker() to ensure a varmaker is set for later ajax
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   146
          calls sharing our .pageid
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   147
        """
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   148
        return self.set_varmaker()
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   149
7266
f625097d1e99 [request] don't init tabindex unnecessarily, since it's added to page data which will makes an ajax call to unload_page_data necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7070
diff changeset
   150
    def next_tabindex(self):
6259
c3d98e4bee12 [request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6108
diff changeset
   151
        nextfunc = self.get_page_data('nexttabfunc')
c3d98e4bee12 [request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6108
diff changeset
   152
        if nextfunc is None:
c3d98e4bee12 [request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6108
diff changeset
   153
            nextfunc = count(1).next
c3d98e4bee12 [request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6108
diff changeset
   154
            self.set_page_data('nexttabfunc', nextfunc)
7266
f625097d1e99 [request] don't init tabindex unnecessarily, since it's added to page data which will makes an ajax call to unload_page_data necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7070
diff changeset
   155
        return nextfunc()
6259
c3d98e4bee12 [request] memoize tabindex across ajax calls (on pageid) to avoid ui confusion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 6108
diff changeset
   156
4366
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   157
    def set_varmaker(self):
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   158
        varmaker = self.get_page_data('rql_varmaker')
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   159
        if varmaker is None:
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   160
            varmaker = rqlvar_maker()
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   161
            self.set_page_data('rql_varmaker', varmaker)
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   162
        return varmaker
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   163
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: 5155
diff changeset
   164
    def set_session(self, session, user=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
        """method called by the session handler when the user is authenticated
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        or an anonymous connection is open
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
        """
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: 5155
diff changeset
   168
        super(CubicWebRequestBase, self).set_session(session, user)
2245
7463e1a748dd new set_session_props method exposed by the repository, use it to be sure session language is in sync the request language
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2202
diff changeset
   169
        # set request language
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: 4851
diff changeset
   170
        vreg = self.vreg
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: 4851
diff changeset
   171
        if self.user:
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: 4851
diff changeset
   172
            try:
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: 4851
diff changeset
   173
                # 1. user specified language
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: 4851
diff changeset
   174
                lang = vreg.typed_value('ui.language',
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: 4851
diff changeset
   175
                                        self.user.properties['ui.language'])
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: 4851
diff changeset
   176
                self.set_language(lang)
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: 4851
diff changeset
   177
                return
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: 4851
diff changeset
   178
            except KeyError:
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: 4851
diff changeset
   179
                pass
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: 4851
diff changeset
   180
        if vreg.config['language-negociation']:
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: 4851
diff changeset
   181
            # 2. http negociated language
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: 4851
diff changeset
   182
            for lang in self.header_accept_language():
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: 4851
diff changeset
   183
                if lang in self.translations:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
                    self.set_language(lang)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
                    return
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: 4851
diff changeset
   186
        # 3. default language
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: 4851
diff changeset
   187
        self.set_default_language(vreg)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   188
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   189
    def set_language(self, lang):
3275
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3094
diff changeset
   190
        gettext, self.pgettext = self.translations[lang]
5247789df541 [gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3094
diff changeset
   191
        self._ = self.__ = gettext
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
        self.lang = lang
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        self.debug('request language: %s', lang)
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   194
        if self.cnx:
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: 5155
diff changeset
   195
            self.cnx.set_session_props(lang=lang)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   196
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
    # input form parameters management ########################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   198
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
    # common form parameters which should be protected against html values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    # XXX can't add 'eid' for instance since it may be multivalued
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
    # dont put rql as well, if query contains < and > it will be corrupted!
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   202
    no_script_form_params = set(('vid',
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   203
                                 'etype',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
                                 'vtitle', 'title',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
                                 '__message',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
                                 '__redirectvid', '__redirectrql'))
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   207
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
    def setup_params(self, params):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        """WARNING: we're intentionaly leaving INTERNAL_FIELD_VALUE here
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   211
        subclasses should overrides to
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        """
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: 4851
diff changeset
   213
        self.form = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
        if params is None:
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: 4851
diff changeset
   215
            return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
        encoding = self.encoding
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: 4851
diff changeset
   217
        for param, val in params.iteritems():
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: 4851
diff changeset
   218
            if isinstance(val, (tuple, list)):
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: 4851
diff changeset
   219
                val = [unicode(x, encoding) for x in val]
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: 4851
diff changeset
   220
                if len(val) == 1:
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: 4851
diff changeset
   221
                    val = val[0]
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: 4851
diff changeset
   222
            elif isinstance(val, str):
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: 4851
diff changeset
   223
                val = unicode(val, encoding)
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: 4851
diff changeset
   224
            if param in self.no_script_form_params and val:
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: 4851
diff changeset
   225
                val = self.no_script_form_param(param, val)
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: 4851
diff changeset
   226
            if param == '_cwmsgid':
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: 4851
diff changeset
   227
                self.set_message_id(val)
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: 4851
diff changeset
   228
            elif param == '__message':
7432
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   229
                warn('[3.13] __message in request parameter is deprecated (may '
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   230
                     'only be given to .build_url). Seeing this message usualy '
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   231
                     'means your application hold some <form> where you should '
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   232
                     'replace use of __message hidden input by form.set_message, '
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   233
                     'so new _cwmsgid mechanism is properly used',
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   234
                     DeprecationWarning)
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: 4851
diff changeset
   235
                self.set_message(val)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
            else:
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: 4851
diff changeset
   237
                self.form[param] = val
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   238
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: 4851
diff changeset
   239
    def no_script_form_param(self, param, value):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        """ensure there is no script in a user form param
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        by default return a cleaned string instead of raising a security
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
        exception
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
        this method should be called on every user input (form at least) fields
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
        that are at some point inserted in a generated html page to protect
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
        against script kiddies
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
        """
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: 4851
diff changeset
   249
        # safety belt for strange urls like http://...?vtitle=yo&vtitle=yo
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: 4851
diff changeset
   250
        if isinstance(value, (list, tuple)):
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: 4851
diff changeset
   251
            self.error('no_script_form_param got a list (%s). Who generated the URL ?',
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: 4851
diff changeset
   252
                       repr(value))
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: 4851
diff changeset
   253
            value = value[0]
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: 4851
diff changeset
   254
        return remove_html_tags(value)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   255
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    def list_form_param(self, param, form=None, pop=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
        """get param from form parameters and return its value as a list,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
        skipping internal markers if any
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   259
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
        * if the parameter isn't defined, return an empty list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
        * if the parameter is a single (unicode) value, return a list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
          containing that value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        * if the parameter is already a list or tuple, just skip internal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
          markers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
        if pop is True, the parameter is removed from the form dictionnary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
        if form is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
            form = self.form
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   270
        return list_form_param(form, param, pop)
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   271
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   273
    def reset_headers(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   274
        """used by AutomaticWebTest to clear html headers between tests on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
        the same resultset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
        """
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7660
diff changeset
   277
        self.html_headers = HTMLHead(self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
        return self
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
    # web state helpers #######################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   281
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: 4851
diff changeset
   282
    @property
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: 4851
diff changeset
   283
    def message(self):
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: 4851
diff changeset
   284
        try:
7432
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   285
            return self.session.data.pop(self._msgid, u'')
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: 4851
diff changeset
   286
        except AttributeError:
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: 4851
diff changeset
   287
            try:
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: 4851
diff changeset
   288
                return self._msg
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: 4851
diff changeset
   289
            except AttributeError:
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: 4851
diff changeset
   290
                return None
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: 4851
diff changeset
   291
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
    def set_message(self, msg):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
        assert isinstance(msg, unicode)
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: 4851
diff changeset
   294
        self._msg = msg
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: 4851
diff changeset
   295
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: 4851
diff changeset
   296
    def set_message_id(self, msgid):
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: 4851
diff changeset
   297
        self._msgid = msgid
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: 4851
diff changeset
   298
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: 4851
diff changeset
   299
    @cached
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: 4851
diff changeset
   300
    def redirect_message_id(self):
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: 4851
diff changeset
   301
        return make_uid()
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: 4851
diff changeset
   302
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: 4851
diff changeset
   303
    def set_redirect_message(self, msg):
7432
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   304
        # TODO - this should probably be merged with append_to_redirect_message
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: 4851
diff changeset
   305
        assert isinstance(msg, unicode)
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: 4851
diff changeset
   306
        msgid = self.redirect_message_id()
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: 5155
diff changeset
   307
        self.session.data[msgid] = msg
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: 4851
diff changeset
   308
        return msgid
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: 4851
diff changeset
   309
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: 4851
diff changeset
   310
    def append_to_redirect_message(self, msg):
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: 4851
diff changeset
   311
        msgid = self.redirect_message_id()
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: 5155
diff changeset
   312
        currentmsg = self.session.data.get(msgid)
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: 4851
diff changeset
   313
        if currentmsg is not None:
7432
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   314
            currentmsg = u'%s %s' % (currentmsg, msg)
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: 4851
diff changeset
   315
        else:
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: 4851
diff changeset
   316
            currentmsg = msg
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: 5155
diff changeset
   317
        self.session.data[msgid] = currentmsg
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: 4851
diff changeset
   318
        return msgid
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: 4851
diff changeset
   319
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: 4851
diff changeset
   320
    def reset_message(self):
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: 4851
diff changeset
   321
        if hasattr(self, '_msg'):
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: 4851
diff changeset
   322
            del self._msg
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: 4851
diff changeset
   323
        if hasattr(self, '_msgid'):
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: 4851
diff changeset
   324
            del self._msgid
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   325
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
    def update_search_state(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
        """update the current search state"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
        searchstate = self.form.get('__mode')
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   329
        if not searchstate and self.cnx:
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: 5155
diff changeset
   330
            searchstate = self.session.data.get('search_state', 'normal')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
        self.set_search_state(searchstate)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
    def set_search_state(self, searchstate):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
        """set a new search state"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
        if searchstate is None or searchstate == 'normal':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
            self.search_state = (searchstate or 'normal',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
            self.search_state = ('linksearch', searchstate.split(':'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
            assert len(self.search_state[-1]) == 4
5244
5467674ad101 [web] put a fake object that raise Unauthorized on any attribute access as req.cnx and req._user, so we are properly asked to authenticated on any view that tries to do something with one of those attributes (instead of doing defensive programming everywhere we're doing that)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5223
diff changeset
   340
        if self.cnx:
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: 5155
diff changeset
   341
            self.session.data['search_state'] = searchstate
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
1173
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   343
    def match_search_state(self, rset):
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   344
        """when searching an entity to create a relation, return True if entities in
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   345
        the given rset may be used as relation end
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   346
        """
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   347
        try:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   348
            searchedtype = self.search_state[1][-1]
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   349
        except IndexError:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   350
            return False # no searching for association
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   351
        for etype in rset.column_types(0):
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   352
            if etype != searchedtype:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   353
                return False
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   354
        return True
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   355
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
    def update_breadcrumbs(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
        """stores the last visisted page in session data"""
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: 5155
diff changeset
   358
        searchstate = self.session.data.get('search_state')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
        if searchstate == 'normal':
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: 5155
diff changeset
   360
            breadcrumbs = self.session.data.get('breadcrumbs')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
            if breadcrumbs is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
                breadcrumbs = SizeConstrainedList(10)
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: 5155
diff changeset
   363
                self.session.data['breadcrumbs'] = breadcrumbs
4864
e77d3e95872d [web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   364
                breadcrumbs.append(self.url())
e77d3e95872d [web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   365
            else:
e77d3e95872d [web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   366
                url = self.url()
4974
025a491bad0c take care to empty breadcrumbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4868
diff changeset
   367
                if breadcrumbs and breadcrumbs[-1] != url:
4864
e77d3e95872d [web req] check latest url in breadcrumbs is not the same as the current one to keep more valuable information
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   368
                    breadcrumbs.append(url)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
    def last_visited_page(self):
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: 5155
diff changeset
   371
        breadcrumbs = self.session.data.get('breadcrumbs')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
        if breadcrumbs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
            return breadcrumbs.pop()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
        return self.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   376
    def user_rql_callback(self, rqlargs, *args, **kwargs):
7793
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   377
        """register a user callback to execute some rql query, and return a URL
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   378
        to call that callback which can be inserted in an HTML view.
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   379
7793
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   380
        `rqlargs` should be a tuple containing argument to give to the execute function.
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   381
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   382
        The first argument following rqlargs must be the message to be
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   383
        displayed after the callback is called.
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   384
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   385
        For other allowed arguments, see :meth:`user_callback` method
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   386
        """
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   387
        def rqlexec(req, rql, args=None, key=None):
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   388
            req.execute(rql, args, key)
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   389
        return self.user_callback(rqlexec, rqlargs, *args, **kwargs)
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   390
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   391
    def user_callback(self, cb, cbargs, *args, **kwargs):
7793
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   392
        """register the given user callback and return a URL which can
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   393
        be inserted in an HTML view. When the URL is accessed, the
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   394
        callback function will be called (as 'cb(req, \*cbargs)', and a
7793
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   395
        message will be displayed in the web interface. The third
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   396
        positional argument must be 'msg', containing the message.
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   397
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   398
        You can specify the underlying js function to call using a 'jsfunc'
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   399
        named args, to one of :func:`userCallback`,
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   400
        ':func:`userCallbackThenUpdateUI`, ':func:`userCallbackThenReloadPage`
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   401
        (the default). Take care arguments may vary according to the used
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   402
        function.
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   403
        """
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   404
        self.add_js('cubicweb.ajax.js')
6062
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   405
        jsfunc = kwargs.pop('jsfunc', 'userCallbackThenReloadPage')
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   406
        if 'msg' in kwargs:
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   407
            warn('[3.10] msg should be given as positional argument',
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   408
                 DeprecationWarning, stacklevel=2)
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   409
            args = (kwargs.pop('msg'),) + args
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   410
        assert not kwargs, 'dunno what to do with remaining kwargs: %s' % kwargs
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   411
        cbname = self.register_onetime_callback(cb, *cbargs)
f1a550102f5c [request] enhanced user[_rql]_callback method, allowing to call other js variant, eg one of userCallback, userCallbackThenUpdateUI, userCallbackThenReloadPage (the default). Also benefit from the new magic js object.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5940
diff changeset
   412
        return "javascript: %s" % getattr(js, jsfunc)(cbname, *args)
2792
135580d15d42 rename and move cw.RequestSessionMixIn to cw.req.RequestSessionBase; move some appobjects methods where they actually belong to
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2788
diff changeset
   413
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
    def register_onetime_callback(self, func, *args):
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   415
        cbname = build_cb_uid(func.__name__)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
        def _cb(req):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
            try:
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7894
diff changeset
   418
                return func(req, *args)
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7894
diff changeset
   419
            finally:
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7894
diff changeset
   420
                self.unregister_callback(self.pageid, cbname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
        self.set_page_data(cbname, _cb)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
        return cbname
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   423
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
    def unregister_callback(self, pageid, cbname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
        assert pageid is not None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
        assert cbname.startswith('cb_')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
        self.info('unregistering callback %s for pageid %s', cbname, pageid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
        self.del_page_data(cbname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
    def clear_user_callbacks(self):
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: 5155
diff changeset
   431
        if self.session is not None: # XXX
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: 5155
diff changeset
   432
            for key in self.session.data.keys():
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: 5155
diff changeset
   433
                if key.startswith('cb_'):
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: 5155
diff changeset
   434
                    del self.session.data[key]
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   435
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
    # web edition helpers #####################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   437
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
    @cached # so it's writed only once
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   439
    def fckeditor_config(self):
7277
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 7273
diff changeset
   440
        fckeditor_url = self.build_url('fckeditor/fckeditor.js')
acd7f0e9f276 [etwist] Generates apache's mod_concat-like urls for js and css
Quentin Roquefort <roquefort.quentin@gmail.com>
parents: 7273
diff changeset
   441
        self.add_js(fckeditor_url, localfile=False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
        self.html_headers.define_var('fcklang', self.lang)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
        self.html_headers.define_var('fckconfigpath',
7070
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   444
                                     self.data_url('cubicweb.fckcwconfig.js'))
1013
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 940
diff changeset
   445
    def use_fckeditor(self):
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 940
diff changeset
   446
        return self.vreg.config.fckeditor_installed() and self.property_value('ui.fckeditor')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
    def edited_eids(self, withtype=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   449
        """return a list of edited eids"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
        yielded = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        # warning: use .keys since the caller may change `form`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
        form = self.form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
            eids = form['eid']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
        except KeyError:
4155
80cc9c6ddcf0 NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4154
diff changeset
   456
            raise NothingToEdit(self._('no selected entities'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   457
        if isinstance(eids, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   458
            eids = (eids,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   459
        for peid in eids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   460
            if withtype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   461
                typekey = '__type:%s' % peid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
                assert typekey in form, 'no entity type specified'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
                yield peid, form[typekey]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
                yield peid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   466
            yielded = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   467
        if not yielded:
4155
80cc9c6ddcf0 NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4154
diff changeset
   468
            raise NothingToEdit(self._('no selected entities'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
    # minparams=3 by default: at least eid, __type, and some params to change
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   471
    def extract_entity_params(self, eid, minparams=3):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   472
        """extract form parameters relative to the given eid"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
        params = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
        eid = str(eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
        form = self.form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
        for param in form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
                name, peid = param.split(':', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
            except ValueError:
7639
5c9e94a09e97 [web form] avoid mis-formated warning for _cw_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   480
                if not param.startswith('__') and param not in ('eid', '_cw_fields'):
6079
bd7e3042791e fix the fix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6078
diff changeset
   481
                    self.warning('param %s mis-formatted', param)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   482
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
            if peid == eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
                value = form[param]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
                if value == INTERNAL_FIELD_VALUE:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
                    value = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
                params[name] = value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
        params['eid'] = eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
        if len(params) < minparams:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
            raise RequestError(self._('missing parameters for entity %s') % eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
        return params
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   492
4277
35cd057339b2 turn all the stuff used to handle 'generic relations' in forms into proper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   493
    # XXX this should go to the GenericRelationsField. missing edition cancel protocol.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   494
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
    def remove_pending_operations(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
        """shortcut to clear req's pending_{delete,insert} entries
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   497
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
        This is needed when the edition is completed (whether it's validated
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
        or cancelled)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
        """
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: 5155
diff changeset
   501
        self.session.data.pop('pending_insert', None)
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: 5155
diff changeset
   502
        self.session.data.pop('pending_delete', None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
    def cancel_edition(self, errorurl):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
        """remove pending operations and `errorurl`'s specific stored data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   506
        """
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: 5155
diff changeset
   507
        self.session.data.pop(errorurl, None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
        self.remove_pending_operations()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   509
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
    # high level methods for HTTP headers management ##########################
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
    # must be cached since login/password are popped from the form dictionary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
    # and this method may be called multiple times during authentication
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
    def get_authorization(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
        """Parse and return the Authorization header"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
        if self.authmode == "cookie":
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   518
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   519
                user = self.form.pop("__login")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
                passwd = self.form.pop("__password", '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   521
                return user, passwd.encode('UTF8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
                self.debug('no login/password in form params')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   524
                return None, None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   525
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   526
            return self.header_authorization()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   527
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   528
    def get_cookie(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   529
        """retrieve request cookies, returns an empty cookie if not found"""
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   530
        # XXX use http_headers implementation
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
        try:
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   532
            return SimpleCookie(self.get_header('Cookie'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   533
        except KeyError:
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   534
            return SimpleCookie()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   535
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   536
    def set_cookie(self, name, value, maxage=300, expires=None, secure=False):
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   537
        """set / update a cookie
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   538
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
        by default, cookie will be available for the next 5 minutes.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
        Give maxage = None to have a "session" cookie expiring when the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   541
        client close its browser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   542
        """
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   543
        if isinstance(name, SimpleCookie):
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   544
            warn('[3.13] set_cookie now takes name and value as two first '
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   545
                 'argument, not anymore cookie object and name',
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   546
                 DeprecationWarning, stacklevel=2)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   547
            secure = name[value]['secure']
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   548
            name, value = value, name[value].value
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   549
        if maxage: # don't check is None, 0 may be specified
7858
f91255a3b2c2 [web request] fix stupid error that gone through the review
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7855
diff changeset
   550
            assert expires is None, 'both max age and expires cant be specified'
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   551
            expires = maxage + time.time()
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   552
        elif expires:
8176
eff4fe02ec64 [req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8081
diff changeset
   553
            # we don't want to handle times before the EPOCH (cause bug on
eff4fe02ec64 [req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8081
diff changeset
   554
            # windows). Also use > and not >= else expires == 0 and Cookie think
eff4fe02ec64 [req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8081
diff changeset
   555
            # that means no expire...
eff4fe02ec64 [req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8081
diff changeset
   556
            assert expires + GMTOFFSET > date(1970, 1, 1)
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   557
            expires = timegm((expires + GMTOFFSET).timetuple())
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   558
        else:
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   559
            expires = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   560
        # make sure cookie is set on the correct path
7870
1a1e04163cde [test] unicode name/value in cookie makes tests failing (SimpleCookie then suppose it's a dict...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7858
diff changeset
   561
        cookie = Cookie(str(name), str(value), self.base_url_path(),
1a1e04163cde [test] unicode name/value in cookie makes tests failing (SimpleCookie then suppose it's a dict...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7858
diff changeset
   562
                        expires=expires, secure=secure)
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   563
        self.headers_out.addHeader('Set-cookie', cookie)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   564
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   565
    def remove_cookie(self, name, bwcompat=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
        """remove a cookie by expiring it"""
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   567
        if bwcompat is not None:
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   568
            warn('[3.13] remove_cookie now take only a name as argument',
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   569
                 DeprecationWarning, stacklevel=2)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   570
            name = bwcompat
8176
eff4fe02ec64 [req cookie] fix remove_cookie expires which was leading to expires computed to 0 in set_cookie and the Cookie class interpret that has no expires. Closes #2154654
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8081
diff changeset
   571
        self.set_cookie(name, '', maxage=0, expires=date(2000, 1, 1))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   572
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   573
    def set_content_type(self, content_type, filename=None, encoding=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   574
        """set output content type for this request. An optional filename
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   575
        may be given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   576
        """
6353
b622602f8e9d don't add another encoding if already present in content-type
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6259
diff changeset
   577
        if content_type.startswith('text/') and ';charset=' not in content_type:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   578
            content_type += ';charset=' + (encoding or self.encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   579
        self.set_header('content-type', content_type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   580
        if filename:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   581
            if isinstance(filename, unicode):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   582
                filename = header(filename).encode()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   583
            self.set_header('content-disposition', 'inline; filename=%s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   584
                            % filename)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   585
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
    # high level methods for HTML headers management ##########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   587
2258
79bc598c6411 when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2255
diff changeset
   588
    def add_onload(self, jscode):
4851
e55bdd10421e remove deprecation warning introduced by add_onload api change
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   589
        self.html_headers.add_onload(jscode)
2258
79bc598c6411 when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2255
diff changeset
   590
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   591
    def add_js(self, jsfiles, localfile=True):
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   592
        """specify a list of JS files to include in the HTML headers.
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   593
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   594
        :param jsfiles: a JS filename or a list of JS filenames
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   595
        :param localfile: if True, the default data dir prefix is added to the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   596
                          JS filename
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   597
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
        if isinstance(jsfiles, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   599
            jsfiles = (jsfiles,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   600
        for jsfile in jsfiles:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
            if localfile:
7070
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   602
                jsfile = self.data_url(jsfile)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   603
            self.html_headers.add_js(jsfile)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   604
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   605
    def add_css(self, cssfiles, media=u'all', localfile=True, ieonly=False,
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   606
                iespec=u'[if lt IE 8]'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   607
        """specify a CSS file to include in the HTML headers
5290
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4984
diff changeset
   608
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   609
        :param cssfiles: a CSS filename or a list of CSS filenames.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   610
        :param media: the CSS's media if necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
        :param localfile: if True, the default data dir prefix is added to the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   612
                          CSS filename
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   613
        :param ieonly: True if this css is specific to IE
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   614
        :param iespec: conditional expression that will be used around
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   615
                       the css inclusion. cf:
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   616
                       http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   617
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   618
        if isinstance(cssfiles, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   619
            cssfiles = (cssfiles,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   620
        if ieonly:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   621
            if self.ie_browser():
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   622
                extraargs = [iespec]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   623
                add_css = self.html_headers.add_ie_css
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   624
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   625
                return # no need to do anything on non IE browsers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   626
        else:
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   627
            extraargs = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   628
            add_css = self.html_headers.add_css
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   629
        for cssfile in cssfiles:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   630
            if localfile:
7070
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   631
                cssfile = self.data_url(cssfile)
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   632
            add_css(cssfile, media, *extraargs)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   633
5713
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   634
    @deprecated('[3.9] use ajax_replace_url() instead, naming rql and vid arguments')
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   635
    def build_ajax_replace_url(self, nodeid, rql, vid, replacemode='replace',
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   636
                               **extraparams):
5713
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   637
        return self.ajax_replace_url(nodeid, replacemode, rql=rql, vid=vid,
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   638
                                     **extraparams)
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   639
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   640
    def ajax_replace_url(self, nodeid, replacemode='replace', **extraparams):
5290
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4984
diff changeset
   641
        """builds an ajax url that will replace nodeid's content
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4984
diff changeset
   642
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   643
        :param nodeid: the dom id of the node to replace
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   644
        :param replacemode: defines how the replacement should be done.
5290
15846058378b [doc/book] fix a bit the request chapter, add autodoc
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4984
diff changeset
   645
5713
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   646
          Possible values are :
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   647
          - 'replace' to replace the node's content with the generated HTML
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   648
          - 'swap' to replace the node itself with the generated HTML
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   649
          - 'append' to append the generated HTML to the node's content
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   650
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   651
        Arbitrary extra named arguments may be given, they will be included as
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   652
        parameters of the generated url.
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   653
        """
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   654
        # define a function in headers and use it in the link to avoid url
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   655
        # unescaping pb: browsers give the js expression to the interpreter
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   656
        # after having url unescaping the content. This may make appear some
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   657
        # quote or other special characters that will break the js expression.
5713
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   658
        extraparams.setdefault('fname', 'view')
605f571198eb [web] ajax_replace_url superseed build_ajax_replace_url, more generic and relying on the json controller (closes #750090)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5685
diff changeset
   659
        url = self.build_url('json', **extraparams)
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   660
        cbname = build_cb_uid(url[:50])
7515
e1ba23fdcf2d closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7411
diff changeset
   661
        # think to propagate pageid. XXX see https://www.cubicweb.org/ticket/1753121
7660
a1506b5306cc closes #1826543: error on some ajax_replace_url call
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7639
diff changeset
   662
        jscode = u'function %s() { $("#%s").%s; }' % (
7515
e1ba23fdcf2d closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7411
diff changeset
   663
            cbname, nodeid, js.loadxhtml(url, {'pageid': self.pageid},
e1ba23fdcf2d closes #1484510: ajax_replace_url should propagate page id (until an underlying api does it automatically
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7411
diff changeset
   664
                                         'get', replacemode))
6564
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   665
        self.html_headers.add_post_inline_script(jscode)
ff9f7c566464 [request] fix ajax_replace_url which breaks if the url contains some quotes (that will be properly quoted but unquoted by the browser, breaking the js expression) by using a separated js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6543
diff changeset
   666
        return "javascript: %s()" % cbname
1801
672acc730ce5 ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1718
diff changeset
   667
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   668
    # urls/path management ####################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   669
7432
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   670
    def build_url(self, *args, **kwargs):
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   671
        """return an absolute URL using params dictionary key/values as URL
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   672
        parameters. Values are automatically URL quoted, and the
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   673
        publishing method to use may be specified or will be guessed.
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   674
        """
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   675
        if '__message' in kwargs:
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   676
            msg = kwargs.pop('__message')
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   677
            kwargs['_cwmsgid'] = self.set_redirect_message(msg)
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   678
        return super(CubicWebRequestBase, self).build_url(*args, **kwargs)
cab99ccdb774 [ui messages, xss] Start migration towards use of _msgid instead of __message (prone to XSS injection) closes #1698245
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 7412
diff changeset
   679
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   680
    def url(self, includeparams=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   681
        """return currently accessed url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   682
        return self.base_url() + self.relative_path(includeparams)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   683
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   684
    def selected(self, url):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   685
        """return True if the url is equivalent to currently accessed url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   686
        reqpath = self.relative_path().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   687
        baselen = len(self.base_url())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   688
        return (reqpath == url[baselen:].lower())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   689
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   690
    def base_url_prepend_host(self, hostname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   691
        protocol, roothost = urlsplit(self.base_url())[:2]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   692
        if roothost.startswith('www.'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   693
            roothost = roothost[4:]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   694
        return '%s://%s.%s' % (protocol, hostname, roothost)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   695
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   696
    def base_url_path(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   697
        """returns the absolute path of the base url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   698
        return urlsplit(self.base_url())[2]
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   699
7070
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   700
    def data_url(self, relpath):
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   701
        """returns the absolute path for a data resouce"""
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   702
        return self.datadir_url + relpath
5f8e52d722c5 [web] provide a data_url() method on req and get_rid of explicit datadir_url usage (#1438736)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6581
diff changeset
   703
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   704
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   705
    def from_controller(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   706
        """return the id (string) of the controller issuing the request"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   707
        controller = self.relative_path(False).split('/', 1)[0]
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
   708
        registered_controllers = self.vreg['controllers'].keys()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
        if controller in registered_controllers:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   710
            return controller
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   711
        return 'view'
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   712
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   713
    def validate_cache(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   714
        """raise a `DirectResponse` exception if a cached page along the way
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   715
        exists and is still usable.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   716
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   717
        calls the client-dependant implementation of `_validate_cache`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   718
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   719
        self._validate_cache()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   720
        if self.http_method() == 'HEAD':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   721
            raise StatusResponse(200, '')
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   722
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   723
    # abstract methods to override according to the web front-end #############
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   724
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   725
    def http_method(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   726
        """returns 'POST', 'GET', 'HEAD', etc."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
        raise NotImplementedError()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   728
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   729
    def _validate_cache(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   730
        """raise a `DirectResponse` exception if a cached page along the way
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   731
        exists and is still usable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   732
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
        raise NotImplementedError()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   734
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
    def relative_path(self, includeparams=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
        """return the normalized path of the request (ie at least relative
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2315
diff changeset
   737
        to the instance's root, but some other normalization may be needed
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
        so that the returned path may be used to compare to generated urls
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   740
        :param includeparams:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
           boolean indicating if GET form parameters should be kept in the path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   742
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   743
        raise NotImplementedError()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
    def get_header(self, header, default=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   746
        """return the value associated with the given input HTTP header,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   747
        raise KeyError if the header is not set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   748
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   749
        raise NotImplementedError()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   750
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   751
    def set_header(self, header, value, raw=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
        """set an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   753
        if raw:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   754
            # adding encoded header is important, else page content
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   755
            # will be reconverted back to unicode and apart unefficiency, this
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   756
            # may cause decoding problem (e.g. when downloading a file)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   757
            self.headers_out.setRawHeaders(header, [str(value)])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   758
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   759
            self.headers_out.setHeader(header, value)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   760
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   761
    def add_header(self, header, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
        """add an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   763
        # adding encoded header is important, else page content
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   764
        # will be reconverted back to unicode and apart unefficiency, this
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   765
        # may cause decoding problem (e.g. when downloading a file)
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   766
        self.headers_out.addRawHeader(header, str(value))
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   767
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   768
    def remove_header(self, header):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   769
        """remove an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   770
        self.headers_out.removeHeader(header)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   771
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   772
    def header_authorization(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   773
        """returns a couple (auth-type, auth-value)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   774
        auth = self.get_header("Authorization", None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   775
        if auth:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   776
            scheme, rest = auth.split(' ', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   777
            scheme = scheme.lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   778
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   779
                assert scheme == "basic"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   780
                user, passwd = base64.decodestring(rest).split(":", 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   781
                # XXX HTTP header encoding: use email.Header?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   782
                return user.decode('UTF8'), passwd
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   783
            except Exception, ex:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   784
                self.debug('bad authorization %s (%s: %s)',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   785
                           auth, ex.__class__.__name__, ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   786
        return None, None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   787
1716
b12d9e22bac3 basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
   788
    def parse_accept_header(self, header):
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   789
        """returns an ordered list of accepted values"""
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   790
        try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   791
            value_parser, value_sort_key = ACCEPT_HEADER_PARSER[header.lower()]
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   792
        except KeyError:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   793
            value_parser = value_sort_key = None
1716
b12d9e22bac3 basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
   794
        accepteds = self.get_header(header, '')
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   795
        values = _parse_accept_header(accepteds, value_parser, value_sort_key)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   796
        return (raw_value for (raw_value, parsed_value, score) in values)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   797
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   798
    def header_if_modified_since(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   799
        """If the HTTP header If-modified-since is set, return the equivalent
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   800
        mx date time value (GMT), else return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   801
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   802
        raise NotImplementedError()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   803
3094
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   804
    def demote_to_html(self):
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   805
        """helper method to dynamically set request content type to text/html
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   806
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   807
        The global doctype and xmldec must also be changed otherwise the browser
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   808
        will display '<[' at the beginning of the page
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   809
        """
8063
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   810
        if not self.vreg.config['force-html-content-type']:
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   811
            if not hasattr(self, 'main_stream'):
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   812
                raise Exception("Can't demote to html from an ajax context. You "
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   813
                                "should change force-html-content-type to yes "
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   814
                                "in the instance configuration file.")
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   815
            self.set_content_type('text/html')
1b2a05c9b71c [html, ajax] demote_to_html don't crash anymore if force-html-content-type is activated. Closes #712309
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   816
            self.main_stream.set_doctype(TRANSITIONAL_DOCTYPE_NOEXT)
7187
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   817
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   818
    def set_doctype(self, doctype, reset_xmldecl=True):
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   819
        """helper method to dynamically change page doctype
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   820
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   821
        :param doctype: the new doctype, e.g. '<!DOCTYPE html>'
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   822
        :param reset_xmldecl: if True, remove the '<?xml version="1.0"?>'
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   823
                              declaration from the page
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   824
        """
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   825
        self.main_stream.set_doctype(doctype, reset_xmldecl)
3094
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   826
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   827
    # page data management ####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   828
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   829
    def get_page_data(self, key, default=None):
5683
ca2badf54858 [request] fix typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5505
diff changeset
   830
        """return value associated to `key` in current page data"""
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: 5155
diff changeset
   831
        page_data = self.session.data.get(self.pageid)
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: 5155
diff changeset
   832
        if page_data is None:
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: 5155
diff changeset
   833
            return default
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   834
        return page_data.get(key, default)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   835
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   836
    def set_page_data(self, key, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   837
        """set value associated to `key` in current page data"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   838
        self.html_headers.add_unload_pagedata()
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: 5155
diff changeset
   839
        page_data = self.session.data.setdefault(self.pageid, {})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   840
        page_data[key] = value
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: 5155
diff changeset
   841
        self.session.data[self.pageid] = page_data
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   842
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   843
    def del_page_data(self, key=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   844
        """remove value associated to `key` in current page data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   845
        if `key` is None, all page data will be cleared
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   846
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   847
        if key is None:
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: 5155
diff changeset
   848
            self.session.data.pop(self.pageid, None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   849
        else:
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: 5155
diff changeset
   850
            try:
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: 5155
diff changeset
   851
                del self.session.data[self.pageid][key]
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: 5155
diff changeset
   852
            except KeyError:
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: 5155
diff changeset
   853
                pass
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   854
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   855
    # user-agent detection ####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   856
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   857
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   858
    def useragent(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   859
        return self.get_header('User-Agent', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   860
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   861
    def ie_browser(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   862
        useragent = self.useragent()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   863
        return useragent and 'MSIE' in useragent
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   864
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   865
    def xhtml_browser(self):
2558
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   866
        """return True if the browser is considered as xhtml compatible.
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   867
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   868
        If the instance is configured to always return text/html and not
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   869
        application/xhtml+xml, this method will always return False, even though
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   870
        this is semantically different
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   871
        """
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   872
        if self.vreg.config['force-html-content-type']:
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   873
            return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   874
        useragent = self.useragent()
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   875
        # * MSIE/Konqueror does not support xml content-type
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   876
        # * Opera supports xhtml and handles namespaces properly but it breaks
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   877
        #   jQuery.attr()
495
f8b1edfe9621 [#80966] Opera supports xhtml and handles namespaces properly but it breaks jQuery.attr(), so xhtml_browser return False if the webbrowser is opera
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 0
diff changeset
   878
        if useragent and ('MSIE' in useragent or 'KHTML' in useragent
f8b1edfe9621 [#80966] Opera supports xhtml and handles namespaces properly but it breaks jQuery.attr(), so xhtml_browser return False if the webbrowser is opera
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 0
diff changeset
   879
                          or 'Opera' in useragent):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   880
            return False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   881
        return True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   882
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   883
    def html_content_type(self):
2558
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   884
        if self.xhtml_browser():
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   885
            return 'application/xhtml+xml'
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   886
        return 'text/html'
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   887
2559
46859078c866 [R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2558
diff changeset
   888
    def document_surrounding_div(self):
46859078c866 [R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2558
diff changeset
   889
        if self.xhtml_browser():
4454
aba1b563705b [request] add a note about the encoding mgmt (or lack thereof)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 4212
diff changeset
   890
            return (u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE + # XXX encoding ?
2559
46859078c866 [R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2558
diff changeset
   891
                    u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">')
46859078c866 [R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2558
diff changeset
   892
        return u'<div>'
46859078c866 [R xhtml] remove xhtml_wrap* function, use instead a single req.document_surrounding_div method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2558
diff changeset
   893
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
   894
    @deprecated('[3.9] use req.uiprops[rid]')
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   895
    def external_resource(self, rid, default=_MARKER):
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   896
        """return a path to an external resource, using its identifier
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   897
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   898
        raise `KeyError` if the resource is not defined
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   899
        """
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   900
        try:
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5444
diff changeset
   901
            return self.uiprops[rid]
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   902
        except KeyError:
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   903
            if default is _MARKER:
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   904
                raise
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   905
            return default
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   906
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
   907
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   908
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   909
## HTTP-accept parsers / utilies ##############################################
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   910
def _mimetype_sort_key(accept_info):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   911
    """accepted mimetypes must be sorted by :
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   912
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   913
    1/ highest score first
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   914
    2/ most specific mimetype first, e.g. :
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   915
       - 'text/html level=1' is more specific 'text/html'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   916
       - 'text/html' is more specific than 'text/*'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   917
       - 'text/*' itself more specific than '*/*'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   918
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   919
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   920
    raw_value, (media_type, media_subtype, media_type_params), score = accept_info
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   921
    # FIXME: handle '+' in media_subtype ? (should xhtml+xml have a
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   922
    # higher precedence than xml ?)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   923
    if media_subtype == '*':
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   924
        score -= 0.0001
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   925
    if media_type == '*':
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   926
        score -= 0.0001
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   927
    return 1./score, media_type, media_subtype, 1./(1+len(media_type_params))
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   928
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   929
def _charset_sort_key(accept_info):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   930
    """accepted mimetypes must be sorted by :
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   931
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   932
    1/ highest score first
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   933
    2/ most specific charset first, e.g. :
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   934
       - 'utf-8' is more specific than '*'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   935
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   936
    raw_value, value, score = accept_info
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   937
    if value == '*':
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   938
        score -= 0.0001
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   939
    return 1./score, value
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   940
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   941
def _parse_accept_header(raw_header, value_parser=None, value_sort_key=None):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   942
    """returns an ordered list accepted types
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   943
7273
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   944
    :param value_parser: a function to parse a raw accept chunk. If None
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   945
    is provided, the function defaults to identity. If a function is provided,
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   946
    it must accept 2 parameters ``value`` and ``other_params``. ``value`` is
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   947
    the value found before the first ';', `other_params` is a dictionary
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   948
    built from all other chunks after this first ';'
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   949
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   950
    :param value_sort_key: a key function to sort values found in the accept
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   951
    header. This function will be passed a 3-tuple
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   952
    (raw_value, parsed_value, score). If None is provided, the default
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   953
    sort_key is 1./score
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   954
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   955
    :return: a list of 3-tuple (raw_value, parsed_value, score),
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   956
    ordered by score. ``parsed_value`` will be the return value of
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   957
    ``value_parser(raw_value)``
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   958
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   959
    if value_sort_key is None:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   960
        value_sort_key = lambda infos: 1./infos[-1]
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   961
    values = []
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   962
    for info in raw_header.split(','):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   963
        score = 1.0
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   964
        other_params = {}
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   965
        try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   966
            value, infodef = info.split(';', 1)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   967
        except ValueError:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   968
            value = info
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   969
        else:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   970
            for info in infodef.split(';'):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   971
                try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   972
                    infokey, infoval = info.split('=')
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   973
                    if infokey == 'q': # XXX 'level'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   974
                        score = float(infoval)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   975
                        continue
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   976
                except ValueError:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   977
                    continue
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   978
                other_params[infokey] = infoval
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   979
        parsed_value = value_parser(value, other_params) if value_parser else value
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   980
        values.append( (value.strip(), parsed_value, score) )
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   981
    values.sort(key=value_sort_key)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   982
    return values
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   983
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   984
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   985
def _mimetype_parser(value, other_params):
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   986
    """return a 3-tuple
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   987
    (type, subtype, type_params) corresponding to the mimetype definition
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   988
    e.g. : for 'text/*', `mimetypeinfo` will be ('text', '*', {}), for
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   989
    'text/html;level=1', `mimetypeinfo` will be ('text', '*', {'level': '1'})
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   990
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   991
    try:
7273
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
   992
        media_type, media_subtype = value.strip().split('/', 1)
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   993
    except ValueError: # safety belt : '/' should always be present
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   994
        media_type = value.strip()
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   995
        media_subtype = '*'
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   996
    return (media_type, media_subtype, other_params)
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   997
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   998
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   999
ACCEPT_HEADER_PARSER = {
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1000
    'accept': (_mimetype_parser, _mimetype_sort_key),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1001
    'accept-charset': (None, _charset_sort_key),
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1002
    }
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1003
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1004
from cubicweb import set_log_methods
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1005
set_log_methods(CubicWebRequestBase, LOGGER)