Coder Social home page Coder Social logo

nottrobin / yaml-responses Goto Github PK

View Code? Open in Web Editor NEW

This project forked from canonical/canonicalwebteam.yaml-responses

0.0 1.0 0.0 18 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%

yaml-responses's Introduction

canonicalwebteam.yaml-responses

CircleCI build status 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 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

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 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:

yaml-responses's People

Contributors

nottrobin avatar

Watchers

James Cloos avatar

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.