diff --git a/dapitains/app/app.py b/dapitains/app/app.py index 8b1baf4..0676993 100644 --- a/dapitains/app/app.py +++ b/dapitains/app/app.py @@ -51,6 +51,24 @@ def collection_view( else: return msg_4xx(f"nav parameter has a wrong value {nav}", code=400) + def inject_json(related: Collection) -> Dict: + if related.resource: + inj = { + "collection": templates["collection"].partial({"id": related.identifier}).uri, + "document": templates["document"].partial({"resource": related.identifier}).uri, + } + if related.citeStructure: + inj["navigation"] = templates["navigation"].partial({"resource": related.identifier}).uri + else: + inj ={"collection": templates["collection"].partial({"id": related.identifier}).uri} + + inj.update({ + "totalParents": coll.total_parents, + "totalChildren": coll.total_children + }) + + return inj + return Response(json.dumps({ "@context": "https://distributed-text-services.github.io/specifications/context/1-alpha1.json", "dtsVersion": "1-alpha", @@ -60,23 +78,11 @@ def collection_view( "collection": templates["collection"].uri, "member": [ related.json( - inject=dict( - **{ - "collection": templates["collection"].partial({"id": related.identifier}).uri, - "document": templates["document"].partial({"resource": related.identifier}).uri, - }, - **( - { - "navigation": templates["navigation"].partial({"resource": related.identifier}).uri, - } if related.citeStructure else {} - ) - ) if related.resource else related.json({ - "collection": templates["collection"].partial({"id": related.identifier}).uri - }) + inject=inject_json(related) ) for related in related_collections ] - }), mimetype="application/json", status=200) + }), mimetype="application/ld+json", status=200) def document_view(resource, ref, start, end, tree) -> Response: @@ -148,7 +154,7 @@ def navigation_view(resource, ref, start, end, tree, down, templates: Dict[str, return msg_4xx(f"Range is missing one of its parameters (start or end)", code=400) # Start the response - out = { + out = { "@context": "https://distributed-text-services.github.io/specifications/context/1-alpha1.json", "dtsVersion": "1-alpha", "@type": "Navigation", @@ -190,7 +196,7 @@ def navigation_view(resource, ref, start, end, tree, down, templates: Dict[str, else: out["ref"] = start - return Response(json.dumps(out), mimetype="application/json", status=200) + return Response(json.dumps(out), mimetype="application/ld+json", status=200) def create_app( @@ -218,7 +224,7 @@ def index_route(): "navigation" : navigation_template.uri, "document": document_template.uri }), - mimetype="application/json" + mimetype="application/ld+json" ) @app.route("/collection/") diff --git a/dapitains/app/database.py b/dapitains/app/database.py index d064938..0c8c52b 100644 --- a/dapitains/app/database.py +++ b/dapitains/app/database.py @@ -103,6 +103,8 @@ def json(self, inject: Optional[Dict[str, Any]] = None): if key != self.default_tree: data["citationTrees"].append(self.citeStructure[key]) self.citeStructure[key]["identifier"] = key + for tree in data["citationTrees"]: + tree["@type"] = "CitationTree" if self.dublin_core: # ToDo: Fix the way it's presented to adapt to dts view data["dublinCore"] = self.dublin_core if self.extensions: diff --git a/dapitains/app/navigation.py b/dapitains/app/navigation.py index adcc8a3..b0ad209 100644 --- a/dapitains/app/navigation.py +++ b/dapitains/app/navigation.py @@ -51,7 +51,7 @@ def generate_paths(data: List[Dict[str, Any]], path: Optional[List[int]] = None) def recurse(items, current_path): for index, item in enumerate(items): - ref = item.get('ref') + ref = item.get('identifier') if ref: # Record the path for the current reference paths[ref] = current_path + [index] diff --git a/dapitains/tei/citeStructure.py b/dapitains/tei/citeStructure.py index c82a964..2bed2ec 100644 --- a/dapitains/tei/citeStructure.py +++ b/dapitains/tei/citeStructure.py @@ -64,7 +64,7 @@ class CitableUnit: def json(self): out = { "citeType": self.citeType, - "ref": self.ref, + "identifier": self.ref, "level": self.level, "parent": self.parent } diff --git a/tests/test_citeStructure.py b/tests/test_citeStructure.py index e3ae623..489980c 100644 --- a/tests/test_citeStructure.py +++ b/tests/test_citeStructure.py @@ -59,20 +59,20 @@ def test_parsing(): assert parser.generate_xpath("Luke") == "//body/div[@n='Luke']" assert [root.json() for root in parser.find_refs(root=TEI, structure=parser.structure)] == [ - {'citeType': 'book', 'ref': 'Luke', 'parent': None, 'level': 1, 'members': [ - {'citeType': 'chapter', 'ref': 'Luke 1', 'parent': 'Luke', 'level': 2, 'members': [ - {'citeType': 'verse', 'ref': 'Luke 1:1', 'parent': 'Luke 1', 'level': 3}, - {'citeType': 'verse', 'ref': 'Luke 1:2', 'parent': 'Luke 1', 'level': 3}, - {'citeType': 'bloup', 'ref': 'Luke 1#1', 'parent': 'Luke 1', 'level': 3} + {'citeType': 'book', 'identifier': 'Luke', 'parent': None, 'level': 1, 'members': [ + {'citeType': 'chapter', 'identifier': 'Luke 1', 'parent': 'Luke', 'level': 2, 'members': [ + {'citeType': 'verse', 'identifier': 'Luke 1:1', 'parent': 'Luke 1', 'level': 3}, + {'citeType': 'verse', 'identifier': 'Luke 1:2', 'parent': 'Luke 1', 'level': 3}, + {'citeType': 'bloup', 'identifier': 'Luke 1#1', 'parent': 'Luke 1', 'level': 3} ] } ]}, - {'citeType': 'book', 'ref': 'Mark', 'parent': None, 'level': 1, 'members': [ - {'citeType': 'chapter', 'ref': 'Mark 1', 'parent': 'Mark', 'level': 2, 'members': [ - {'citeType': 'verse', 'ref': 'Mark 1:1', 'parent': 'Mark 1', 'level': 3}, - {'citeType': 'verse', 'ref': 'Mark 1:2', 'parent': 'Mark 1', 'level': 3}, - {'citeType': 'bloup', 'ref': 'Mark 1#1', 'parent': 'Mark 1', 'level': 3}, - {'citeType': 'verse', 'ref': 'Mark 1:3', 'parent': 'Mark 1', 'level': 3} + {'citeType': 'book', 'identifier': 'Mark', 'parent': None, 'level': 1, 'members': [ + {'citeType': 'chapter', 'identifier': 'Mark 1', 'parent': 'Mark', 'level': 2, 'members': [ + {'citeType': 'verse', 'identifier': 'Mark 1:1', 'parent': 'Mark 1', 'level': 3}, + {'citeType': 'verse', 'identifier': 'Mark 1:2', 'parent': 'Mark 1', 'level': 3}, + {'citeType': 'bloup', 'identifier': 'Mark 1#1', 'parent': 'Mark 1', 'level': 3}, + {'citeType': 'verse', 'identifier': 'Mark 1:3', 'parent': 'Mark 1', 'level': 3} ]} ]} ] @@ -85,17 +85,17 @@ def test_cite_data(): refs = parser.find_refs(root=TEI, structure=parser.structure) refs = [ref.json() for ref in refs] assert refs == [ - {'citeType': 'book', 'ref': '1', 'parent': None, 'level': 1, 'dublinCore': { + {'citeType': 'book', 'identifier': '1', 'parent': None, 'level': 1, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Introduction', 'Introduction'], 'http://purl.org/dc/terms/creator': ['John Doe']}}, - {'citeType': 'book', 'ref': '2', 'parent': None, 'level': 1, 'dublinCore': {'http://purl.org/dc/terms/title': ["Background", 'Contexte']}}, - {'citeType': 'book', 'ref': '3', 'parent': None, 'level': 1, 'dublinCore': { + {'citeType': 'book', 'identifier': '2', 'parent': None, 'level': 1, 'dublinCore': {'http://purl.org/dc/terms/title': ["Background", 'Contexte']}}, + {'citeType': 'book', 'identifier': '3', 'parent': None, 'level': 1, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Methodology', 'Méthodologie'], 'http://purl.org/dc/terms/creator': ['Albert Einstein']}}, - {'citeType': 'book', 'ref': '4', 'parent': None, 'level': 1, 'dublinCore': { + {'citeType': 'book', 'identifier': '4', 'parent': None, 'level': 1, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Results', 'Résultats'], 'http://purl.org/dc/terms/creator': ['Isaac Newton']}}, - {'citeType': 'book', 'ref': '5', 'parent': None, 'level': 1, 'dublinCore': { + {'citeType': 'book', 'identifier': '5', 'parent': None, 'level': 1, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Conclusion', 'Conclusion'], 'http://purl.org/dc/terms/creator': ['Marie Curie'] }}] @@ -109,24 +109,24 @@ def test_advanced_cite_data(): refs = parser.find_refs(root=TEI, structure=parser.structure) refs = [ref.json() for ref in refs] assert refs == [ - {'citeType': 'part', 'ref': 'part-1', 'parent': None, 'level': 1, 'members': [ - {'citeType': 'book', 'ref': 'part-1.1', 'parent': 'part-1', 'level': 2, 'dublinCore': { + {'citeType': 'part', 'identifier': 'part-1', 'parent': None, 'level': 1, 'members': [ + {'citeType': 'book', 'identifier': 'part-1.1', 'parent': 'part-1', 'level': 2, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Introduction', 'Introduction'], 'http://purl.org/dc/terms/creator': ['John Doe']}}, - {'citeType': 'book', 'ref': 'part-1.2', 'parent': 'part-1', 'level': 2, 'dublinCore': { + {'citeType': 'book', 'identifier': 'part-1.2', 'parent': 'part-1', 'level': 2, 'dublinCore': { 'http://purl.org/dc/terms/title': ["Background", 'Contexte'] }} ], 'extension': {"http://foo.bar/part": ["1"]}}, - {'citeType': 'part', 'ref': 'part-2', 'parent': None, 'level': 1, 'members': [ - {'citeType': 'book', 'ref': 'part-2.3', 'parent': 'part-2', 'level': 2, 'dublinCore': { + {'citeType': 'part', 'identifier': 'part-2', 'parent': None, 'level': 1, 'members': [ + {'citeType': 'book', 'identifier': 'part-2.3', 'parent': 'part-2', 'level': 2, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Methodology', 'Méthodologie'], 'http://purl.org/dc/terms/creator': ['Albert Einstein']}}, - {'citeType': 'book', 'ref': 'part-2.4', 'parent': 'part-2', 'level': 2, 'dublinCore': { + {'citeType': 'book', 'identifier': 'part-2.4', 'parent': 'part-2', 'level': 2, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Results', 'Résultats'], 'http://purl.org/dc/terms/creator': ['Isaac Newton']}} ], 'extension': {"http://foo.bar/part": ["2"]}}, - {'citeType': 'part', 'ref': 'part-3', 'parent': None, 'level': 1, 'members': [ - {'citeType': 'book', 'ref': 'part-3.5', 'parent': 'part-3', 'level': 2, 'dublinCore': { + {'citeType': 'part', 'identifier': 'part-3', 'parent': None, 'level': 1, 'members': [ + {'citeType': 'book', 'identifier': 'part-3.5', 'parent': 'part-3', 'level': 2, 'dublinCore': { 'http://purl.org/dc/terms/title': ['Conclusion', 'Conclusion'], 'http://purl.org/dc/terms/creator': ['Marie Curie'] }} diff --git a/tests/test_db_create.py b/tests/test_db_create.py index 66936a6..c4abb8b 100644 --- a/tests/test_db_create.py +++ b/tests/test_db_create.py @@ -47,10 +47,10 @@ def test_simple_path(): } assert strip_members( get_member_by_path(refs[doc.default_tree], paths[doc.default_tree]["Luke"]) - ) == {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, "Check that members are stripped" + ) == {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, "Check that members are stripped" assert get_member_by_path( refs[doc.default_tree], paths[doc.default_tree]["Mark 1:3"] - ) == {'citeType': 'verse', 'ref': 'Mark 1:3', "level": 3, "parent": "Mark 1"} + ) == {'citeType': 'verse', 'identifier': 'Mark 1:3', "level": 3, "parent": "Mark 1"} def test_navigation(): @@ -68,58 +68,58 @@ def test_navigation(): end=None, down=1 ) == ([ - {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, - {'citeType': 'book', 'ref': 'Mark', "level": 1, "parent": None} + {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, + {'citeType': 'book', 'identifier': 'Mark', "level": 1, "parent": None} ], None, None), "Check that base function works" assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1:1", end="Luke 1#1", down=0) == ( [ - {'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, - {'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"} + {'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, + {'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"} ], - {'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"} + {'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"} ), "Check that ?start/end works" assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1:1", end="Mark 1:2", down=0) == ( [ - {'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, - {'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'verse', 'ref': 'Mark 1:1', "level": 3, "parent": "Mark 1"}, - {'citeType': 'verse', 'ref': 'Mark 1:2', "level": 3, "parent": "Mark 1"} + {'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, + {'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'verse', 'identifier': 'Mark 1:1', "level": 3, "parent": "Mark 1"}, + {'citeType': 'verse', 'identifier': 'Mark 1:2', "level": 3, "parent": "Mark 1"} ], - {'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'verse', 'ref': 'Mark 1:2', "level": 3, "parent": "Mark 1"} + {'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'verse', 'identifier': 'Mark 1:2', "level": 3, "parent": "Mark 1"} ), "Check that ?start/end works across parents" assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke 1", down=1) == ( [ - {'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"}, - {'citeType': 'verse', 'ref': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, - {'citeType': 'verse', 'ref': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, - {'citeType': 'bloup', 'ref': 'Luke 1#1', "level": 3, "parent": "Luke 1"} + {'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"}, + {'citeType': 'verse', 'identifier': 'Luke 1:1', "level": 3, "parent": "Luke 1"}, + {'citeType': 'verse', 'identifier': 'Luke 1:2', "level": 3, "parent": "Luke 1"}, + {'citeType': 'bloup', 'identifier': 'Luke 1#1', "level": 3, "parent": "Luke 1"} ], - {'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"}, + {'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"}, None ), "Check that ?ref works" assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref="Luke", down=1) == ( [ - {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, - {'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"}, + {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, + {'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"}, ], - {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, + {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, None ), "Check that ?ref works" assert get_nav(refs[doc.default_tree], paths[doc.default_tree], start_or_ref=None, end=None, down=2) == ( [ - {'citeType': 'book', 'ref': 'Luke', "level": 1, "parent": None}, - {'citeType': 'chapter', 'ref': 'Luke 1', "level": 2, "parent": "Luke"}, - {'citeType': 'book', 'ref': 'Mark', "level": 1, "parent": None}, - {'citeType': 'chapter', 'ref': 'Mark 1', "level": 2, "parent": "Mark"} + {'citeType': 'book', 'identifier': 'Luke', "level": 1, "parent": None}, + {'citeType': 'chapter', 'identifier': 'Luke 1', "level": 2, "parent": "Luke"}, + {'citeType': 'book', 'identifier': 'Mark', "level": 1, "parent": None}, + {'citeType': 'chapter', 'identifier': 'Mark 1', "level": 2, "parent": "Mark"} ], None, None