Skip to content

Commit

Permalink
[WIP] Ability to show the resource in json
Browse files Browse the repository at this point in the history
  • Loading branch information
PonteIneptique committed Aug 25, 2024
1 parent f148f84 commit 47ec985
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
14 changes: 7 additions & 7 deletions dapitains/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def navigation_route():
"dtsVersion": "1-alpha",
"@type": "Navigation",
"@id": "https://example.org/api/dts/navigation/?resource=https://en.wikisource.org/wiki/Dracula&down=1",
#"resource": collection.json(), # To Do: implement and inject URI templates
"resource": collection.json(), # To Do: implement and inject URI templates
"member": members
}

Expand All @@ -89,11 +89,11 @@ def navigation_route():
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db.init_app(app)
# with app.app_context():
# db.drop_all()
# db.create_all()
#
# catalog, _ = ingest_catalog("/home/thibault/dev/MyDapytains/tests/catalog/example-collection.xml")
# store_catalog(catalog)
with app.app_context():
db.drop_all()
db.create_all()

catalog, _ = ingest_catalog("/home/thibault/dev/MyDapytains/tests/catalog/example-collection.xml")
store_catalog(catalog)

app.run()
16 changes: 16 additions & 0 deletions dapitains/app/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
print("This part of the package can only be imported with the web requirements.")
raise

from typing import Optional, Dict, Any
import dapitains.metadata.classes as abstracts
from dapitains.metadata.xml_parser import Catalog
from dapitains.tei.document import Document
Expand Down Expand Up @@ -68,6 +69,21 @@ class Collection(db.Model):
backref='children'
)

def json(self, inject: Optional[Dict[str, Any]] = None):
data = {
"@type": "Resource" if self.resource else "Collection",
"title": self.title,
**(inject or {})
}
if self.description:
data["description"] = self.description
if self.dublin_core:
data["dublinCore"] = self.dublin_core
if self.extensions:
data["extensions"] = self.extensions

return data

@classmethod
def from_class(cls, obj: abstracts.Collection) -> "Collection":
return cls(
Expand Down
10 changes: 8 additions & 2 deletions dapitains/metadata/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class DublinCore:
language: Optional[str] = None

def json(self):
return {"property": f"http://purl.org/dc/terms/{self.term}", "value": self.value, "language": self.language}
if self.language:
return {"property": f"http://purl.org/dc/terms/{self.term}", "value": self.value, "lang": self.language}
else:
return {"property": f"http://purl.org/dc/terms/{self.term}", "value": self.value}


class Extension(DublinCore):
Expand All @@ -18,7 +21,10 @@ class Extension(DublinCore):
language: Optional[str] = None

def json(self):
return {"property": self.term, "value": self.value, "language": self.language}
if self.language:
return {"property": self.term, "value": self.value, "language": self.language}
else:
return {"property": self.term, "value": self.value}


@dataclass
Expand Down

0 comments on commit 47ec985

Please sign in to comment.