def view_answer(request, slug, language, answer_id): answer_page = get_object_or_404( AnswerPage, language=language, answer_base__id=answer_id) if answer_page.live is False: raise Http404 if answer_page.redirect_to_page: new_page = answer_page.redirect_to_page return redirect(new_page.url, permanent=True) if f"{slug}-{language}-{answer_id}" != answer_page.slug: return redirect(answer_page.url, permanent=True) # We don't want to call answer_page.serve(request) here because that # would bypass wagtail-sharing logic that allows for review of draft # revisions via a sharing site. try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: return answer_page.serve(request) page, args, kwargs = ServeView.route( sharing_site.site, request, request.path ) return ServeView.serve(page, request, args, kwargs)
def dispatch(self, request, path): if request.method.upper() != 'GET': return wagtail_serve(request, path) try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: return wagtail_serve(request, path) page, args, kwargs = self.get_requested_page(sharing_site.site, request, path) return self.serve_latest_revision(page, request, args, kwargs)
def dispatch(self, request, path): if request.method.upper() != "GET": return wagtail_serve(request, path) try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: sharing_site = None if not sharing_site: return wagtail_serve(request, path) page, args, kwargs = self.route(sharing_site.site, request, path) return self.serve(page, request, args, kwargs)
def view_answer(request, slug, language, answer_id): answer_page = get_object_or_404(AnswerPage, language=language, answer_base__id=answer_id) if answer_page.live is False: raise Http404 if answer_page.redirect_to_page: new_page = answer_page.redirect_to_page return redirect(new_page.url, permanent=True) if "{}-{}-{}".format(slug, language, answer_id) != answer_page.slug: return redirect(answer_page.url, permanent=True) else: try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: return answer_page.serve(request) page, args, kwargs = ServeView.get_requested_page( sharing_site.site, request, request.path) return ServeView.serve_latest_revision(page, request, args, kwargs)
def view_answer(request, slug, language, answer_id): answer_page = get_object_or_404( AnswerPage, language=language, answer_base__id=answer_id) if answer_page.live is False: raise Http404 if answer_page.redirect_to: new_page = answer_page.redirect_to.answer_pages.get(language=language) return redirect(new_page.url) if "{}-{}-{}".format(slug, language, answer_id) != answer_page.slug: return redirect(answer_page.url) else: try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: return answer_page.serve(request) page, args, kwargs = ServeView.get_requested_page( sharing_site.site, request, request.path) return ServeView.serve_latest_revision( page, request, args, kwargs)
def dispatch(self, request, path): if request.method.upper() != "GET": return wagtail_serve(request, path) try: sharing_site = SharingSite.find_for_request(request) except SharingSite.DoesNotExist: sharing_site = None if not sharing_site: return wagtail_serve(request, path) # Call the before_route_page hook. for fn in hooks.get_hooks("before_route_page"): result = fn(request, path) if isinstance(result, HttpResponse): return result page, args, kwargs = self.route(sharing_site.site, request, path) return self.serve(page, request, args, kwargs)
def test_root_url_other_port_http(self): site = SharingSite(hostname='test.hostname', port=1234) self.assertEqual(site.root_url, 'http://test.hostname:1234')
def test_root_url_port_443_https(self): site = SharingSite(hostname='test.hostname', port=443) self.assertEqual(site.root_url, 'https://test.hostname')
def test_root_url_port_80_http_no_port(self): site = SharingSite(hostname='test.hostname', port=80) self.assertEqual(site.root_url, 'http://test.hostname')
def test_find_for_request_no_server_port(self): request = self.factory.get('/') del request.META['SERVER_PORT'] with self.assertRaises(SharingSite.DoesNotExist): SharingSite.find_for_request(request)
def test_find_for_request_hostname_keyerror(self): request = self.factory.get('/', SERVER_PORT=5678) del request.META['SERVER_NAME'] with self.assertRaises(SharingSite.DoesNotExist): SharingSite.find_for_request(request)
def test_find_for_request_wrong_port(self): self.create(hostname='hostname', port=1234) request = self.factory.get('/', HTTP_HOST='hostname', SERVER_PORT=5678) with self.assertRaises(SharingSite.DoesNotExist): SharingSite.find_for_request(request)
def test_find_for_request_no_sites(self): request = self.factory.get('/', HTTP_HOST='hostname', SERVER_PORT=1234) with self.assertRaises(SharingSite.DoesNotExist): SharingSite.find_for_request(request)
def test_find_for_request(self): sharing_site = self.create(hostname='hostname', port=1234) request = self.factory.get('/', HTTP_HOST='hostname', SERVER_PORT=1234) self.assertEqual(SharingSite.find_for_request(request), sharing_site)