mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-11 06:25:30 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b1ea1403c | ||
|
|
f97c33e418 | ||
|
|
0f27560015 | ||
|
|
7b93eae884 | ||
|
|
0cd321112f | ||
|
|
bbf0527733 | ||
|
|
8b09f4feb5 | ||
|
|
01affafd7a | ||
|
|
76e791914d | ||
|
|
cbd621435c | ||
|
|
8c548d2392 |
3
.flake8
Normal file
3
.flake8
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[flake8]
|
||||||
|
max-line-length = 99
|
||||||
|
exclude = .git, __pycache__, .pytest_cache, .venv, venv, build
|
||||||
5
.github/workflows/python-package.yml
vendored
5
.github/workflows/python-package.yml
vendored
@@ -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
|
||||||
|
|||||||
3
.pre-commit-config.yaml
Normal file
3
.pre-commit-config.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
- repo: https://github.com/pycqa/flake8
|
||||||
|
hooks:
|
||||||
|
- id: flake8
|
||||||
46
README.md
46
README.md
@@ -1,6 +1,6 @@
|
|||||||
[](https://github.com/dnlbauer/django-signposting/actions/workflows/python-package.yml)
|
[](https://github.com/dnlbauer/django-signposting/actions/workflows/python-package.yml)
|
||||||
|
|
||||||
# FAIR signposting for Django
|
# 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.
|
||||||
@@ -11,20 +11,42 @@ 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.
|
||||||
|
It’s 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 = [
|
||||||
@@ -34,10 +56,7 @@ 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
|
||||||
@@ -58,10 +77,10 @@ def my_view(request):
|
|||||||
return response
|
return response
|
||||||
```
|
```
|
||||||
|
|
||||||
### 3. Signposts are formatted and added as Link headers by the middleware:
|
## 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" ,
|
||||||
@@ -69,6 +88,13 @@ link: <https://schema.org/Dataset> ; rel="type" ,
|
|||||||
<https://example.com/download.zip> ; rel="item" ; type="application/zip"
|
<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.
|
||||||
|
|||||||
@@ -2,9 +2,14 @@ from typing import Callable
|
|||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
from signposting import Signpost
|
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
|
||||||
|
|
||||||
@@ -17,13 +22,12 @@ 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, signposts: list[Signpost]):
|
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
|
||||||
signposts - a list of Signposts
|
signposts - a list of Signposts
|
||||||
@@ -37,3 +41,25 @@ class SignpostingMiddleware:
|
|||||||
|
|
||||||
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
|
||||||
|
|||||||
150
django_signposting/sparql.py
Normal file
150
django_signposting/sparql.py
Normal 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},
|
||||||
|
)
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
|
import json
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from signposting import Signpost
|
from rdflib import Graph
|
||||||
|
from signposting import Signpost, LinkRel
|
||||||
|
import re
|
||||||
|
|
||||||
|
from django_signposting import sparql
|
||||||
|
|
||||||
|
|
||||||
def add_signposts(response: HttpResponse, *args: Signpost):
|
def add_signposts(response: HttpResponse, *args: Signpost):
|
||||||
@@ -13,5 +18,69 @@ def add_signposts(response: HttpResponse, *args: Signpost):
|
|||||||
response._signposts = []
|
response._signposts = []
|
||||||
|
|
||||||
for signpost in args:
|
for signpost in args:
|
||||||
if not signpost in response._signposts:
|
if signpost not in response._signposts:
|
||||||
response._signposts.append(signpost)
|
response._signposts.append(signpost)
|
||||||
|
|
||||||
|
|
||||||
|
def select_url(elements: tuple[str, ...]) -> str | None:
|
||||||
|
def is_url(url: str) -> bool:
|
||||||
|
url_pattern = re.compile(
|
||||||
|
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
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.sessions',
|
'django.contrib.sessions',
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
'django_json_ld',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@@ -48,6 +49,7 @@ MIDDLEWARE = [
|
|||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'django_signposting.middleware.SignpostingMiddleware',
|
'django_signposting.middleware.SignpostingMiddleware',
|
||||||
|
'django_signposting.middleware.JsonLdSignpostingParserMiddleware',
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'example.urls'
|
ROOT_URLCONF = 'example.urls'
|
||||||
@@ -55,7 +57,9 @@ ROOT_URLCONF = 'example.urls'
|
|||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [
|
||||||
|
BASE_DIR / 'example/templates',
|
||||||
|
],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
|||||||
11
example/example/templates/jsonld.html
Normal file
11
example/example/templates/jsonld.html
Normal 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>
|
||||||
|
|
||||||
@@ -14,11 +14,12 @@ Including another URLconf
|
|||||||
1. Import the include() function: from django.urls import include, path
|
1. Import the include() function: from django.urls import include, path
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.my_view),
|
path("", views.SimpleView.as_view()),
|
||||||
|
path("jsonld", views.JsonLdView.as_view()),
|
||||||
|
path("ro-crate", views.rocrate.as_view()),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,12 +1,131 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django_signposting.utils import add_signposts
|
from django.shortcuts import render
|
||||||
|
from django.views import View
|
||||||
|
from django_json_ld.views import JsonLdContextMixin
|
||||||
|
from signposting import LinkRel, Signpost
|
||||||
|
|
||||||
def my_view(request):
|
from django_signposting.utils import add_signposts, jsonld_to_signposts
|
||||||
|
|
||||||
|
|
||||||
|
class SimpleView(View):
|
||||||
|
def get(self, 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, "http://schema.org/Dataset"),
|
||||||
|
Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X"),
|
||||||
|
)
|
||||||
|
|
||||||
return response
|
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
|
||||||
|
|||||||
18
example/requirements.txt
Normal file
18
example/requirements.txt
Normal 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
|
||||||
@@ -8,12 +8,14 @@ authors = [
|
|||||||
{name = "Daniel Bauer", email = "github@dbauer.me"}
|
{name = "Daniel Bauer", email = "github@dbauer.me"}
|
||||||
]
|
]
|
||||||
description = "Add FAIR signpostings to response headers in Django"
|
description = "Add FAIR signpostings to response headers in Django"
|
||||||
version = "0.10.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",
|
"signposting>=0.9.9",
|
||||||
|
"rdflib>=7.1.1",
|
||||||
]
|
]
|
||||||
license = {file= "LICENSE"}
|
license = {file= "LICENSE"}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import pytest
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
270
tests/test_jsonld_signposting_middleware.py
Normal file
270
tests/test_jsonld_signposting_middleware.py
Normal 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"),
|
||||||
|
],
|
||||||
|
)
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from re import A
|
|
||||||
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
|
from signposting import LinkRel, Signpost
|
||||||
|
|
||||||
|
|
||||||
def test_middleware_no_signposting():
|
def test_middleware_no_signposting():
|
||||||
response = HttpResponse()
|
response = HttpResponse()
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
@@ -5,7 +5,7 @@ from signposting import Signpost, LinkRel
|
|||||||
|
|
||||||
def test_add_signpost():
|
def test_add_signpost():
|
||||||
response = HttpResponse()
|
response = HttpResponse()
|
||||||
add_signposts(response, Signpost(LinkRel.item,"http://example.com"))
|
add_signposts(response, Signpost(LinkRel.item, "http://example.com"))
|
||||||
|
|
||||||
assert len(response._signposts) == 1
|
assert len(response._signposts) == 1
|
||||||
|
|
||||||
@@ -18,7 +18,6 @@ def test_add_multiple_signposts():
|
|||||||
Signpost(LinkRel.author, "https://example3.com"),
|
Signpost(LinkRel.author, "https://example3.com"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
assert len(response._signposts) == 3
|
assert len(response._signposts) == 3
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user