From 3617f702c0c7dd1ea9c0b163998d26471d3649bd Mon Sep 17 00:00:00 2001 From: Deimos Date: Fri, 21 Dec 2018 14:37:40 -0700 Subject: [PATCH] Use permanent (HTTP 301) redirects in shortener This should be more correct overall, and in the (rare) cases where the destination changes (due to a topic being moved to a different group or something similar), the site is able to handle it with a 302 redirect after the initial one from the shortener. --- tildes/tildes/views/shortener.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tildes/tildes/views/shortener.py b/tildes/tildes/views/shortener.py index c6c8bd8..3f7e312 100644 --- a/tildes/tildes/views/shortener.py +++ b/tildes/tildes/views/shortener.py @@ -3,7 +3,8 @@ """Views related to the link shortener.""" -from pyramid.httpexceptions import HTTPFound +from mypy_extensions import NoReturn +from pyramid.httpexceptions import HTTPMovedPermanently from pyramid.request import Request from pyramid.response import Response from pyramid.security import NO_PERMISSION_REQUIRED @@ -18,12 +19,14 @@ def get_shortener(request: Request) -> Response: @view_config(route_name="shortener_group", permission=NO_PERMISSION_REQUIRED) -def get_shortener_group(request: Request) -> HTTPFound: +def get_shortener_group(request: Request) -> NoReturn: """Redirect to the base path of a group.""" - raise HTTPFound(location=f"https://tildes.net/~{request.context.path}") + destination = f"https://tildes.net/~{request.context.path}" + raise HTTPMovedPermanently(location=destination) @view_config(route_name="shortener_topic", permission=NO_PERMISSION_REQUIRED) -def get_shortener_topic(request: Request) -> HTTPFound: +def get_shortener_topic(request: Request) -> NoReturn: """Redirect to the full permalink for a topic.""" - raise HTTPFound(location=f"https://tildes.net{request.context.permalink}") + destination = f"https://tildes.net{request.context.permalink}" + raise HTTPMovedPermanently(location=destination)