mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-10 22:15:30 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b93eae884 | ||
|
|
0cd321112f | ||
|
|
bbf0527733 | ||
|
|
8b09f4feb5 | ||
|
|
01affafd7a | ||
|
|
76e791914d | ||
|
|
cbd621435c | ||
|
|
8c548d2392 | ||
|
|
1bc2b60609 | ||
|
|
aafd404f90 | ||
|
|
2014a4f024 | ||
|
|
31fcf221f0 | ||
|
|
bf2604e303 | ||
|
|
2d3dafbacc | ||
|
|
076273d014 | ||
|
|
93738b046f | ||
|
|
a863272c7e | ||
|
|
bbd07d3fab | ||
|
|
695c167d32 | ||
|
|
0a31aa21ff | ||
|
|
aa5850b0bd |
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
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ dist
|
|||||||
.idea
|
.idea
|
||||||
build
|
build
|
||||||
__pycache__
|
__pycache__
|
||||||
|
*.sqlite3
|
||||||
|
|||||||
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
|
||||||
85
README.md
85
README.md
@@ -1,12 +1,17 @@
|
|||||||
# FAIR signposting for Django
|
[](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.
|
||||||
|
|
||||||
@@ -18,9 +23,30 @@ 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 = [
|
||||||
@@ -30,60 +56,43 @@ 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.
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Licensed under the MIT License.
|
Licensed under the MIT License.
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
from typing import Callable
|
from typing import Callable
|
||||||
from django.http import HttpRequest, HttpResponse
|
from django.http import HttpRequest, HttpResponse
|
||||||
|
from signposting import Signpost, LinkRel
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
import re
|
||||||
|
import json
|
||||||
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
|
from django.conf import settings
|
||||||
|
from rdflib import Graph
|
||||||
|
from . import sparql
|
||||||
|
|
||||||
|
|
||||||
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 +24,106 @@ 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 is_url(self, 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))
|
||||||
|
|
||||||
|
def select_url(self, elements: tuple[str, ...]) -> str | None:
|
||||||
|
for elem in elements[::-1]:
|
||||||
|
if self.is_url(str(elem)):
|
||||||
|
return str(elem)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _jsonld_to_signposts(self, jsonld: dict) -> 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 = self.select_url(author)
|
||||||
|
if author:
|
||||||
|
signposts.append(Signpost(LinkRel.author, author))
|
||||||
|
|
||||||
|
license = next(iter(sparql.license_query(g, rootElement)), [])
|
||||||
|
license = self.select_url(license)
|
||||||
|
if license:
|
||||||
|
signposts.append(Signpost(LinkRel.license, license))
|
||||||
|
|
||||||
|
citations = sparql.cite_query(g, rootElement)
|
||||||
|
for citation in citations:
|
||||||
|
citation = self.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 = self.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 = self.select_url(item[:-1])
|
||||||
|
if item:
|
||||||
|
signposts.append(Signpost(LinkRel.item, item, item_media_type))
|
||||||
|
return signposts
|
||||||
|
|
||||||
|
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 = self._jsonld_to_signposts(jsonld)
|
||||||
|
|
||||||
|
response._signposts = signposts
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print(e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
return response
|
||||||
|
|||||||
136
django_signposting/sparql.py
Normal file
136
django_signposting/sparql.py
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
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 { ?s ?p ?rootElement }
|
||||||
|
}
|
||||||
|
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,23 +1,17 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from signposting import Signpost
|
||||||
|
|
||||||
|
|
||||||
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:
|
||||||
values = kwargs[key]
|
response._signposts.append(signpost)
|
||||||
if isinstance(values, str) or isinstance(values, tuple):
|
|
||||||
values = [values]
|
|
||||||
|
|
||||||
if key not in response._signposts:
|
|
||||||
response._signposts[key] = values
|
|
||||||
else:
|
|
||||||
response._signposts[key] += values
|
|
||||||
|
|||||||
0
example/example/__init__.py
Normal file
0
example/example/__init__.py
Normal file
16
example/example/asgi.py
Normal file
16
example/example/asgi.py
Normal 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
128
example/example/settings.py
Normal 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'
|
||||||
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>
|
||||||
|
|
||||||
24
example/example/urls.py
Normal file
24
example/example/urls.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
"""
|
||||||
|
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()),
|
||||||
|
]
|
||||||
70
example/example/views.py
Normal file
70
example/example/views.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
from django.http import HttpResponse
|
||||||
|
from django.views import View
|
||||||
|
from django.shortcuts import render
|
||||||
|
|
||||||
|
from django_json_ld.views import JsonLdContextMixin
|
||||||
|
from django_signposting.utils import add_signposts
|
||||||
|
|
||||||
|
from signposting import Signpost, LinkRel
|
||||||
|
|
||||||
|
|
||||||
|
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})
|
||||||
16
example/example/wsgi.py
Normal file
16
example/example/wsgi.py
Normal 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
22
example/manage.py
Executable 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()
|
||||||
17
example/requirements.txt
Normal file
17
example/requirements.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
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
|
||||||
@@ -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.1"
|
||||||
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
0
tests/__init__.py
Normal 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()
|
||||||
|
|||||||
220
tests/test_jsonld_signposting_middleware.py
Normal file
220
tests/test_jsonld_signposting_middleware.py
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
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"),
|
||||||
|
],
|
||||||
|
)
|
||||||
@@ -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"'
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user