def redirect_to(request, url, permanent=False): """ A generic view for redirecting to the provided URL, using a HTTP 200 response with a META-tag based redirect for whitelisted interactive browsers, and falling back to a 300-class HTTP standard redirect for non-interactive browers. :param request: the original HTTP request triggering the response :type request: :class:`django.http.HttpRequest` :param url: the URL to redirect to :type url: :class:`str` :param permanent: if the redirect is permanent or not. Only used for non-interactive clients. :type permanent: :class:`bool` :returns: an HTTP response :rtype: :class:`django.http.HttpResponse` or derived class """ if is_interactive_user_agent(request): return templated_redirect_response(url) else: if permanent: response_cls = HttpResponsePermanentRedirect else: response_cls = HttpResponseRedirect return response_cls(url)
def test_no_user_agent(self): request = self.get_request_for_user_agent(None) self.assertFalse(is_interactive_user_agent(request))
def test_noninteractive_user_agents(self): for agent in self.NONINTERACTIVE_USER_AGENTS: request = self.get_request_for_user_agent(agent) self.assertFalse(is_interactive_user_agent(request))