def _view()

in bedrock/redirects/util.py [0:0]


    def _view(request, *args, **kwargs):
        # don't want to have 'None' in substitutions
        kwargs = {k: v or "" for k, v in kwargs.items()}
        args = [x or "" for x in args]

        # If it's a callable, call it and get the url out.
        if callable(to):
            to_value = to(request, *args, **kwargs)
        else:
            to_value = to

        if to_value.startswith("/") or HTTP_RE.match(to_value):
            redirect_url = to_value
        else:
            try:
                redirect_url = reverse(to_value, args=to_args, kwargs=to_kwargs)
                # reverse() will give us, by default, a redirection
                # with settings.LANGAUGE_CODE as the prefixed language.
                # We don't want this by default, only if explicitly requested
                redirect_url = _drop_lang_code(redirect_url)
            except NoReverseMatch:
                # Assume it's a URL
                redirect_url = to_value

        if prepend_locale and redirect_url.startswith("/") and kwargs.get("locale"):
            redirect_url = "/{locale}" + redirect_url.lstrip("/")

        # use info from url captures.
        if args or kwargs:
            if args:
                redirect_url = redirect_url.format(*args)
            if kwargs:
                # Use `defaultdict` to default to empty string for missing kwargs.
                redirect_url = redirect_url.format_map(defaultdict(str, kwargs))
            redirect_url = strip_tags(redirect_url)

        if query:
            if merge_query:
                req_query = parse_qs(request.META.get("QUERY_STRING", ""))
                query.update(req_query)

            querystring = urlencode(query, doseq=True)
        elif query is None:
            querystring = request.META.get("QUERY_STRING", "")
        else:
            querystring = ""

        if querystring:
            redirect_url = "?".join([redirect_url, querystring])

        if anchor:
            redirect_url = "#".join([redirect_url, anchor])

        if PROTOCOL_RELATIVE_RE.match(redirect_url):
            redirect_url = "/" + redirect_url.lstrip("/")

        return redirect_class(redirect_url)