mirror of
https://github.com/dnlbauer/django-signposting.git
synced 2026-09-10 14:05:31 +00:00
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>
This commit is contained in:
@@ -21,4 +21,5 @@ from . import views
|
||||
urlpatterns = [
|
||||
path("", views.SimpleView.as_view()),
|
||||
path("jsonld", views.JsonLdView.as_view()),
|
||||
path("ro-crate", views.rocrate.as_view()),
|
||||
]
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
from django.http import HttpResponse
|
||||
from django.views import View
|
||||
from django.shortcuts import render
|
||||
|
||||
from django.views import View
|
||||
from django_json_ld.views import JsonLdContextMixin
|
||||
from django_signposting.utils import add_signposts
|
||||
from signposting import LinkRel, Signpost
|
||||
|
||||
from signposting import Signpost, LinkRel
|
||||
from django_signposting.utils import add_signposts, jsonld_to_signposts
|
||||
|
||||
|
||||
class SimpleView(View):
|
||||
|
||||
def get(self, request):
|
||||
response = HttpResponse("Hello, world!")
|
||||
|
||||
@@ -17,14 +15,13 @@ class SimpleView(View):
|
||||
add_signposts(
|
||||
response,
|
||||
Signpost(LinkRel.type, "http://schema.org/Dataset"),
|
||||
Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X")
|
||||
Signpost(LinkRel.author, "https://orcid.org/0000-0001-9447-460X"),
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class JsonLdView(JsonLdContextMixin, View):
|
||||
|
||||
sd = {
|
||||
"@context": "https://schema.org",
|
||||
"@type": ["WebSite", "Dataset"],
|
||||
@@ -35,12 +32,12 @@ class JsonLdView(JsonLdContextMixin, View):
|
||||
{
|
||||
"@type": "MediaObject",
|
||||
"contentUrl": "https://example.com/download.zip",
|
||||
"encodingFormat": "application/zip"
|
||||
"encodingFormat": "application/zip",
|
||||
},
|
||||
{
|
||||
"@type": "MediaObject",
|
||||
"contentUrl": "https://example.com/metadata.json",
|
||||
}
|
||||
},
|
||||
],
|
||||
"author": {
|
||||
"@type": "Person",
|
||||
@@ -50,21 +47,85 @@ class JsonLdView(JsonLdContextMixin, View):
|
||||
"license": {
|
||||
"@type": "CreativeWork",
|
||||
"name": "CC BY 4.0",
|
||||
"url": "https://creativecommons.org/licenses/by/4.0/"
|
||||
"url": "https://creativecommons.org/licenses/by/4.0/",
|
||||
},
|
||||
"hasPart": [
|
||||
{
|
||||
"@type": "ImageObject",
|
||||
"url": "http://example.com/image.png",
|
||||
"encodingFormat": "image/png"
|
||||
"encodingFormat": "image/png",
|
||||
},
|
||||
{
|
||||
"@type": "ImageObject",
|
||||
"url": "http://example.com/image2.png",
|
||||
"encodingFormat": "image/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
|
||||
|
||||
@@ -15,3 +15,4 @@ signposting==0.9.9
|
||||
soupsieve==2.6
|
||||
sqlparse==0.5.1
|
||||
urllib3==2.2.3
|
||||
rocrate==0.11.0
|
||||
|
||||
Reference in New Issue
Block a user