cubicweb/etwist/request.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Nov 2016 16:14:17 +0100
changeset 11888 0849a5eb57b8
parent 11870 3a84a79c4ed5
child 11913 4516c3956d46
permissions -rw-r--r--
[rtags] Allow to 'derive' rtags Since some releases, rtags (structure underlying uicfg) have selector and may be copied using something like: new_rtags = deepcopy(original_rtags) new_rtags.__module__ = __name__ new_rtags.__select__ = custom_selector The problem is that starting from that, both rtags wil diverge and changes in original_rtags won't be considered, while we usually want to set a few specific rules only in new_rtags. To fix this problem, this cset introduces the notion of "derivated/parent" rtag, eg: new_rtags = original_rtags.derive(__name__, custom_selector) Beside easier copying, when using the above method changes in original_rtags which are not overriden by new_rtags will be considered since it only hold its specific rules but look among its parent chain for non-found keys. Along the way, flake8 unittest_rtags. Closes #16164880

# copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""Twisted request handler for CubicWeb"""

from six import text_type

from cubicweb.web.request import CubicWebRequestBase


class CubicWebTwistedRequestAdapter(CubicWebRequestBase):
    """ from twisted .req to cubicweb .form
    req.files are put into .form[<filefield>]
    """
    def __init__(self, req, vreg, https):
        self._twreq = req
        super(CubicWebTwistedRequestAdapter, self).__init__(
            vreg, https, req.args, headers=req.received_headers)
        for key, name_stream_list in req.files.items():
            for name, stream in name_stream_list:
                if name is not None:
                    name = text_type(name, self.encoding)
                self.form.setdefault(key, []).append((name, stream))
            # 3.16.4 backward compat
            if len(self.form[key]) == 1:
                self.form[key] = self.form[key][0]
        self.content = self._twreq.content  # stream

    def http_method(self):
        """returns 'POST', 'GET', 'HEAD', etc."""
        return self._twreq.method

    def relative_path(self, includeparams=True):
        """return the normalized path of the request (ie at least relative to
        the instance's root, but some other normalization may be needed so that
        the returned path may be used to compare to generated urls

        :param includeparams:
           boolean indicating if GET form parameters should be kept in the path
        """
        path = self._twreq.uri[1:]  # remove the root '/'
        if not includeparams:
            path = path.split('?', 1)[0]
        return path