From 9607ebdcc1827a11d4b5aec4a412f2394ae325b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Cl=C3=A9rice?= Date: Mon, 26 Aug 2024 09:12:42 +0200 Subject: [PATCH] Working citeStructure but None citeStructure are broken --- dapitains/app/app.py | 35 +++++++++++++++++----------------- dapitains/app/database.py | 5 ++++- dapitains/app/ingest.py | 21 ++++++++++++-------- dapitains/tei/citeStructure.py | 11 +++++++++++ 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/dapitains/app/app.py b/dapitains/app/app.py index 4832ef0..c039e64 100644 --- a/dapitains/app/app.py +++ b/dapitains/app/app.py @@ -61,22 +61,23 @@ def collection_view( "totalChildren": coll.total_children, "collection": templates["collection"].uri, "member": [ - ( - related.json(inject=(**{ - "collection": templates["collection"].partial({"id": related.identifier}).uri, - "document": templates["collection"].partial({"id": related.identifier}).uri, - }, **( - { - "navigation": templates["collection"].partial( - {"id": related.identifier}).uri, - } if hasattr(coll, "citeStructure") else {})) - if related.resource - else related.json({ - "collection": templates["collection"].partial({"id": related.identifier}).uri - }) - ) - for related in related_collections - ] + 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 coll.citeStructure else {} + ) + ) if related.resource else related.json({ + "collection": templates["collection"].partial({"id": related.identifier}).uri + }) + ) + for related in related_collections + ] }), mimetype="application/json", status=200) @@ -126,7 +127,7 @@ def create_app( Initialisation of the DB is up to you """ navigation_template = uritemplate.URITemplate("/navigation/{?resource}{&ref,start,end,tree,down}") - collection_template = uritemplate.URITemplate("/navigation/collection/{?id,nav}") + collection_template = uritemplate.URITemplate("/collection/collection/{?id,nav}") document_template = uritemplate.URITemplate("/document/{?resource}{&ref,start,end,tree}") @app.route("/collection/") diff --git a/dapitains/app/database.py b/dapitains/app/database.py index 9094e35..954c8c6 100644 --- a/dapitains/app/database.py +++ b/dapitains/app/database.py @@ -58,6 +58,7 @@ class Collection(db.Model): filepath = db.Column(db.String, nullable=True) dublin_core = db.Column(JSONEncoded, nullable=True) extensions = db.Column(JSONEncoded, nullable=True) + citeStructure = db.Column(JSONEncoded, nullable=True) # One-to-one relationship with Navigation navigation = db.relationship('Navigation', uselist=False, backref='collection', lazy=True) @@ -91,7 +92,9 @@ def json(self, inject: Optional[Dict[str, Any]] = None): } if self.description: data["description"] = self.description - if self.dublin_core: + if self.citeStructure: + data["citeStructure"] = self.citeStructure + if self.dublin_core: # ToDo: Fix the way it's presented to adapt to dts view data["dublinCore"] = self.dublin_core if self.extensions: data["extensions"] = self.extensions diff --git a/dapitains/app/ingest.py b/dapitains/app/ingest.py index 5372c54..04cce1e 100644 --- a/dapitains/app/ingest.py +++ b/dapitains/app/ingest.py @@ -15,13 +15,18 @@ def store_catalog(catalog: Catalog): keys[coll_db.identifier] = coll_db.id if collection.resource: doc = Document(collection.filepath) - references = { - tree: [ref.json() for ref in obj.find_refs(doc.xml, structure=obj.units)] - for tree, obj in doc.citeStructure.items() - } - paths = {key: generate_paths(tree) for key, tree in references.items()} - nav = Navigation(collection_id=coll_db.id, paths=paths, references=references) - db.session.add(nav) + if doc.citeStructure: + references = { + tree: [ref.json() for ref in obj.find_refs(doc.xml, structure=obj.units)] + for tree, obj in doc.citeStructure.items() + } + paths = {key: generate_paths(tree) for key, tree in references.items()} + nav = Navigation(collection_id=coll_db.id, paths=paths, references=references) + db.session.add(nav) + coll_db.citeStructure = { + key: value.units.json() + for key, value in doc.citeStructure.items() + } db.session.commit() for parent, child in catalog.relationships: @@ -169,4 +174,4 @@ def get_nav( catalog, _ = ingest_catalog("/home/thibault/dev/MyDapytains/tests/catalog/example-collection.xml") - store_catalog(catalog) \ No newline at end of file + store_catalog(catalog) diff --git a/dapitains/tei/citeStructure.py b/dapitains/tei/citeStructure.py index bdf2338..53bc9e5 100644 --- a/dapitains/tei/citeStructure.py +++ b/dapitains/tei/citeStructure.py @@ -38,6 +38,17 @@ def get(self, ref: str): return f"{self.match}[{self.use}='{ref}']" return f"{self.match}[{self.use}={ref}]" + def json(self): + out = { + "citeType": self.citeType, + } + if self.children: + out["citeStructure"] = [ + child.json() + for child in self.children + ] + return out + @dataclass class CitableUnit: