2024-11-22 10:41:51 +01:00
2024-11-11 09:40:52 +01:00
2024-10-27 22:20:02 +01:00
2024-08-15 22:17:15 +02:00

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

The library can automatically add signposts to web pages that already make metadata available via JSON-LD in HTML via <script type="application/ld+json"> tags. 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 extracts supported properties from JSON-LD and adds the corresponding signposting headers to the HttpResponse.

Automatic parsing is compatible with extensions that provide JSON-LD as part of a web page, such as django-json-ld. It can also extract signposts from rich metadata descriptions of datasets such as RO-Crate format (see example views).

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

Manual parsing of JSON-LD

If you have metadata in JSON-LD available, but it is not rendered as part of the response, you can still parse it manually to create signposting links:

from django.http import HttpResponse
from django_signposting.utils import add_signposts, jsonld_to_signposts

response = HttpResponse("Hello World")
json_ld = {
    "@context": "http://schema.org/",
    "@graph": [
        ...
    ]
}
signposts = jsonld_to_signposts(json_ld)
add_signposts(response, **signposts)

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

Languages
Python 99.6%
HTML 0.4%