mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-10 22:15:30 +00:00
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>
This commit is contained in:
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'django_json_ld',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -48,6 +49,7 @@ MIDDLEWARE = [
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
'django_signposting.middleware.SignpostingMiddleware',
|
||||
'django_signposting.middleware.JsonLdSignpostingParserMiddleware',
|
||||
]
|
||||
|
||||
ROOT_URLCONF = 'example.urls'
|
||||
@@ -55,7 +57,9 @@ ROOT_URLCONF = 'example.urls'
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'DIRS': [
|
||||
BASE_DIR / 'example/templates',
|
||||
],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'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>
|
||||
|
||||
@@ -19,5 +19,6 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.my_view),
|
||||
path("", views.SimpleView.as_view()),
|
||||
path("jsonld", views.JsonLdView.as_view()),
|
||||
]
|
||||
|
||||
@@ -1,17 +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
|
||||
|
||||
|
||||
def my_view(request):
|
||||
response = HttpResponse("Hello, world!")
|
||||
class SimpleView(View):
|
||||
|
||||
# Add signpostings as string
|
||||
add_signposts(
|
||||
response,
|
||||
Signpost(LinkRel.type, "http://schema.org/Dataset"),
|
||||
Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X")
|
||||
)
|
||||
def get(self, request):
|
||||
response = HttpResponse("Hello, world!")
|
||||
|
||||
return response
|
||||
# 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})
|
||||
|
||||
Reference in New Issue
Block a user