Easily serve 302
and 410
responses with simple YAML files.
This module contains Django and Flask helpers to serve "302 Found" redirect and "410 Gone" deleted responses from lists read from redirects.yaml
and deleted.yaml
files contained in the app directory.
redirects.yaml
hello: /world # A simple redirect
example-(?P<name>.*): http://example.com/{name} # A redirect with a regex replacement
deleted.yaml
deleted:
deleted/.*/regex: # This will match "/deleted/{anything}/regex"
deleted/with/message:
message: "Gone, gone, gone" # This context data is passed directly to the template
You can use this library with either Django or Flask.
Install the packege for Django as follows:
pip install canonicalwebteam.yaml_responses[django] # For Django helpers
Here's how to add URL patterns for redirects and deleted endpoints to Django:
This will read redirects.yaml
and deleted.yaml
pages in the project root.
For the "deleted" responses, it will look for a template called 410.html
in the default templates/
directory.
# urls.py
from django.conf.urls import url
from canonicalwebteam.yaml_responses.django import (
create_redirect_views,
create_deleted_views
)
urlpatterns = django.create_redirect_views() # Read redirects.yaml
urlpatterns += django.create_deleted_views() # Read deleted.yaml
urlpatterns += ... # The rest of your views
path
: The path to the YAML filepermanent
: Return "301 Moved Permanently" statuses instead of 302
E.g.:
urlpatterns = create_redirect_views(
path="config/permanent-redirects.yaml",
permanent=True
)
urlpatterns += ... # The rest of the views
path
: The path to the YAML fileview_callback
: An alternative function to process Deleted responses
E.g.:
def deleted_callback(request, url_mapping, settings, *args, **kwargs):
return render(request, "errors/410.html", url_mapping, status=410)
urlpatterns = create_deleted_views(
path="config/deleted-paths.yaml",
view_callback=deleted_callback
)
Install the package for Flask as follows:
pip install canonicalwebteam.yaml_responses[flask] # For Flask helpers
Here's how to process redirects and deleted before other views are processed in Flask:
This will read redirects.yaml
and deleted.yaml
pages in the project root.
For the "deleted" responses, it will look for a template called 410.html
in the default templates/
directory.
# app.py
from flask import Flask
from canonicalwebteam.yaml_responses.flask import (
prepare_deleted,
prepare_redirects,
)
app = Flask()
app.before_request(prepare_redirects()) # Read redirects.yaml
app.before_request(prepare_deleted()) # Read deleted.yaml
path
: The path to the YAML filepermanent
: Return "301 Moved Permanently" statuses instead of 302
E.g.:
app.before_request(
prepare_redirects(
path=f"{parent_dir}/redirects.yaml",
permanent=True
)
)
path
: The path to the YAML fileview_callback
: An alternative function to process Deleted responses
E.g.:
def deleted_callback(context):
return render_template("errors/410.html"), 410
app.before_request(
path=prepare_deleted(path="config/deleted-paths.yaml",
view_callback=deleted_callback)
)
This package has evolved from, and is intended to replace, the following projects: