24 Commits
0.9 ... v0.10.2

Author SHA1 Message Date
Daniel Bauer
6b1ea1403c Create Signposting from RO-Crate (#2)
* improvate jsonld parsing to be compatible with ro-crate

* refactoring

* example with RO-Crate

* bump version

---------

Co-authored-by: Daniel Bauer <daniel.bauer@senckenberg.de>
2024-11-21 15:40:46 +01:00
Daniel Bauer
f97c33e418 update TODOs 2024-11-21 10:44:00 +01:00
Daniel Bauer
0f27560015 fix pip install example 2024-11-19 08:28:22 +01:00
Daniel Bauer
7b93eae884 Update pyproject.toml 2024-11-18 17:23:53 +01:00
Daniel Bauer
0cd321112f Extract signposts from jsonld (#1)
* use rdflib to extract signposts from jsonld

* use rdflib to extract signposts from jsonld

* linting

* unit tests for jsonld middleware

* update readme

* update flake pipeline

---------

Co-authored-by: Daniel Bauer <daniel.bauer@senckenberg.de>
2024-11-11 13:02:55 +01:00
Daniel Bauer
bbf0527733 more agressive lint checking in pipeline 2024-11-11 09:45:26 +01:00
Daniel Bauer
8b09f4feb5 linting 2024-11-11 09:40:52 +01:00
Daniel Bauer
01affafd7a requirements file for example 2024-11-08 17:47:09 +01:00
Daniel Bauer
76e791914d update example to use signposting.Signpost 2024-11-08 17:44:48 +01:00
Daniel Bauer
cbd621435c Update README.md 2024-11-01 13:39:08 +01:00
Daniel Bauer
8c548d2392 add todos to readme 2024-11-01 13:17:56 +01:00
Daniel Bauer
1bc2b60609 update readme 2024-11-01 13:12:17 +01:00
Daniel Bauer
aafd404f90 add license to pyproject.toml 2024-11-01 13:10:07 +01:00
Daniel Bauer
2014a4f024 update readme 2024-11-01 13:09:19 +01:00
Daniel Bauer
31fcf221f0 add type test 2024-11-01 13:06:22 +01:00
Daniel Bauer
bf2604e303 bump version 2024-11-01 13:03:33 +01:00
Daniel Bauer
2d3dafbacc use signposting package 2024-11-01 13:02:56 +01:00
Daniel Bauer
076273d014 fix tests cannot find main package 2024-11-01 12:50:21 +01:00
daniel
93738b046f move folder 2024-10-27 22:24:45 +01:00
daniel
a863272c7e add simple example 2024-10-27 22:21:00 +01:00
daniel
bbd07d3fab update gitignore 2024-10-27 22:20:02 +01:00
daniel
695c167d32 fix import in README.md 2024-10-27 22:17:03 +01:00
Daniel Bauer
0a31aa21ff remove debug statement 2024-08-20 16:07:16 +02:00
Daniel Bauer
aa5850b0bd Update README.md 2024-08-15 23:21:21 +02:00
23 changed files with 1007 additions and 116 deletions

3
.flake8 Normal file
View File

@@ -0,0 +1,3 @@
[flake8]
max-line-length = 99
exclude = .git, __pycache__, .pytest_cache, .venv, venv, build

View File

@@ -32,10 +32,7 @@ jobs:
python -m pip install -e '.[dev]' python -m pip install -e '.[dev]'
- name: Lint with flake8 - name: Lint with flake8
run: | run: |
# stop the build if there are Python syntax errors or undefined names flake8 . --count --max-line-length=127 --statistics
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest - name: Test with pytest
run: | run: |
pytest pytest

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ dist
.idea .idea
build build
__pycache__ __pycache__
*.sqlite3

3
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,3 @@
- repo: https://github.com/pycqa/flake8
hooks:
- id: flake8

View File

@@ -1,26 +1,52 @@
# FAIR signposting for Django [![Python package](https://github.com/dnlbauer/django-signposting/actions/workflows/python-package.yml/badge.svg)](https://github.com/dnlbauer/django-signposting/actions/workflows/python-package.yml)
# FAIR signposting middleware for Django
`django_signposting` is a Django middleware library that facilitates the addition of `django_signposting` is a Django middleware library that facilitates the addition of
FAIR signposting headers to HTTP responses. FAIR signposting headers to HTTP responses.
This middleware helps in making your data more FAIR (Findable, accessible, interoperable, reuseable) by 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. embedding signposting headers in responses, guiding clients to relevant resources linked to the response content.
Based on the [Signposting](https://github.com/stain/signposting) library.
## Features ## Features
- Automatically adds signposting headers to HTTP responses. - 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. - Supports multiple relation types with optional media type specification.
- Easily integrable with existing Django applications. - Easily integrable with existing Django applications.
## Installation ## Installation
```bash ```bash
pip install django_signposting pip install django-signposting
``` ```
## Usage ## Usage
### 1. Add Middleware ### Automatic parsing of JSON-LD
Add the middleware to your Django project's `MIDDLEWARE` setting in `settings.py`: To enable automatic parsing of JSON-LD, add the following middleware classes to your Django project's MIDDLEWARE setting in settings.py:
```python
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.
Its compatible with tools that provide JSON-LD, such as [django-json-ld](https://pypi.org/project/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`:
```python ```python
MIDDLEWARE = [ MIDDLEWARE = [
@@ -30,60 +56,45 @@ MIDDLEWARE = [
] ]
``` ```
### 2. Add Signposts to your Views 2. **Add Signposts to your Views:** Use the `add_signposts` utility function:
You can add signposting headers in your Django views using the provided `add_signposts` utility function.
Here's how you can use it:
```python ```python
from django.http import HttpResponse from django.http import HttpResponse
from django_signposting.util import add_signposts from django_signposting.utils import add_signposts
from signposting import Signpost, LinkRel
def my_view(request): def my_view(request):
response = HttpResponse("Hello, world!") response = HttpResponse("Hello, world!")
# Add signpostings as string # Add signpostings as string
add_signposts(response, add_signposts(
type="https://schema.org/Dataset", response,
author="https://orcid.org/0000-0001-9447-460X") 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 return response
``` ```
Multiple links with the same link type can be added as lists and the content type of a link ## Signposts are formatted and added as Link headers by the middleware
can be defined by using tuples:
```python
from django.http import HttpResponse
from django_signposting.util import add_signposts
def my_view(request):
response = HttpResponse("Hello, world!")
# Add signpostings as string
add_signposts(response,
type="https://schema.org/Dataset",
author="https://orcid.org/0000-0001-9447-460X",
item=[
("https://example.com/image.png", "image/png"),
("https://example.com/download.zip", "application/zip")
])
return response
```
### 3. Signposts are formatted and added as Link headers by the middleware:
```bash ```bash
curl -I https://example.com curl -I http://localhost:8000
HTTP/2 200 HTTP/2 200
... ...
link: <https://schema.org/Dataset> ; rel="type" , link: <https://schema.org/Dataset> ; rel="type" ,
<https://orcid.org/0000-0001-9447-460X> ; rel="author" , <https://orcid.org/0000-0001-9447-460X> ; rel="author" ,
<https://example.com/image.png> ; rel="item" ; type="application/json+ld" <https://example.com/download.zip> ; rel="item" ; type="application/zip"
``` ```
## TODO
- [ ] Option to add signposts in HTML via <link> elements.
- [ ] Add support for link sets
- [ ] Add support for specifying profile extension attribute
## License ## License
Licensed under the MIT License. Licensed under the MIT License.

View File

@@ -1,9 +1,15 @@
from typing import Callable from typing import Callable
from django.http import HttpRequest, HttpResponse from django.http import HttpRequest, HttpResponse
from signposting import Signpost
from bs4 import BeautifulSoup
import json
from django.utils.deprecation import MiddlewareMixin
from django.conf import settings
from .utils import jsonld_to_signposts
class SignpostingMiddleware: class SignpostingMiddleware:
def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]): def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]):
self.get_response = get_response self.get_response = get_response
@@ -16,25 +22,44 @@ class SignpostingMiddleware:
if not hasattr(response, "_signposts"): if not hasattr(response, "_signposts"):
return response return response
self._add_signposts(response, response._signposts) self._add_signposts(response, response._signposts)
return response return response
def _add_signposts(self, response: HttpResponse, typed_links: dict[str, list[str|tuple[str, str]]]): def _add_signposts(self, response: HttpResponse, signposts: list[Signpost]):
""" Adds signposting headers to the respones. """Adds signposting headers to the respones.
params: params:
response - the response object response - the response object
typed_links - a map of relation types to a list of corresponding links. Each link can be a link or a tuple of link and media type. signposts - a list of Signposts
""" """
link_snippets = [] link_snippets = []
for relation_type in typed_links.keys(): for signpost in signposts:
links = typed_links.get(relation_type, []) link_snippets.append(f'<{signpost.target}> ; rel="{signpost.rel}"')
for link in links: if signpost.type:
if isinstance(link, tuple) and len(link) > 1: link_snippets[-1] += f' ; type="{signpost.type}"'
link_snippets.append(f'<{link[0]}> ; rel="{relation_type}" ; type="{link[1]}"')
else:
link_snippets.append(f'<{link}> ; rel="{relation_type}"')
response["Link"] = " , ".join(link_snippets) response["Link"] = " , ".join(link_snippets)
class JsonLdSignpostingParserMiddleware(MiddlewareMixin):
def process_response(
self, request: HttpRequest, response: HttpResponse
) -> HttpResponse:
if not getattr(settings, "SIGNPOSTING_PARSE_JSONLD", True):
return response
if response.get("Content-Type", "").startswith("text/html"):
soup = BeautifulSoup(response.content, "html.parser")
for script in soup.find_all("script", type="application/ld+json"):
try:
jsonld = json.loads(script.string)
signposts = jsonld_to_signposts(jsonld)
response._signposts = signposts
except json.JSONDecodeError as e:
print(e)
continue
return response

View File

@@ -0,0 +1,150 @@
from rdflib import Graph
from rdflib.query import Result
def root_element_query(g: Graph) -> Result:
return g.query("""
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?rootElement
WHERE {
# has a type
?rootElement a ?type ;
# has a license or an author or a creator
(schema:license | schema:author | schema:creator ) ?value .
# No incoming edges for ?rootElement
FILTER NOT EXISTS {
?incoming ?p ?rootElement .
# also allow incoming edges that link entities BACK to the dataset
FILTER(?p != schema:isPartOf)
FILTER(?p != schema:mainEntityOfPage)
FILTER(?p != schema:recordedIn)
FILTER(?p != schema:exampleOfWork)
FILTER(?p != schema:includedInDataCatalogue)
FILTER(?p != schema:subjectOf)
FILTER(?p != schema:dataset)
# allow incoming edges from ro-crate-metadata.json
FILTER(CONTAINS(?incoming, "ro-crate-metadata.json")) .
}
}
LIMIT 1
""")
def type_query(g: Graph, rootElement: str) -> Graph:
return g.query(
"""
PREFIX schema: <?context>
SELECT DISTINCT ?type
WHERE {
?rootElement a ?type .
}
""",
initBindings={"rootElement": rootElement},
)
def author_query(g: Graph, rootElement: str):
return g.query(
"""
PREFIX schema: <http://schema.org/>
SELECT ?author_id ?identifier ?url
WHERE {
?rootElement schema:author ?author .
BIND(?author AS ?author_id) # Get the @id of the author
OPTIONAL { ?author schema:identifier ?identifier }
OPTIONAL { ?author schema:url ?url }
}
LIMIT 1
""",
initBindings={"rootElement": rootElement},
)
def license_query(g: Graph, rootElement: str):
return g.query(
"""
PREFIX schema: <http://schema.org/>
SELECT ?element_id ?identifier ?url
WHERE {
?rootElement schema:license ?element .
BIND(?element AS ?element_id) # Get the @id of the element
OPTIONAL { ?element schema:identifier ?identifier }
OPTIONAL { ?element schema:url ?url }
}
LIMIT 1
""",
initBindings={"rootElement": rootElement},
)
def cite_query(g: Graph, rootElement: str):
return g.query(
"""
PREFIX schema: <http://schema.org/>
SELECT ?element_id ?identifier ?url
WHERE {
?rootElement schema:url ?element .
BIND(?element AS ?element_id) # Get the @id of the element
OPTIONAL { ?element schema:identifier ?identifier }
OPTIONAL { ?element schema:url ?url }
}
LIMIT 1
""",
initBindings={"rootElement": rootElement},
)
def sameas_query(g: Graph, rootElement: str):
return g.query(
"""
PREFIX schema: <http://schema.org/>
SELECT ?element_id ?contentUrl ?identifier ?url ?encoding
WHERE {
?rootElement schema:sameAs ?element .
BIND(?element AS ?element_id) # Get the @id of the element
OPTIONAL { ?element schema:contentUrl ?contentUrl }
OPTIONAL { ?element schema:identifier ?identifier }
OPTIONAL { ?element schema:url ?url }
OPTIONAL { ?element schema:encodingFormat ?encoding }
}
""",
initBindings={"rootElement": rootElement},
)
def item_query(g: Graph, rootElement: str):
return g.query(
"""
PREFIX schema: <http://schema.org/>
SELECT ?element_id ?identifier ?url ?contentUrl ?encoding
WHERE {
?rootElement schema:hasPart ?element .
#VALUES ?element_type { schema:MediaObject schema:Dataset }
#?element a ?element_type .
BIND(?element AS ?element_id) # Get the @id of the element
OPTIONAL { ?element schema:contentUrl ?contentUrl }
OPTIONAL { ?element schema:identifier ?identifier }
OPTIONAL { ?element schema:url ?url }
OPTIONAL { ?element schema:encodingFormat ?encoding }
}
""",
initBindings={"rootElement": rootElement},
)

View File

@@ -1,23 +1,86 @@
import json
from django.http import HttpResponse from django.http import HttpResponse
from rdflib import Graph
from signposting import Signpost, LinkRel
import re
from django_signposting import sparql
def add_signposts(response: HttpResponse, **kwargs): def add_signposts(response: HttpResponse, *args: Signpost):
""" Adds signposting headers to the responses. """ Adds signposting headers to the responses.
params: params:
response - the response object response - the response object
kwargs - a map of relation types to a list of corresponding links. Each link can be a link or a tuple of link and media type. args - a list of signposts to add to this resposnse.
""" """
if not hasattr(response, '_signposts'): if not hasattr(response, '_signposts'):
response._signposts = {} response._signposts = []
for key in kwargs.keys(): for signpost in args:
if signpost not in response._signposts:
response._signposts.append(signpost)
values = kwargs[key]
if isinstance(values, str) or isinstance(values, tuple):
values = [values]
if key not in response._signposts: def select_url(elements: tuple[str, ...]) -> str | None:
response._signposts[key] = values def is_url(url: str) -> bool:
else: url_pattern = re.compile(
response._signposts[key] += values r"^(https?|ftp)://" # protocol
r"(?:(?:[a-zA-Z0-9-_]+\.)?[a-zA-Z0-9-]+\.[a-zA-Z]{2,6})" # domain
r"(?::\d{1,5})?" # optional port
r"(?:/.*)?$" # path
)
return bool(url_pattern.match(url))
for elem in elements[::-1]:
if is_url(str(elem)):
return str(elem)
return None
def jsonld_to_signposts(jsonld: dict) -> []:
signposts = []
# TODO use jsonld context in query as prefix
g = Graph().parse(data=json.dumps(jsonld), format="json-ld")
rootElement = next(iter(sparql.root_element_query(g)), None)
if rootElement:
rootElement = rootElement[0]
else:
print("No root element found")
return {}
types = sparql.type_query(g, rootElement)
for type in types:
signposts.append(Signpost(LinkRel.type, str(type[0])))
authors = sparql.author_query(g, rootElement)
for author in authors:
author = select_url(author)
if author:
signposts.append(Signpost(LinkRel.author, author))
license = next(iter(sparql.license_query(g, rootElement)), [])
license = select_url(license)
if license:
signposts.append(Signpost(LinkRel.license, license))
citations = sparql.cite_query(g, rootElement)
for citation in citations:
citation = select_url(citation)
if citation:
signposts.append(Signpost(LinkRel.cite_as, citation))
sameas = sparql.sameas_query(g, rootElement)
for sa in sameas:
sa_media_type = sa[-1]
sa = select_url(sa[:-1])
if sa:
signposts.append(Signpost(LinkRel.describedby, sa, sa_media_type))
items = sparql.item_query(g, rootElement)
for item in items:
item_media_type = item[-1]
item = select_url(item[:-1])
if item:
signposts.append(Signpost(LinkRel.item, item, item_media_type))
return signposts

View File

16
example/example/asgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
ASGI config for example project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
application = get_asgi_application()

128
example/example/settings.py Normal file
View File

@@ -0,0 +1,128 @@
"""
Django settings for example project.
Generated by 'django-admin startproject' using Django 5.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-0_!deorp-j4ng0d$4#jrwcbw#@#)a5qf)_wgxq%or7ic4v3euc'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_json_ld',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_signposting.middleware.SignpostingMiddleware',
'django_signposting.middleware.JsonLdSignpostingParserMiddleware',
]
ROOT_URLCONF = 'example.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'example/templates',
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'example.wsgi.application'
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
{% load render_json_ld from json_ld %}
{% render_json_ld sd %}
</head>
<body>
<p>Hello, world!</p>
</body>
</html>

25
example/example/urls.py Normal file
View File

@@ -0,0 +1,25 @@
"""
URL configuration for simple project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.urls import path
from . import views
urlpatterns = [
path("", views.SimpleView.as_view()),
path("jsonld", views.JsonLdView.as_view()),
path("ro-crate", views.rocrate.as_view()),
]

131
example/example/views.py Normal file
View File

@@ -0,0 +1,131 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
from django_json_ld.views import JsonLdContextMixin
from signposting import LinkRel, Signpost
from django_signposting.utils import add_signposts, jsonld_to_signposts
class SimpleView(View):
def get(self, request):
response = HttpResponse("Hello, world!")
# Add signpostings as string
add_signposts(
response,
Signpost(LinkRel.type, "http://schema.org/Dataset"),
Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X"),
)
return response
class JsonLdView(JsonLdContextMixin, View):
sd = {
"@context": "https://schema.org",
"@type": ["WebSite", "Dataset"],
"name": "My Dataset",
"description": "A dataset of things.",
"url": "https://example.com",
"sameAs": [
{
"@type": "MediaObject",
"contentUrl": "https://example.com/download.zip",
"encodingFormat": "application/zip",
},
{
"@type": "MediaObject",
"contentUrl": "https://example.com/metadata.json",
},
],
"author": {
"@type": "Person",
"name": "Daniel Bauer",
"url": "https://orcid.org/0000-0001-9447-460X",
},
"license": {
"@type": "CreativeWork",
"name": "CC BY 4.0",
"url": "https://creativecommons.org/licenses/by/4.0/",
},
"hasPart": [
{
"@type": "ImageObject",
"url": "http://example.com/image.png",
"encodingFormat": "image/png",
},
{
"@type": "ImageObject",
"url": "http://example.com/image2.png",
"encodingFormat": "image/png",
},
],
}
def get(self, request):
return render(request, "jsonld.html", context={"sd": self.sd})
class rocrate(JsonLdContextMixin, View):
def get(self, request):
import json
import os
import tempfile
# Build an RO-Crate and serve its preview
from rocrate.model.person import Person
from rocrate.model.creativework import CreativeWork
from rocrate.rocrate import ROCrate
with tempfile.TemporaryDirectory() as d:
crate = ROCrate(gen_preview=True)
crate.add_file(
"http://example.com/test.pdf",
properties={"name": "test file", "encodingFormat": "application/pdf"},
)
author = crate.add(
Person(
crate,
"https://orcid.org/0000-0001-9447-460X",
properties={"name": "Daniel Bauer"},
)
)
license = crate.add(
CreativeWork(
crate,
"https://spdx.org/licenses/CC0-1.0",
properties={
"@id": "https://spdx.org/licenses/CC0-1.0",
"@type": "CreativeWork",
"name": "CC0-1.0",
"description": "Creative Commons Zero v1.0 Universal",
},
)
)
sameAs = crate.add(
CreativeWork(
crate,
"https://example.com/ro-crate-metadata.json",
properties={"encodingFormat": "application/ld+json"},
)
)
crate.root_dataset["name"] = "My Dataset"
crate.root_dataset["description"] = "A dataset of things."
crate.root_dataset["author"] = author
crate.root_dataset["creator"] = author
crate.root_dataset["license"] = license
crate.root_dataset["url"] = "https://example.com"
crate.root_dataset["sameAs"] = sameAs
crate.write(d)
# Extract JSON-LD from RO-Crate metadata and build signposts from it
metadata = open(os.path.join(d, "ro-crate-metadata.json"), "r").read()
metadata = json.loads(metadata)
signposts = jsonld_to_signposts(metadata)
preview = open(os.path.join(d, "ro-crate-preview.html"), "r").read()
response = HttpResponse(preview)
add_signposts(response, *signposts)
return response

16
example/example/wsgi.py Normal file
View File

@@ -0,0 +1,16 @@
"""
WSGI config for example project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
application = get_wsgi_application()

22
example/manage.py Executable file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'example.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()

18
example/requirements.txt Normal file
View File

@@ -0,0 +1,18 @@
asgiref==3.8.1
beautifulsoup4==4.12.3
certifi==2024.8.30
charset-normalizer==3.4.0
django==5.1.3
django-json-ld==0.0.5
django-signposting==0.10.0
httplink==0.2.0
idna==3.10
pyparsing==3.2.0
rdflib==7.1.1
requests==2.32.3
rfc3987==1.3.8
signposting==0.9.9
soupsieve==2.6
sqlparse==0.5.1
urllib3==2.2.3
rocrate==0.11.0

View File

@@ -7,13 +7,17 @@ name = "django_signposting"
authors = [ authors = [
{name = "Daniel Bauer", email = "github@dbauer.me"} {name = "Daniel Bauer", email = "github@dbauer.me"}
] ]
description = "Add FAIR signpostings to response headers" description = "Add FAIR signpostings to response headers in Django"
version = "0.9.0" version = "0.10.2"
readme = "README.md" readme = "README.md"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
"Django>=3.0", "beautifulsoup4>=4.12.3",
"django>=3.0",
"signposting>=0.9.9",
"rdflib>=7.1.1",
] ]
license = {file= "LICENSE"}
[project.urls] [project.urls]
Homepage = "https://github.com/dnlbauer/django-signposting" Homepage = "https://github.com/dnlbauer/django-signposting"

0
tests/__init__.py Normal file
View File

View File

@@ -1,7 +1,5 @@
import pytest
from django.conf import settings from django.conf import settings
def pytest_sessionstart(session): def pytest_sessionstart(session):
print("test")
settings.configure() settings.configure()

View File

@@ -0,0 +1,270 @@
import json
from django.http import HttpRequest, HttpResponse
from django_signposting.middleware import JsonLdSignpostingParserMiddleware
from signposting import Signpost, LinkRel
def jsonld_test_runner(jsonld, expected_signposts):
response = HttpResponse(f"""
<html><head>
<script type="application/ld+json">{json.dumps(jsonld)}</script>
</head>
<body></body>
</html>
""")
response.status_code = 200
middleware = JsonLdSignpostingParserMiddleware(lambda request: response)
response = middleware(HttpRequest())
for expected_signpost in expected_signposts:
assert expected_signpost in response._signposts
assert len(expected_signposts) == len(response._signposts)
def test_jsonld_signposting_basic():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@type": "WebPage",
"author": {"url": "http://example.com/author"},
"url": "http://example.com/url",
"license": {"identifier": "http://example.com/license"},
"hasPart": [
{
"url": "http://example.com/image1.jpg",
},
{
"url": "http://example.com/image2.jpg",
},
],
"sameAs": [
{
"url": "http://example.com/metadata.json",
},
{
"url": "http://example.com/metadata.xml",
},
],
},
[
Signpost(LinkRel.type, "http://schema.org/WebPage"),
Signpost(LinkRel.author, "http://example.com/author"),
Signpost(LinkRel.cite_as, "http://example.com/url"),
Signpost(LinkRel.license, "http://example.com/license"),
Signpost(LinkRel.item, "http://example.com/image1.jpg"),
Signpost(LinkRel.item, "http://example.com/image2.jpg"),
Signpost(LinkRel.describedby, "http://example.com/metadata.json"),
Signpost(LinkRel.describedby, "http://example.com/metadata.xml"),
],
)
def test_jsonld_empty_signposting():
jsonld_test_runner({}, [])
def test_jsonld_signposting_media_types():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@type": "WebPage",
"author": {"url": "http://example.com/author"},
"url": "http://example.com/url",
"license": {"identifier": "http://example.com/license"},
"hasPart": [
{
"url": "http://example.com/image1.jpg",
"encodingFormat": "image/jpeg",
},
{"url": "http://example.com/image2.jpg", "encodingFormat": "image/png"},
],
"sameAs": [
{
"url": "http://example.com/metadata.json",
"encodingFormat": "application/json",
},
{
"url": "http://example.com/metadata.xml",
"encodingFormat": "application/xml",
},
],
},
[
Signpost(LinkRel.type, "http://schema.org/WebPage"),
Signpost(LinkRel.author, "http://example.com/author"),
Signpost(LinkRel.cite_as, "http://example.com/url"),
Signpost(LinkRel.license, "http://example.com/license"),
Signpost(LinkRel.item, "http://example.com/image1.jpg", "image/jpeg"),
Signpost(LinkRel.item, "http://example.com/image2.jpg", "image/png"),
Signpost(
LinkRel.describedby,
"http://example.com/metadata.json",
"application/json",
),
Signpost(
LinkRel.describedby,
"http://example.com/metadata.xml",
"application/xml",
),
],
)
def test_jsonld_signposting_property_precedence():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@type": "WebPage",
"author": {
"identifier": "http://example.com/author-ident",
"url": "http://example.com/author-url",
},
"hasPart": [
{
"identifier": "http://example.com/image1-identifier.jpg",
"url": "http://example.com/image1-url.jpg",
"contentUrl": "http://example.com/image1-content.jpg",
},
{
"identifier": "http://example.com/image2-identifier.jpg",
"url": "http://example.com/image2-url.jpg",
},
],
},
[
Signpost(LinkRel.type, "http://schema.org/WebPage"),
Signpost(LinkRel.author, "http://example.com/author-url"),
Signpost(LinkRel.item, "http://example.com/image1-content.jpg"),
Signpost(LinkRel.item, "http://example.com/image2-url.jpg"),
],
)
def test_jsonld_signposting_ids():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@graph": [
{
"@type": "WebPage",
"author": {"@id": "http://example.com/author"},
"url": "http://example.com/url",
"license": {"@id": "http://example.com/license"},
"hasPart": [
{
"@id": "http://example.com/image1.jpg",
},
],
"sameAs": [
{
"@id": "http://example.com/metadata.json",
},
],
},
{"@id": "http://example.com/author", "@type": "Person"},
{"@id": "http://example.com/license"},
{
"@id": "http://example.com/image1.jpg",
"encodingFormat": "image/jpeg",
},
{"@id": "http://example.com/metadata.json"},
],
},
[
Signpost(LinkRel.type, "http://schema.org/WebPage"),
Signpost(LinkRel.author, "http://example.com/author"),
Signpost(LinkRel.cite_as, "http://example.com/url"),
Signpost(LinkRel.license, "http://example.com/license"),
Signpost(LinkRel.item, "http://example.com/image1.jpg", "image/jpeg"),
Signpost(LinkRel.describedby, "http://example.com/metadata.json"),
],
)
def test_jsonld_signposting_ids_with_url():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@graph": [
{
"@type": "WebPage",
"author": {"@id": "http://example.com/author"},
},
{
"@id": "http://example.com/author",
"url": "http://example.com/realAuthorUrl",
},
],
},
[
Signpost(LinkRel.type, "http://schema.org/WebPage"),
Signpost(LinkRel.author, "http://example.com/realAuthorUrl"),
],
)
def test_jsonld_signposting_multiple_types():
jsonld_test_runner(
{
"@context": "http://schema.org",
"@type": "Dataset",
"author": "http://example.com/author",
},
[
Signpost(LinkRel.type, "http://schema.org/Dataset"),
Signpost(LinkRel.author, "http://example.com/author"),
],
)
def test_jsonld_signposting_rocrate():
jsonld_test_runner(
{
"@context": "http://w3id.org/ro/crate/1.1/context",
"@graph": [
{
"@type": "CreativeWork",
"@id": "ro-crate-metadata.json",
"conformsTo": {"@id": "http://w3id.org/ro/crate/1.1"},
"about": {"@id": "http://example.com/myCrate"}
},
{
"@type": "Dataset",
"@id": "http://example.com/myCrate",
"author": {"@id": "http://example.com/author"},
"datePublished": "2024",
"name": "Test crate",
"description": "This is a detached test create",
"license": {"@id": "http://example.com/license"},
"hasPart": [
{"@id": "http://example.com/file"}
]
},
{
"@id": "http://example.com/author",
"@type": "Person",
"name": "Daniel Bauer"
},
{
"@id": "http://example.com/license",
"@type": "CreativeWork",
"name": "Test License"
},
{
"@id": "http://example.com/file",
"@type": "File",
"contentUrl": "http://example.com/image1.jpg",
"encodingFormat": "image/jpeg"
}
]
},
[
Signpost(LinkRel.type, "http://schema.org/Dataset"),
Signpost(LinkRel.author, "http://example.com/author"),
Signpost(LinkRel.license, "http://example.com/license"),
Signpost(LinkRel.item, "http://example.com/image1.jpg", "image/jpeg"),
],
)

View File

@@ -1,5 +1,6 @@
from django.http import HttpResponse from django.http import HttpResponse
from django_signposting.middleware import SignpostingMiddleware from django_signposting.middleware import SignpostingMiddleware
from signposting import LinkRel, Signpost
def test_middleware_no_signposting(): def test_middleware_no_signposting():
@@ -14,7 +15,9 @@ def test_middleware_no_signposting():
def test_middleware_signposting(): def test_middleware_signposting():
response = HttpResponse() response = HttpResponse()
response.status_code = 200 response.status_code = 200
response._signposts = {"author": ["http://example.com"]} response._signposts = [
Signpost(LinkRel.author, "http://example.com")
]
middleware = SignpostingMiddleware(lambda request: response) middleware = SignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
@@ -24,15 +27,11 @@ def test_middleware_signposting():
def test_middleware_multiple_signposts(): def test_middleware_multiple_signposts():
response = HttpResponse() response = HttpResponse()
response.status_code = 200 response.status_code = 200
response._signposts = { response._signposts = [
"author": [ Signpost(LinkRel.author, "http://example.com"),
"http://example.com", Signpost(LinkRel.author, "http://example2.com"),
"http://example2.com" Signpost(LinkRel.cite_as, "http://example3.com"),
], ]
"cite-as": [
"http://example3.com"
]
}
middleware = SignpostingMiddleware(lambda request: response) middleware = SignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
@@ -45,24 +44,34 @@ def test_middleware_multiple_signposts():
def test_middleware_signpost_with_content_type(): def test_middleware_signpost_with_content_type():
response = HttpResponse() response = HttpResponse()
response.status_code = 200 response.status_code = 200
response._signposts = { response._signposts = [
"item": [ Signpost(LinkRel.item, "http://example.com", "text/json")
("http://example.com", "test/json"), ]
]
}
middleware = SignpostingMiddleware(lambda request: response) middleware = SignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
assert response.headers["Link"] == '<http://example.com> ; rel="item" ; type="test/json"' assert response.headers["Link"] == '<http://example.com> ; rel="item" ; type="text/json"'
def test_middleware_ignore_error_responses(): def test_middleware_ignore_error_responses():
response = HttpResponse() response = HttpResponse()
response.status_code = 400 response.status_code = 400
response._signposts = { response._signposts = [
"author": ["https://example.com"] Signpost(LinkRel.author, "http://example.com")
} ]
middleware = SignpostingMiddleware(lambda request: response) middleware = SignpostingMiddleware(lambda request: response)
response = middleware(None) response = middleware(None)
assert "Link" not in response.headers assert "Link" not in response.headers
def test_middleware_type_link():
response = HttpResponse()
response.status_code = 200
response._signposts = [
Signpost(LinkRel.type, "http://schema.org/Dataset")
]
middleware = SignpostingMiddleware(lambda request: response)
response = middleware(None)
assert response.headers["Link"] == '<http://schema.org/Dataset> ; rel="type"'

View File

@@ -1,47 +1,37 @@
from django.http import HttpResponse from django.http import HttpResponse
from django_signposting.utils import add_signposts from django_signposting.utils import add_signposts
from signposting import Signpost, LinkRel
def test_add_signpost(): def test_add_signpost():
response = HttpResponse() response = HttpResponse()
add_signposts(response, item="http://example.com") add_signposts(response, Signpost(LinkRel.item, "http://example.com"))
assert response._signposts["item"] == ["http://example.com"] assert len(response._signposts) == 1
def test_add_multiple_signposts(): def test_add_multiple_signposts():
response = HttpResponse() response = HttpResponse()
add_signposts(response, add_signposts(response,
item="http://example.com", Signpost(LinkRel.item, "http://example.com"),
author=["https://example2.com", "https://example3.com"] Signpost(LinkRel.author, "https://example2.com"),
Signpost(LinkRel.author, "https://example3.com"),
) )
assert response._signposts == { assert len(response._signposts) == 3
"item": ["http://example.com"],
"author": [
"https://example2.com",
"https://example3.com",
]
}
def test_add_signposts_with_content_type(): def test_add_signpost_call_multiple_times():
response = HttpResponse() response = HttpResponse()
add_signposts(response, add_signposts(response, Signpost(LinkRel.item, "http://example.com"))
item=("http://example.com", "text/json"), add_signposts(response, Signpost(LinkRel.item, "http://example2.com"))
author=["https://example2.com", "https://example3.com"]
)
assert response._signposts == { assert len(response._signposts) == 2
"item": [("http://example.com", "text/json")],
"author": [
"https://example2.com",
"https://example3.com",
]
}
def test_add_signposts_from_dict(): def test_add_signpost_duplicate():
response = HttpResponse() response = HttpResponse()
add_signposts(response, **{"cite-as": ["https://example.com"]}) add_signposts(response, Signpost(LinkRel.item, "http://example.com"))
assert response._signposts["cite-as"] == ["https://example.com"] add_signposts(response, Signpost(LinkRel.item, "http://example.com"))
assert len(response._signposts) == 1