Python flask.request 模块,_get_current_object() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用flask.request._get_current_object()

项目:pillar    作者:armadillica    | 项目源码 | 文件源码
def __fake_request_url_rule(self, method: str, url_path: str):
        """Tries to force-set the request URL rule.

        This is required by Eve (since 0.70) to be able to construct a
        Location HTTP header that points to the resource item.

        See post_internal, put_internal and patch_internal.
        """

        import werkzeug.exceptions as wz_exceptions

        with self.test_request_context(method=method, path=url_path) as ctx:
            try:
                rule, _ = ctx.url_adapter.match(url_path, method=method, return_rule=True)
            except (wz_exceptions.MethodNotAllowed, wz_exceptions.NotFound):
                # We're POSTing things that we haven't told Eve are POSTable. Try again using the
                # GET method.
                rule, _ = ctx.url_adapter.match(url_path, method='GET', return_rule=True)
            current_request = request._get_current_object()
            current_request.url_rule = rule

            yield ctx
项目:Hawkeye    作者:tozhengxq    | 项目源码 | 文件源码
def process_view(self, app, view_func, view_kwargs):
        """ This method is called just before the flask view is called.
        This is done by the dispatch_request method.
        """
        real_request = request._get_current_object()
        try:
            toolbar = self.debug_toolbars[real_request]
        except KeyError:
            return view_func

        for panel in toolbar.panels:
            new_view = panel.process_view(real_request, view_func, view_kwargs)
            if new_view:
                view_func = new_view

        return view_func
项目:partycrasher    作者:naturalness    | 项目源码 | 文件源码
def determine_user_agent_facing_host():
    """
    Determines the host for the active request as seen by the User-Agent
    (client), assuming proxies along the way have been being truthful.
    """

    # Request is a proxy object, and cannot be weakly-referenced; instead,
    # get a reference to true object.
    true_request = request._get_current_object()
    if true_request in HOST_CACHE:
        return HOST_CACHE[true_request]
    else:
        host = calculate_user_agent_facing_host()
        HOST_CACHE[true_request] = host
        return host
项目:dodotable    作者:spoqa    | 项目源码 | 文件源码
def get_session(self):
        ctx = request._get_current_object()
        try:
            session = ctx._current_session
        except AttributeError:
            return None
        else:
            return session
项目:zeus    作者:getsentry    | 项目源码 | 文件源码
def get_worker():
    try:
        return request._get_current_object()
    except RuntimeError:
        return None
项目:Hawkeye    作者:tozhengxq    | 项目源码 | 文件源码
def process_request(self):
        g.debug_toolbar = self

        if not self._show_toolbar():
            return

        real_request = request._get_current_object()

        self.debug_toolbars[real_request] = (
            DebugToolbar(real_request, self.jinja_env))

        for panel in self.debug_toolbars[real_request].panels:
            panel.process_request(real_request)
项目:Hawkeye    作者:tozhengxq    | 项目源码 | 文件源码
def teardown_request(self, exc):
        self.debug_toolbars.pop(request._get_current_object(), None)
项目:Hawkeye    作者:tozhengxq    | 项目源码 | 文件源码
def process_response(self, response):
        real_request = request._get_current_object()
        if real_request not in self.debug_toolbars:
            return response

        # Intercept http redirect codes and display an html page with a
        # link to the target.
        if current_app.config['DEBUG_TB_INTERCEPT_REDIRECTS']:
            if (response.status_code in self._redirect_codes and
                    not real_request.is_xhr):
                redirect_to = response.location
                redirect_code = response.status_code
                if redirect_to:
                    content = self.render('redirect.html', {
                        'redirect_to': redirect_to,
                        'redirect_code': redirect_code
                    })
                    response.content_length = len(content)
                    response.location = None
                    response.response = [content]
                    response.status_code = 200

        # If the http response code is 200 then we process to add the
        # toolbar to the returned html response.
        if not (response.status_code == 200 and
                response.is_sequence and
                response.headers['content-type'].startswith('text/html')):
            return response

        response_html = response.data.decode(response.charset)

        no_case = response_html.lower()
        body_end = no_case.rfind('</body>')

        if body_end >= 0:
            before = response_html[:body_end]
            after = response_html[body_end:]
        elif no_case.startswith('<!doctype html>'):
            before = response_html
            after = ''
        else:
            warnings.warn('Could not insert debug toolbar.'
                          ' </body> tag not found in response.')
            return response

        toolbar = self.debug_toolbars[real_request]

        for panel in toolbar.panels:
            panel.process_response(real_request, response)

        toolbar_html = toolbar.render_toolbar()

        content = ''.join((before, toolbar_html, after))
        content = content.encode(response.charset)
        response.response = [content]
        response.content_length = len(content)

        return response