GithubHelp home page GithubHelp logo

canonical / canonicalwebteam.yaml-responses Goto Github PK

View Code? Open in Web Editor NEW
3.0 16.0 2.0 41 KB

Easily serve 302 and 410 responses with simple YAML files in Django or Flask

License: GNU Lesser General Public License v3.0

Python 100.00%
package web-and-design

canonicalwebteam.yaml-responses's Introduction

canonicalwebteam.yaml-responses

Tests Code coverage

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.

File format

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

Usage

You can use this library with either Django or Flask.

Django

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:

create_redirect_views and create_deleted_views basic usage

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_helpers import (
    create_redirect_views,
    create_deleted_views
)

urlpatterns = create_redirect_views()  # Read redirects.yaml
urlpatterns += create_deleted_views()  # Read deleted.yaml

urlpatterns += ...  # The rest of your views

Options for create_redirect_views

E.g.:

urlpatterns = create_redirect_views(
    path="config/permanent-redirects.yaml",
    permanent=True
)
urlpatterns += ...  # The rest of the views

Options for create_deleted_views

  • path: The path to the YAML file
  • view_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
)

Flask

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:

prepare_redirects and prepare_deleted basic usage

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_helpers import (
    prepare_deleted,
    prepare_redirects,
)

app = Flask()

app.before_request(prepare_redirects())  # Read redirects.yaml
app.before_request(prepare_deleted())  # Read deleted.yaml

Options for prepare_redirects

E.g.:

app.before_request(
    prepare_redirects(
        path=f"{parent_dir}/redirects.yaml",
        permanent=True
    )
)

Options for prepare_deleted

  • path: The path to the YAML file
  • view_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)
)

Notes

This package has evolved from, and is intended to replace, the following projects:

canonicalwebteam.yaml-responses's People

Contributors

jkfran avatar nottrobin avatar petesfrench avatar pmahnke avatar renovate-bot avatar steverydz avatar tbille avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

nottrobin jkfran

canonicalwebteam.yaml-responses's Issues

Append query parameters

If a URL comes in containing query parameters and is then redirected to a new URL that contains more query parameters, the URL will end up looking like ?some=thing?another=thing, which won't work. Let's do something more elegant. Probably just append all query parameters to each other properly.

Update to support newer versions of Django

As of django=3.0, the import
from django.conf.urls import url
has been deprecated and should be replaced with
django.urls import re_path

In addition, all usages of the url method should be replaced with re_path

Fix YAMLLoadWarning

Make changes to fix the following warning

YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/publish.yml
  • actions/checkout v2
  • pypa/gh-action-pypi-publish e777b33388fefa46ce597d8afa9c15a5357af36f
  • actions/checkout v2
  • codecov/codecov-action v1
.github/workflows/tests.yml
  • actions/checkout v2
  • actions/checkout v2
  • actions/checkout v2
  • codecov/codecov-action v1
  • actions/checkout v2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.