web/request.py
author Damien Garaud <damien.garaud@logilab.fr>
Tue, 04 Mar 2014 18:28:46 +0100
changeset 9549 a1f94c2d2a39
parent 9469 032825bbacab
child 9561 3bdf85279c67
permissions -rw-r--r--
[web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'. Also update the related docstring of the __init__ method.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
     1
# copyright 2003-2013 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
8600
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
    25
import urllib
8752
e19f4bba89cd Add CubicWebRequestBase.content (closes #2742453)
Julien Cristau <julien.cristau@logilab.fr>
parents: 8732
diff changeset
    26
from StringIO import StringIO
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
    27
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
    28
from Cookie import SimpleCookie
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
    29
from calendar import timegm
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
    30
from datetime import date, datetime
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
from urlparse import urlsplit
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
    32
import httplib
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
from itertools import count
6078
c08e74b0609e fix missing import in web.request
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 6064
diff changeset
    34
from warnings import warn
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
from rql.utils import rqlvar_maker
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
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
    39
from logilab.common.deprecation import deprecated
2312
af4d8f75c5db use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2258
diff changeset
    40
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
    41
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
    42
from cubicweb.req import RequestSessionBase
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
from cubicweb.dbapi import DBAPIRequest
6063
17a53f6dea42 [request] js is defined in uilib, not utils
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6062
diff changeset
    44
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
    45
from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
    46
from cubicweb.view import 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
    47
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
    48
                          RequestError, StatusResponse)
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
    49
from cubicweb.web.httpcache import GMTOFFSET, get_validators
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
    50
from cubicweb.web.http_headers import Headers, Cookie, parseDateTime
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
662
6f867ab70e3d move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents: 610
diff changeset
    52
_MARKER = object()
6f867ab70e3d move _MARKER from appobject to web.request
sylvain.thenault@logilab.fr
parents: 610
diff changeset
    53
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
    54
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
    55
    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
    56
    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
    57
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
def list_form_param(form, param, pop=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    """get param from form parameters and return its value as a list,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    skipping internal markers if any
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
    * if the parameter isn't defined, return an empty list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    * if the parameter is a single (unicode) value, return a list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
      containing that value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    * if the parameter is already a list or tuple, just skip internal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
      markers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
8238
087bb529035c [spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8177
diff changeset
    69
    if pop is True, the parameter is removed from the form dictionary
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    if pop:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            value = form.pop(param)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            return []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        value = form.get(param, ())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    if value is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        value = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    elif not isinstance(value, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        value = [value]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
    return [v for v in value if v != INTERNAL_FIELD_VALUE]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
    86
class _CubicWebRequestBase(RequestSessionBase):
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    87
    """abstract HTTP request, should be extended according to the HTTP backend
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    88
    Immutable attributes that describe the received query and generic configuration
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    89
    """
8128
0a927fe4541b [controllers] deprecate JSonController and implement AjaxController / ajax-func registry (closes #2110265)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8082
diff changeset
    90
    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
    91
9549
a1f94c2d2a39 [web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents: 9469
diff changeset
    92
    def __init__(self, vreg, https=False, form=None, headers=None):
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    93
        """
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    94
        :vreg: Vregistry,
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    95
        :https: boolean, s this a https request
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    96
        :form: Forms value
9549
a1f94c2d2a39 [web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents: 9469
diff changeset
    97
        :headers: dict, request header
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
    98
        """
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
    99
        super(_CubicWebRequestBase, self).__init__(vreg)
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   100
        #: (Boolean) Is this an https request.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        self.https = https
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8316
diff changeset
   102
        #: User interface property (vary with https) (see :ref:`uiprops`)
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   103
        self.uiprops = None
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8316
diff changeset
   104
        #: url for serving datadir (vary with https) (see :ref:`resources`)
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   105
        self.datadir_url = None
8869
a46197b1696f [webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8732
diff changeset
   106
        if https and vreg.config.https_uiprops is not None:
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
   107
            self.uiprops = vreg.config.https_uiprops
8869
a46197b1696f [webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8732
diff changeset
   108
        else:
a46197b1696f [webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8732
diff changeset
   109
            self.uiprops = vreg.config.uiprops
a46197b1696f [webconfig] fallback to default configs if https ones are not defined
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8732
diff changeset
   110
        if https and vreg.config.https_datadir_url is not None:
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
   111
            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
   112
        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
   113
            self.datadir_url = vreg.config.datadir_url
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   114
        #: 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
   115
        self.html_headers = HTMLHead(self)
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   116
        #: received headers
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   117
        self._headers_in = Headers()
9549
a1f94c2d2a39 [web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents: 9469
diff changeset
   118
        if headers is not None:
a1f94c2d2a39 [web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents: 9469
diff changeset
   119
            for k, v in headers.iteritems():
a1f94c2d2a39 [web] Avoid using an empty dict as default parameter of 'CubicWebRequestBase'.
Damien Garaud <damien.garaud@logilab.fr>
parents: 9469
diff changeset
   120
                self._headers_in.addRawHeader(k, v)
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   121
        #: form parameters
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
        self.setup_params(form)
8752
e19f4bba89cd Add CubicWebRequestBase.content (closes #2742453)
Julien Cristau <julien.cristau@logilab.fr>
parents: 8732
diff changeset
   123
        #: received body
e19f4bba89cd Add CubicWebRequestBase.content (closes #2742453)
Julien Cristau <julien.cristau@logilab.fr>
parents: 8732
diff changeset
   124
        self.content = StringIO()
9037
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   125
        # use header to set default language (may ne overwriten by user one later)
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   126
        if vreg.config.get('language-negociation', False):
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   127
            # http negociated language
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   128
            accepted_languages = self.header_accept_language()
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   129
        else:
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   130
            accepted_languages = ()
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   131
        for lang in accepted_languages:
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   132
            if lang in self.translations:
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   133
                self.set_language(lang)
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   134
                break
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   135
        else:
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   136
            self.set_default_language(vreg)
6fd0ac6506cb [web-request] handle default language earlier
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9022
diff changeset
   137
        # 3. default language
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   138
        #: dictionary that may be used to store request data that has to be
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   139
        #: shared among various components used to publish the request (views,
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   140
        #: controller, application...)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        self.data = {}
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   142
        #:  search state: 'normal' or 'linksearch' (eg searching for an object
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   143
        #:  to create a relation with another)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   144
        self.search_state = ('normal',)
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   145
        #: page id, set by htmlheader template
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        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
   147
        self._set_pageid()
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   148
        # prepare output header
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   149
        #: Header used for the final response
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   150
        self.headers_out = Headers()
8312
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8309
diff changeset
   151
        #: HTTP status use by the final response
6c2119509fac [web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8309
diff changeset
   152
        self.status_out  = 200
3816
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   153
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   154
    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
   155
        """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
   156
        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
   157
        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
   158
        """
37b376bb4088 [web] set pageid at request instanciation rather than in htmlheader template
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3275
diff changeset
   159
        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
   160
        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
   161
            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
   162
            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
   163
        self.pageid = pid
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
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
   165
    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
   166
        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
   167
             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
   168
        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
   169
    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
   170
        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
   171
             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
   172
        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
   173
    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
   174
8309
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   175
    def base_url(self, secure=None):
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   176
        """return the root url of the instance
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   177
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   178
        secure = False -> base-url
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   179
        secure = None  -> https-url if req.https
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   180
        secure = True  -> https if it exist
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   181
        """
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   182
        if secure is None:
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   183
            secure = self.https
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   184
        base_url = None
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   185
        if secure:
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   186
            base_url = self.vreg.config.get('https-url')
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   187
        if base_url is None:
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   188
            base_url = super(_CubicWebRequestBase, self).base_url()
8309
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   189
        return base_url
48ef505aa9f9 [request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8271
diff changeset
   190
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
   191
    @property
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   192
    def authmode(self):
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   193
        """Authentification mode of the instance
8480
086cff6a306a [book] fix build warnings/errors. Closes #2430042
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8316
diff changeset
   194
        (see :ref:`WebServerConfig`)"""
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   195
        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
   196
8271
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   197
    # Various variable generator.
3bd9b317b1d8 [req, doc] add some documentation of CubicWebRequestBase attributes
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8239
diff changeset
   198
6581
4a3b264589dc [dbapi] enhanced web compatible request when using dbapi
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6564
diff changeset
   199
    @property
2801
7ef4c1c9266b fix syntax error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2792
diff changeset
   200
    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
   201
        """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
   202
        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
   203
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   204
        * 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
   205
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   206
        * 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
   207
          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
   208
        """
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   209
        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
   210
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
   211
    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
   212
        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
   213
        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
   214
            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
   215
            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
   216
        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
   217
4366
d51f28ba9399 fif inlined relation forms pb w/ new ajax forms.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4277
diff changeset
   218
    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
   219
        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
   220
        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
   221
            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
   222
            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
   223
        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
   224
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    # input form parameters management ########################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   226
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    # common form parameters which should be protected against html values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    # XXX can't add 'eid' for instance since it may be multivalued
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    # 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
   230
    no_script_form_params = set(('vid',
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   231
                                 'etype',
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
                                 'vtitle', 'title',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
                                 '__message',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
                                 '__redirectvid', '__redirectrql'))
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   235
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
    def setup_params(self, params):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        """WARNING: we're intentionaly leaving INTERNAL_FIELD_VALUE here
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   239
        subclasses should overrides to
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        """
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
   241
        self.form = {}
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        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
   243
            return
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
        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
   245
        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
   246
            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
   247
                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
   248
                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
   249
                    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
   250
            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
   251
                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
   252
            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
   253
                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
   254
            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
   255
                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
   256
            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
   257
                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
   258
                     '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
   259
                     '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
   260
                     '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
   261
                     '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
   262
                     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
   263
                self.set_message(val)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
            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
   265
                self.form[param] = val
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   266
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
   267
    def no_script_form_param(self, param, value):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
        """ensure there is no script in a user form param
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
        by default return a cleaned string instead of raising a security
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   271
        exception
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
        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
   274
        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
   275
        against script kiddies
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
        """
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
   277
        # 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
   278
        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
   279
            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
   280
                       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
   281
            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
   282
        return remove_html_tags(value)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   283
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    def list_form_param(self, param, form=None, pop=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        """get param from form parameters and return its value as a list,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
        skipping internal markers if any
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   287
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        * if the parameter isn't defined, return an empty list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
        * if the parameter is a single (unicode) value, return a list
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
          containing that value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
        * if the parameter is already a list or tuple, just skip internal
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
          markers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
8238
087bb529035c [spelling] fix dictionnary -> dictionary typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8177
diff changeset
   294
        if pop is True, the parameter is removed from the form dictionary
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
        if form is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
            form = self.form
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   298
        return list_form_param(form, param, pop)
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   299
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
    def reset_headers(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
        """used by AutomaticWebTest to clear html headers between tests on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
        the same resultset
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
        """
7762
a3f9ba4d44eb [web] add option to make resources-concat optional (implements #1910615)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7660
diff changeset
   304
        self.html_headers = HTMLHead(self)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
        return self
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
    # web state helpers #######################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   308
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
   309
    @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
   310
    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
   311
        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
   312
            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
   313
        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
   314
            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
   315
                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
   316
            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
   317
                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
   318
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
    def set_message(self, msg):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
        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
   321
        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
   322
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
    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
   324
        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
   325
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
   326
    @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
   327
    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
   328
        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
   329
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
   330
    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
   331
        # 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
   332
        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
   333
        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
   334
        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
   335
        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
   336
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
   337
    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
   338
        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
   339
        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
   340
        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
   341
            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
   342
        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
   343
            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
   344
        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
   345
        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
   346
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
   347
    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
   348
        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
   349
            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
   350
        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
   351
            del self._msgid
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   352
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
    def update_search_state(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
        """update the current search state"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
        searchstate = self.form.get('__mode')
8541
5b6bc27ece6e [web] search state doesn't need cnx, remove useless test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8538
diff changeset
   356
        if not searchstate:
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
   357
            searchstate = self.session.data.get('search_state', 'normal')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
        self.set_search_state(searchstate)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
    def set_search_state(self, searchstate):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
        """set a new search state"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
        if searchstate is None or searchstate == 'normal':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
            self.search_state = (searchstate or 'normal',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
            self.search_state = ('linksearch', searchstate.split(':'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
            assert len(self.search_state[-1]) == 4
8541
5b6bc27ece6e [web] search state doesn't need cnx, remove useless test
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8538
diff changeset
   367
        self.session.data['search_state'] = searchstate
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
1173
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   369
    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
   370
        """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
   371
        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
   372
        """
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   373
        try:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   374
            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
   375
        except IndexError:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   376
            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
   377
        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
   378
            if etype != searchedtype:
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   379
                return False
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   380
        return True
8f123fd081f4 forgot to add that expected method (was a function in view.__init__)
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   381
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
    def update_breadcrumbs(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
        """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
   384
        searchstate = self.session.data.get('search_state')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
        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
   386
            breadcrumbs = self.session.data.get('breadcrumbs')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
            if breadcrumbs is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
                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
   389
                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
   390
                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
   391
            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
   392
                url = self.url()
4974
025a491bad0c take care to empty breadcrumbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4868
diff changeset
   393
                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
   394
                    breadcrumbs.append(url)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
    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
   397
        breadcrumbs = self.session.data.get('breadcrumbs')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
        if breadcrumbs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
            return breadcrumbs.pop()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        return self.base_url()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
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
   402
    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
   403
        """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
   404
        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
   405
7793
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   406
        `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
   407
8a330017ca4d [doc] add some documentation in cubicweb.web.action
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 7660
diff changeset
   408
        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
   409
        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
   410
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
        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
   412
        """
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
        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
   414
            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
   415
        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
   416
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
   417
    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
   418
        """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
   419
        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
   420
        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
   421
        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
   422
        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
   423
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
   424
        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
   425
        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
   426
        ':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
   427
        (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
   428
        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
   429
        """
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
   430
        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
   431
        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
   432
        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
   433
            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
   434
                 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
   435
            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
   436
        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
   437
        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
   438
        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
   439
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
    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
   441
        cbname = build_cb_uid(func.__name__)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   442
        def _cb(req):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
            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
   444
                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
   445
            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
   446
                self.unregister_callback(self.pageid, cbname)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   447
        self.set_page_data(cbname, _cb)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
        return cbname
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   449
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   450
    def unregister_callback(self, pageid, cbname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   451
        assert pageid is not None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   452
        assert cbname.startswith('cb_')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   453
        self.info('unregistering callback %s for pageid %s', cbname, pageid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   454
        self.del_page_data(cbname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   455
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   456
    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
   457
        if self.session is not None: # XXX
8696
0bb18407c053 [toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   458
            for key in list(self.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
   459
                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
   460
                    del self.session.data[key]
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   461
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
    # web edition helpers #####################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   463
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
    @cached # so it's writed only once
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   465
    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
   466
        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
   467
        self.add_js(fckeditor_url, localfile=False)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   468
        self.html_headers.define_var('fcklang', self.lang)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   469
        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
   470
                                     self.data_url('cubicweb.fckcwconfig.js'))
1013
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 940
diff changeset
   471
    def use_fckeditor(self):
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 940
diff changeset
   472
        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
   473
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   474
    def edited_eids(self, withtype=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   475
        """return a list of edited eids"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   476
        yielded = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   477
        # warning: use .keys since the caller may change `form`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   478
        form = self.form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   479
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
            eids = form['eid']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   481
        except KeyError:
4155
80cc9c6ddcf0 NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4154
diff changeset
   482
            raise NothingToEdit(self._('no selected entities'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   483
        if isinstance(eids, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   484
            eids = (eids,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   485
        for peid in eids:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   486
            if withtype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   487
                typekey = '__type:%s' % peid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   488
                assert typekey in form, 'no entity type specified'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   489
                yield peid, form[typekey]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   490
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
                yield peid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   492
            yielded = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   493
        if not yielded:
4155
80cc9c6ddcf0 NothingToEdit is not a ValidationError, simplify
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4154
diff changeset
   494
            raise NothingToEdit(self._('no selected entities'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   495
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   496
    # 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
   497
    def extract_entity_params(self, eid, minparams=3):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   498
        """extract form parameters relative to the given eid"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   499
        params = {}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   500
        eid = str(eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   501
        form = self.form
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   502
        for param in form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   503
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   504
                name, peid = param.split(':', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   505
            except ValueError:
7639
5c9e94a09e97 [web form] avoid mis-formated warning for _cw_fields
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7536
diff changeset
   506
                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
   507
                    self.warning('param %s mis-formatted', param)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   508
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   509
            if peid == eid:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   510
                value = form[param]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   511
                if value == INTERNAL_FIELD_VALUE:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   512
                    value = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   513
                params[name] = value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   514
        params['eid'] = eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   515
        if len(params) < minparams:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   516
            raise RequestError(self._('missing parameters for entity %s') % eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   517
        return params
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   518
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
   519
    # XXX this should go to the GenericRelationsField. missing edition cancel protocol.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   520
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   521
    def remove_pending_operations(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   522
        """shortcut to clear req's pending_{delete,insert} entries
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   523
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   524
        This is needed when the edition is completed (whether it's validated
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   525
        or cancelled)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   526
        """
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
   527
        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
   528
        self.session.data.pop('pending_delete', None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   529
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   530
    def cancel_edition(self, errorurl):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   531
        """remove pending operations and `errorurl`'s specific stored data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   532
        """
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
   533
        self.session.data.pop(errorurl, None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   534
        self.remove_pending_operations()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   535
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   536
    # high level methods for HTTP headers management ##########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   537
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   538
    # must be cached since login/password are popped from the form dictionary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   539
    # and this method may be called multiple times during authentication
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   540
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   541
    def get_authorization(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   542
        """Parse and return the Authorization header"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   543
        if self.authmode == "cookie":
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   544
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   545
                user = self.form.pop("__login")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   546
                passwd = self.form.pop("__password", '')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   547
                return user, passwd.encode('UTF8')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   548
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   549
                self.debug('no login/password in form params')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   550
                return None, None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   551
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   552
            return self.header_authorization()
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   553
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   554
    def get_cookie(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   555
        """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
   556
        # XXX use http_headers implementation
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   557
        try:
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   558
            return SimpleCookie(self.get_header('Cookie'))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   559
        except KeyError:
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   560
            return SimpleCookie()
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   561
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   562
    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
   563
        """set / update a cookie
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   564
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   565
        by default, cookie will be available for the next 5 minutes.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   566
        Give maxage = None to have a "session" cookie expiring when the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   567
        client close its browser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   568
        """
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   569
        if isinstance(name, SimpleCookie):
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   570
            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
   571
                 '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
   572
                 DeprecationWarning, stacklevel=2)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   573
            secure = name[value]['secure']
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   574
            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
   575
        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
   576
            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
   577
            expires = maxage + time.time()
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   578
        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
   579
            # 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
   580
            # 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
   581
            # 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
   582
            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
   583
            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
   584
        else:
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   585
            expires = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   586
        # 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
   587
        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
   588
                        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
   589
        self.headers_out.addHeader('Set-cookie', cookie)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   590
7855
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   591
    def remove_cookie(self, name, bwcompat=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   592
        """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
   593
        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
   594
            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
   595
                 DeprecationWarning, stacklevel=2)
54283a5b7afc [web request] fix cookie 'expires' formating (closes #1953945)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7793
diff changeset
   596
            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
   597
        self.set_cookie(name, '', maxage=0, expires=date(2000, 1, 1))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   598
8611
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   599
    def set_content_type(self, content_type, filename=None, encoding=None,
8612
3bb96935a6b9 [web] use `inline` `Content-Disposition` by default (closes #2535734)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8611
diff changeset
   600
                         disposition='inline'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   601
        """set output content type for this request. An optional filename
8611
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   602
        may be given.
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   603
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   604
        The disposition argument may be `attachement` or `inline` as specified
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   605
        for the Content-disposition HTTP header. The disposition parameter have
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   606
        no effect if no filename are specified.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   607
        """
6353
b622602f8e9d don't add another encoding if already present in content-type
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 6259
diff changeset
   608
        if content_type.startswith('text/') and ';charset=' not in content_type:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   609
            content_type += ';charset=' + (encoding or self.encoding)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   610
        self.set_header('content-type', content_type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   611
        if filename:
8611
51068fe1e39a [web] allow configuration of the Content-disposition value
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8610
diff changeset
   612
            header = [disposition]
8610
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   613
            unicode_filename = None
8600
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
   614
            try:
8610
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   615
                ascii_filename = filename.encode('ascii')
8600
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
   616
            except UnicodeEncodeError:
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
   617
                # fallback filename for very old browser
8610
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   618
                unicode_filename = filename
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   619
                ascii_filename = filename.encode('ascii', 'ignore')
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   620
            # escape " and \
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   621
            # see http://greenbytes.de/tech/tc2231/#attwithfilenameandextparamescaped
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   622
            ascii_filename = ascii_filename.replace('\x5c', r'\\').replace('"', r'\"')
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   623
            header.append('filename="%s"' % ascii_filename)
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   624
            if unicode_filename is not None:
8600
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
   625
                # encoded filename according RFC5987
8610
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   626
                urlquoted_filename = urllib.quote(unicode_filename.encode('utf-8'), '')
b1145ad53999 [downloadable] fix filename HTTP header for simple name with space (closes #2535715)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8600
diff changeset
   627
                header.append("filename*=utf-8''" + urlquoted_filename)
8600
d74addac92bb [downloadable] fix filename in HTTP header (closes #2522325, #2522324)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8480
diff changeset
   628
            self.set_header('content-disposition', ';'.join(header))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   629
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   630
    # high level methods for HTML headers management ##########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   631
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
   632
    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
   633
        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
   634
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   635
    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
   636
        """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
   637
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   638
        :param jsfiles: a JS filename or a list of JS filenames
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   639
        :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
   640
                          JS filename
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   641
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   642
        if isinstance(jsfiles, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   643
            jsfiles = (jsfiles,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   644
        for jsfile in jsfiles:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   645
            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
   646
                jsfile = self.data_url(jsfile)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   647
            self.html_headers.add_js(jsfile)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   648
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   649
    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
   650
                iespec=u'[if lt IE 8]'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   651
        """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
   652
8032
bcb87336c7d2 [doc] fix most of ReST compilation errors and warnings
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7990
diff changeset
   653
        :param cssfiles: a CSS filename or a list of CSS filenames.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   654
        :param media: the CSS's media if necessary
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   655
        :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
   656
                          CSS filename
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   657
        :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
   658
        :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
   659
                       the css inclusion. cf:
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   660
                       http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   661
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   662
        if isinstance(cssfiles, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   663
            cssfiles = (cssfiles,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   664
        if ieonly:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   665
            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
   666
                extraargs = [iespec]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   667
                add_css = self.html_headers.add_ie_css
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   668
            else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   669
                return # no need to do anything on non IE browsers
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   670
        else:
4860
cedb6afdb7da [web] fix #736332: iespec functionality for add_css
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4719
diff changeset
   671
            extraargs = []
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   672
            add_css = self.html_headers.add_css
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   673
        for cssfile in cssfiles:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   674
            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
   675
                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
   676
            add_css(cssfile, media, *extraargs)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   677
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
   678
    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
   679
        """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
   680
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
   681
        :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
   682
        :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
   683
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
   684
          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
   685
          - '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
   686
          - '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
   687
          - '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
   688
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
   689
        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
   690
        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
   691
        """
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
   692
        # 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
   693
        # 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
   694
        # 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
   695
        # 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
   696
        extraparams.setdefault('fname', 'view')
8730
d4d9f33fd01b [web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8728
diff changeset
   697
        # remove pageid from the generated URL as it's forced as a parameter
d4d9f33fd01b [web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8728
diff changeset
   698
        # to the loadxhtml call below.
d4d9f33fd01b [web/request] Prune extraneous 'pageid' from generated ajax URL parameters (closes #2758130)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8728
diff changeset
   699
        extraparams.pop('pageid', None)
8728
75be9de9d68e [web] Use the new '/ajax' URL path to access the AjaxController (closes #2758254)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8612
diff changeset
   700
        url = self.build_url('ajax', **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
   701
        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
   702
        # 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
   703
        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
   704
            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
   705
                                         '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
   706
        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
   707
        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
   708
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   709
    # urls/path management ####################################################
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   710
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
   711
    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
   712
        """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
   713
        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
   714
        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
   715
        """
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
   716
        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
   717
            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
   718
            kwargs['_cwmsgid'] = self.set_redirect_message(msg)
9022
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   719
        if not args:
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   720
            method = 'view'
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   721
            if (self.from_controller() == 'view'
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   722
                and not '_restpath' in kwargs):
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   723
                method = self.relative_path(includeparams=False) or 'view'
a1cee6915ea3 [req] drop from_controller on non WebRequest object (Closes #2901079)
pierre-yves
parents: 8974
diff changeset
   724
            args = (method,)
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   725
        return super(_CubicWebRequestBase, self).build_url(*args, **kwargs)
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
   726
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   727
    def url(self, includeparams=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   728
        """return currently accessed url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   729
        return self.base_url() + self.relative_path(includeparams)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   730
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   731
    def selected(self, url):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   732
        """return True if the url is equivalent to currently accessed url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   733
        reqpath = self.relative_path().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   734
        baselen = len(self.base_url())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   735
        return (reqpath == url[baselen:].lower())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   736
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   737
    def base_url_prepend_host(self, hostname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   738
        protocol, roothost = urlsplit(self.base_url())[:2]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   739
        if roothost.startswith('www.'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   740
            roothost = roothost[4:]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   741
        return '%s://%s.%s' % (protocol, hostname, roothost)
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
    def base_url_path(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   744
        """returns the absolute path of the base url"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   745
        return urlsplit(self.base_url())[2]
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   746
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
   747
    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
   748
        """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
   749
        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
   750
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   751
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   752
    def from_controller(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   753
        """return the id (string) of the controller issuing the request"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   754
        controller = self.relative_path(False).split('/', 1)[0]
8696
0bb18407c053 [toward py3k] rewrite dict.keys() and dict.values() (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
   755
        if controller in self.vreg['controllers']:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   756
            return controller
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   757
        return 'view'
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   758
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   759
    def is_client_cache_valid(self):
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   760
        """check if a client cached page exists (as specified in request
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   761
        headers) and is still usable.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   762
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   763
        Return False if the page has to be calculated, else True.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   764
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   765
        Some response cache headers may be set by this method.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   766
        """
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   767
        modified = True
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   768
        if self.get_header('Cache-Control') not in ('max-age=0', 'no-cache'):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   769
            # Here, we search for any invalid 'not modified' condition
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   770
            # see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   771
            validators = get_validators(self._headers_in)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   772
            if validators: # if we have no
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   773
                modified = any(func(val, self.headers_out) for func, val in validators)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   774
        # Forge expected response
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   775
        if modified:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   776
            if 'Expires' not in self.headers_out:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   777
                # Expires header seems to be required by IE7 -- Are you sure ?
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   778
                self.add_header('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   779
            if self.http_method() == 'HEAD':
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   780
                self.status_out = 200
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   781
                # XXX replace by True once validate_cache bw compat method is dropped
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   782
                return 200
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   783
            # /!\ no raise, the function returns and we keep processing the request
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   784
        else:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   785
            # overwrite headers_out to forge a brand new not-modified response
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   786
            self.headers_out = self._forge_cached_headers()
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   787
            if self.http_method() in ('HEAD', 'GET'):
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   788
                self.status_out = httplib.NOT_MODIFIED
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   789
            else:
9229
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   790
                self.status_out = httplib.PRECONDITION_FAILED
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   791
            # XXX replace by True once validate_cache bw compat method is dropped
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   792
            return self.status_out
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   793
        # XXX replace by False once validate_cache bw compat method is dropped
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   794
        return None
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   795
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   796
    @deprecated('[3.18] use .is_client_cache_valid() method instead')
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   797
    def validate_cache(self):
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   798
        """raise a `StatusResponse` exception if a cached page along the way
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   799
        exists and is still usable.
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   800
        """
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   801
        status_code = self.is_client_cache_valid()
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   802
        if status_code is not None:
739ae5366bed [web] stop using deprecated StatusResponse. Closes #3098215
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9138
diff changeset
   803
            raise StatusResponse(status_code)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   804
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   805
    # abstract methods to override according to the web front-end #############
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   806
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   807
    def http_method(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   808
        """returns 'POST', 'GET', 'HEAD', etc."""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   809
        raise NotImplementedError()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   810
8316
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   811
    def _forge_cached_headers(self):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   812
        # overwrite headers_out to forge a brand new not-modified response
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   813
        headers = Headers()
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   814
        for header in (
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   815
            # Required from sec 10.3.5:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   816
            'date', 'etag', 'content-location', 'expires',
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   817
            'cache-control', 'vary',
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   818
            # Others:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   819
            'server', 'proxy-authenticate', 'www-authenticate', 'warning'):
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   820
            value = self._headers_in.getRawHeaders(header)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   821
            if value is not None:
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   822
                headers.setRawHeaders(header, value)
d5b1b75805dd [cache] factorize _validate_cache() logic implemented in wsgi and twisted handlers
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 8314
diff changeset
   823
        return headers
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   824
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   825
    def relative_path(self, includeparams=True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   826
        """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
   827
        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
   828
        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
   829
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   830
        :param includeparams:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   831
           boolean indicating if GET form parameters should be kept in the path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   832
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   833
        raise NotImplementedError()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   834
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   835
    # http headers ############################################################
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   836
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   837
    ### incoming headers
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   838
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   839
    def get_header(self, header, default=None, raw=True):
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   840
        """return the value associated with the given input header, raise
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   841
        KeyError if the header is not set
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   842
        """
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   843
        if raw:
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   844
            return self._headers_in.getRawHeaders(header, [default])[0]
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   845
        return self._headers_in.getHeader(header, default)
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   846
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   847
    def header_accept_language(self):
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   848
        """returns an ordered list of preferred languages"""
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   849
        acceptedlangs = self.get_header('Accept-Language', raw=False) or {}
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   850
        for lang, _ in sorted(acceptedlangs.iteritems(), key=lambda x: x[1],
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   851
                              reverse=True):
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   852
            lang = lang.split('-')[0]
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   853
            yield lang
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   854
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   855
    def header_if_modified_since(self):
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   856
        """If the HTTP header If-modified-since is set, return the equivalent
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   857
        date time value (GMT), else return None
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   858
        """
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   859
        mtime = self.get_header('If-modified-since', raw=False)
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   860
        if mtime:
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   861
            # :/ twisted is returned a localized time stamp
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   862
            return datetime.fromtimestamp(mtime) + GMTOFFSET
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   863
        return None
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   864
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   865
    ### outcoming headers
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   866
    def set_header(self, header, value, raw=True):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   867
        """set an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   868
        if raw:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   869
            # adding encoded header is important, else page content
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   870
            # 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
   871
            # 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
   872
            self.headers_out.setRawHeaders(header, [str(value)])
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   873
        else:
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   874
            self.headers_out.setHeader(header, value)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   875
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   876
    def add_header(self, header, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   877
        """add an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   878
        # adding encoded header is important, else page content
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   879
        # 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
   880
        # 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
   881
        self.headers_out.addRawHeader(header, str(value))
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   882
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   883
    def remove_header(self, header):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   884
        """remove an output HTTP header"""
5155
1dea6e0fdfc1 Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents: 4984
diff changeset
   885
        self.headers_out.removeHeader(header)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   886
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   887
    def header_authorization(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   888
        """returns a couple (auth-type, auth-value)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   889
        auth = self.get_header("Authorization", None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   890
        if auth:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   891
            scheme, rest = auth.split(' ', 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   892
            scheme = scheme.lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   893
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   894
                assert scheme == "basic"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   895
                user, passwd = base64.decodestring(rest).split(":", 1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   896
                # XXX HTTP header encoding: use email.Header?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   897
                return user.decode('UTF8'), passwd
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8645
diff changeset
   898
            except Exception as ex:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   899
                self.debug('bad authorization %s (%s: %s)',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   900
                           auth, ex.__class__.__name__, ex)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   901
        return None, None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   902
1716
b12d9e22bac3 basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
   903
    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
   904
        """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
   905
        try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   906
            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
   907
        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
   908
            value_parser = value_sort_key = None
1716
b12d9e22bac3 basic support for http Accept header (untested)
sylvain.thenault@logilab.fr
parents: 1560
diff changeset
   909
        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
   910
        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
   911
        return (raw_value for (raw_value, parsed_value, score) in values)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   912
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   913
    @deprecated('[3.17] demote_to_html is deprecated as we always serve html')
3094
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   914
    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
   915
        """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
   916
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   917
        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
   918
        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
   919
        """
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   920
        pass
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   921
7187
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   922
8314
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   923
    # xml doctype #############################################################
cfd6ab461849 [Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8312
diff changeset
   924
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   925
    def set_doctype(self, doctype, reset_xmldecl=None):
7187
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   926
        """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
   927
496f51b92154 [views] extend HTMLStream API to be able to change doctype / xmldecl
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7164
diff changeset
   928
        :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
   929
        """
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   930
        if reset_xmldecl is not None:
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   931
            warn('[3.17] reset_xmldecl is deprecated as we only serve html',
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   932
                 DeprecationWarning, stacklevel=2)
9138
bc6e25dbfd04 [web doctype] don't give through reset_xmldecl to avoid double deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8974
diff changeset
   933
        self.main_stream.set_doctype(doctype)
3094
978ed8c2c0e4 [googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2650
diff changeset
   934
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   935
    # page data management ####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   936
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   937
    def get_page_data(self, key, default=None):
5683
ca2badf54858 [request] fix typo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5505
diff changeset
   938
        """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
   939
        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
   940
        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
   941
            return default
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   942
        return page_data.get(key, default)
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   943
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   944
    def set_page_data(self, key, value):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   945
        """set value associated to `key` in current page data"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   946
        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
   947
        page_data = self.session.data.setdefault(self.pageid, {})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   948
        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
   949
        self.session.data[self.pageid] = page_data
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   950
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   951
    def del_page_data(self, key=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   952
        """remove value associated to `key` in current page data
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   953
        if `key` is None, all page data will be cleared
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   954
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   955
        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
   956
            self.session.data.pop(self.pageid, None)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   957
        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
   958
            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
   959
                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
   960
            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
   961
                pass
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   962
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   963
    # user-agent detection ####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   964
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   965
    @cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   966
    def useragent(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   967
        return self.get_header('User-Agent', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   968
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   969
    def ie_browser(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   970
        useragent = self.useragent()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   971
        return useragent and 'MSIE' in useragent
1426
379261551578 remove trailing spaces
sylvain.thenault@logilab.fr
parents: 1421
diff changeset
   972
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   973
    @deprecated('[3.17] xhtml_browser is deprecated (xhtml is no longer served)')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   974
    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
   975
        """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
   976
81c8b5312f9c move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2556
diff changeset
   977
        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
   978
        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
   979
        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
   980
        """
8941
7b26fe71404f drop xhtml content-type support (closes #2065651)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8889
diff changeset
   981
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   982
1421
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   983
    def html_content_type(self):
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   984
        return 'text/html'
77ee26df178f doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents: 1173
diff changeset
   985
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
   986
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   987
class DBAPICubicWebRequestBase(_CubicWebRequestBase, DBAPIRequest):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   988
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   989
    def set_session(self, session):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   990
        """method called by the session handler when the user is authenticated
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   991
        or an anonymous connection is open
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   992
        """
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   993
        super(CubicWebRequestBase, self).set_session(session)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   994
        # set request language
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   995
        user_lang = self.user.properties.get('ui.language')
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   996
        if user_lang is not None:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   997
            lang = self.vreg.typed_value('ui.language', user_lang)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   998
            self.set_language(lang)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
   999
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1000
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1001
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1002
def _cnx_func(name):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1003
    def proxy(req, *args, **kwargs):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1004
        return getattr(req.cnx, name)(*args, **kwargs)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1005
    return proxy
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1006
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1007
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1008
class ConnectionCubicWebRequestBase(_CubicWebRequestBase):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1009
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1010
    def __init__(self, vreg, https=False, form=None, headers={}):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1011
        """"""
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1012
        self.cnx = None
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1013
        self.session = None
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1014
        self.vreg = vreg
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1015
        try:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1016
            # no vreg or config which doesn't handle translations
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1017
            self.translations = vreg.config.translations
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1018
        except AttributeError:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1019
            self.translations = {}
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1020
        super(ConnectionCubicWebRequestBase, self).__init__(vreg, https=https,
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1021
                                                       form=form, headers=headers)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1022
        from cubicweb.dbapi import DBAPISession, _NeedAuthAccessMock
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1023
        self.session = DBAPISession(None)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1024
        self.cnx = self.user = _NeedAuthAccessMock()
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1025
        #: cache entities built during the request
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1026
        self._eid_cache = {}
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1027
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1028
    def set_cnx(self, cnx):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1029
        self.cnx = cnx
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1030
        self.session = cnx._session
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1031
        self._set_user(cnx.user)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1032
        # set user language
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1033
        user_lang = self.user.properties.get('ui.language')
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1034
        if user_lang is not None:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1035
            lang = self.vreg.typed_value('ui.language', user_lang)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1036
            self.set_language(lang)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1037
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1038
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1039
    def execute(self, *args, **kwargs):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1040
        rset = self.cnx.execute(*args, **kwargs)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1041
        rset.req = self
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1042
        return rset
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1043
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1044
    def set_default_language(self, vreg):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1045
        # XXX copy from dbapi
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1046
        try:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1047
            lang = vreg.property_value('ui.language')
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1048
        except Exception: # property may not be registered
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1049
            lang = 'en'
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1050
        try:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1051
            self.set_language(lang)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1052
        except KeyError:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1053
            # this occurs usually during test execution
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1054
            self._ = self.__ = unicode
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1055
            self.pgettext = lambda x, y: unicode(y)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1056
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9402
diff changeset
  1057
    entity_metas = _cnx_func('entity_metas')
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1058
    source_defs = _cnx_func('source_defs')
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1059
    get_shared_data = _cnx_func('get_shared_data')
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1060
    set_shared_data = _cnx_func('set_shared_data')
9469
032825bbacab [multi-sources-removal] Drop entities.source column
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9402
diff changeset
  1061
    describe = _cnx_func('describe') # deprecated XXX
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1062
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1063
    # server-side service call #################################################
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1064
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1065
    def call_service(self, regid, **kwargs):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1066
        return self.cnx.call_service(regid, **kwargs)
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1067
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1068
    # entities cache management ###############################################
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1069
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1070
    def entity_cache(self, eid):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1071
        return self._eid_cache[eid]
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1072
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1073
    def set_entity_cache(self, entity):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1074
        self._eid_cache[entity.eid] = entity
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1075
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1076
    def cached_entities(self):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1077
        return self._eid_cache.values()
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1078
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1079
    def drop_entity_cache(self, eid=None):
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1080
        if eid is None:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1081
            self._eid_cache = {}
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1082
        else:
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1083
            del self._eid_cache[eid]
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1084
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1085
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1086
9071
46885bfa4150 Use new repoapi for the web stack
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9063
diff changeset
  1087
CubicWebRequestBase = ConnectionCubicWebRequestBase
5444
f7fdb5dd82f6 [webconfig] introduce property sheets. Use them to replace external_resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
  1088
7164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1089
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1090
## 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
  1091
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
  1092
    """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
  1093
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1094
    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
  1095
    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
  1096
       - '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
  1097
       - '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
  1098
       - '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
  1099
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1100
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1101
    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
  1102
    # 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
  1103
    # 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
  1104
    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
  1105
        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
  1106
    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
  1107
        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
  1108
    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
  1109
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1110
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
  1111
    """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
  1112
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1113
    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
  1114
    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
  1115
       - '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
  1116
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1117
    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
  1118
    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
  1119
        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
  1120
    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
  1121
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1122
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
  1123
    """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
  1124
7273
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
  1125
    :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
  1126
    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
  1127
    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
  1128
    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
  1129
    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
  1130
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
  1131
    :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
  1132
    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
  1133
    (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
  1134
    sort_key is 1./score
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
  1135
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
  1136
    :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
  1137
    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
  1138
    ``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
  1139
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1140
    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
  1141
        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
  1142
    values = []
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1143
    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
  1144
        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
  1145
        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
  1146
        try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1147
            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
  1148
        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
  1149
            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
  1150
        else:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1151
            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
  1152
                try:
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1153
                    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
  1154
                    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
  1155
                        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
  1156
                        continue
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1157
                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
  1158
                    continue
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1159
                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
  1160
        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
  1161
        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
  1162
    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
  1163
    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
  1164
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1165
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1166
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
  1167
    """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
  1168
    (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
  1169
    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
  1170
    '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
  1171
    """
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1172
    try:
7273
a949fc438029 [etwist] refactor child resources management
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7187
diff changeset
  1173
        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
  1174
    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
  1175
        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
  1176
        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
  1177
    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
  1178
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1179
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1180
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
  1181
    '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
  1182
    '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
  1183
    }
93a19c1831aa [http] implement 1587305: provide better implementation of Accept header parsing + tests
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 7070
diff changeset
  1184
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
  1185
from cubicweb import set_log_methods
9063
383d45cf61fa [webrequest] introduce an alternative implementation using the repoapi
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 9041
diff changeset
  1186
set_log_methods(_CubicWebRequestBase, LOGGER)