Skip to main content

Add FAIR signpostings to response headers in Django

Project description

Python package

FAIR signposting middleware for Django

django_signposting is a Django middleware library that facilitates the addition of FAIR signposting headers to HTTP responses. This middleware helps in making your data more FAIR (Findable, accessible, interoperable, reuseable) by embedding signposting headers in responses, guiding clients to relevant resources linked to the response content.

Based on the Signposting library.

Features

  • Automatically adds signposting headers to HTTP responses.
  • Signposts can be added manually or automatically be parsed from JSON-LD/schema.org
  • Supports multiple relation types with optional media type specification.
  • Easily integrable with existing Django applications.

Installation

pip install django-signposting

Usage

Automatic parsing of JSON-LD

To enable automatic parsing of JSON-LD, add the following middleware classes to your Django project's MIDDLEWARE setting in settings.py:

MIDDLEWARE = [
    ...,
    'django_signposting.middleware.SignpostingMiddleware',
    'django_signposting.middleware.JsonLdSignpostingParserMiddleware',
    ...,
]

This setup allows django_signposting to extract JSON-LD embedded in HTML <script type="application/ld+json"> tags and add the corresponding signposting headers. It’s compatible with tools that provide JSON-LD, such as django-json-ld.

Note: The middleware order is important! Place SignpostingMiddleware before JsonLdSignpostingParserMiddleware to ensure proper extraction and processing of JSON-LD content.

Manual signposting

For cases where JSON-LD is not embedded, or you want to specify headers manually, you can use the add_signposts utility.

  1. Add Middleware: Add the SignpostingMiddleware to your Django project's MIDDLEWARE setting in settings.py:
MIDDLEWARE = [
    ...,
    'django_signposting.middleware.SignpostingMiddleware',
    ...,
]
  1. Add Signposts to your Views: Use the add_signposts utility function:
from django.http import HttpResponse
from django_signposting.utils import add_signposts
from signposting import Signpost, LinkRel

def my_view(request):
    response = HttpResponse("Hello, world!")
    
    # Add signpostings as string
    add_signposts(
        response,
        Signpost(LinkRel.type, "https://schema.org/Dataset"),
        Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X")
        Signpost(LinkRel.item, "https://example.com/download.zip", "application/zip")
    )

    return response

Signposts are formatted and added as Link headers by the middleware

curl -I http://localhost:8000
HTTP/2 200 
...
link: <https://schema.org/Dataset> ; rel="type" ,
      <https://orcid.org/0000-0001-9447-460X> ; rel="author" ,
      <https://example.com/download.zip> ; rel="item" ; type="application/zip"

TODO

  • Option to add signposts in HTML via elements.
  • Add support for link sets
  • Add support for specifying profile extension attribute

License

Licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_signposting-0.10.2.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

django_signposting-0.10.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file django_signposting-0.10.2.tar.gz.

File metadata

  • Download URL: django_signposting-0.10.2.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for django_signposting-0.10.2.tar.gz
Algorithm Hash digest
SHA256 ad4e1c261daaffeefb4d71400ce070253d022099c4f26579bfeddaebafa102e7
MD5 ffd95410651947abeaf25178a2851ceb
BLAKE2b-256 0f017c49bb97079db4df13752cd0e227cc2a62bce850fab28a9e38e39ca8536e

See more details on using hashes here.

File details

Details for the file django_signposting-0.10.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_signposting-0.10.2-py3-none-any.whl
Algorithm Hash digest
SHA256 78afc89a989be0e48f5a2dd1be88119765321dad74cb411e867e622f93bda93d
MD5 e4d26d6bb0bd68532e85ab67cf49c7d5
BLAKE2b-256 005f67e4345b4797569ce1f45b85ca70d036bc1bc1a6ea4b9a1f9350d23306a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page