-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from UUDigitalHumanitieslab/feature/collectio…
…n-records-api basic collection records endpoint
- Loading branch information
Showing
7 changed files
with
190 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import List | ||
from rdflib import Graph, RDF, IdentifiedNode | ||
from rdflib.term import Node | ||
|
||
from triplestore.utils import Triples | ||
|
||
def list_from_graph_collection(graph: Graph, list_node: IdentifiedNode) -> List[Node]: | ||
''' | ||
Extract a list of nodes from an RDF collection in a graph | ||
''' | ||
|
||
items = list(graph.objects(list_node, RDF.first)) | ||
rest_nodes = graph.objects(list_node, RDF.rest) | ||
for rest in rest_nodes: | ||
items += list_from_graph_collection(graph, rest) | ||
return items | ||
|
||
|
||
def list_to_graph_collection(items: List[Node], items_node: IdentifiedNode) -> Graph: | ||
''' | ||
Return a list of items as an RDF collection | ||
''' | ||
|
||
g = Graph() | ||
collection = g.collection(items_node) | ||
collection += items # indirectly modifies g | ||
return g | ||
|
||
|
||
def collection_triples(graph: Graph, list_node: IdentifiedNode) -> Triples: | ||
''' | ||
Select all triples that make up an RDF collection in a graph. | ||
This collects the chain of `rdf:first` / `rdf:rest` relations that make up the | ||
collection. It collects what is actually stored in the graph, rather than a | ||
normalised version, so this method should be used to select the current triples in | ||
a delete or update operation. | ||
''' | ||
|
||
triples = list(graph.triples((list_node, RDF.first, None))) | ||
triples += list(graph.triples((list_node, RDF.rest, None))) | ||
|
||
for rest in graph.objects(list_node, RDF.rest): | ||
triples += collection_triples(graph, rest) | ||
|
||
return triples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import re_path | ||
|
||
from . import api | ||
|
||
urlpatterns = [ | ||
re_path('collection-records/(?P<collection>.+)/', api.CollectionRecordsView.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters