Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(models-ontology): delete actions object #796

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/dsp_tools/commands/project/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ class OntoIri:
ContextType = dict[str, OntoIri]


@unique
class Actions(Enum):
Create = 1
Read = 2
Update = 3
Delete = 4


@unique
class Cardinality(Enum):
C_1 = "1"
Expand Down
76 changes: 28 additions & 48 deletions src/dsp_tools/commands/project/models/ontology.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
associated PropertyClasses and ResourceClasses as well as the assignments.
* Access the information that has been provided to the instance

UPDATE:
* You need an instance of an existing Ontology by reading an instance
* Change the attributes by assigning the new values
* Call the ``update```method on the instance

DELETE
* Instantiate a new objects with ``iri``(IRI of group) given, or use any instance that has the iri set,
that is, that You've read before
Expand All @@ -33,7 +28,7 @@
import regex

from dsp_tools.commands.project.models.context import Context
from dsp_tools.commands.project.models.helpers import Actions, WithId
from dsp_tools.commands.project.models.helpers import WithId
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.commands.project.models.propertyclass import PropertyClass
Expand Down Expand Up @@ -151,7 +146,7 @@ def context(self) -> Context:
return self._context

@classmethod
def fromJsonObj(cls, con: Connection, json_obj: Any) -> "Ontology":
def fromJsonObj(cls, con: Connection, json_obj: Any) -> Ontology:
#
# First let's get the ID (IRI) of the ontology
#
Expand Down Expand Up @@ -253,9 +248,9 @@ def __oneOntologiesFromJsonObj(cls, con: Connection, json_obj: Any, context: Con
)

@classmethod
def allOntologiesFromJsonObj(cls, con: Connection, json_obj: Any) -> list["Ontology"]:
def allOntologiesFromJsonObj(cls, con: Connection, json_obj: Any) -> list[Ontology]:
context = Context(json_obj.get("@context"))
ontos: list["Ontology"] = []
ontos: list[Ontology] = []
if json_obj.get("@graph") is not None:
for o in json_obj["@graph"]:
ontos.append(Ontology.__oneOntologiesFromJsonObj(con, o, context))
Expand All @@ -265,63 +260,48 @@ def allOntologiesFromJsonObj(cls, con: Connection, json_obj: Any) -> list["Ontol
ontos.append(onto)
return ontos

def toJsonObj(self, action: Actions) -> Any:
def create(self) -> Ontology:
jsonobj = self._toJsonObj_create()
result = self._con.post(Ontology.ROUTE, jsonobj)
return Ontology.fromJsonObj(self._con, result)

def _toJsonObj_create(self):
rdfs = self._context.prefix_from_iri("http://www.w3.org/2000/01/rdf-schema#")
knora_api = self._context.prefix_from_iri("http://api.knora.org/ontology/knora-api/v2#")
tmp = {}
if action == Actions.Create:
if self._name is None:
raise BaseError("There must be a valid name given!")
if self._label is None:
raise BaseError("There must be a valid label given!")
if self._project is None:
raise BaseError("There must be a valid project given!")
tmp = {
knora_api + ":ontologyName": self._name,
knora_api + ":attachedToProject": {"@id": self._project},
rdfs + ":label": self._label,
"@context": self._context.toJsonObj(),
}
if self._comment is not None:
tmp[rdfs + ":comment"] = self._comment
elif action == Actions.Update:
Nora-Olivia-Ammann marked this conversation as resolved.
Show resolved Hide resolved
if self._lastModificationDate is None:
raise BaseError("'last_modification_date' must be in ontology!")
tmp = {
"@id": self._iri,
rdfs + ":label": self._label,
knora_api + ":lastModificationDate": self._lastModificationDate.toJsonObj(),
"@context": self._context.toJsonObj(),
}
if self._label is not None and "label" in self._changed:
tmp[rdfs + ":label"] = self._label
if self._comment is not None and "comment" in self._changed:
tmp[rdfs + ":comment"] = self._comment
if self._name is None:
raise BaseError("There must be a valid name given!")
if self._label is None:
raise BaseError("There must be a valid label given!")
if self._project is None:
raise BaseError("There must be a valid project given!")
tmp = {
knora_api + ":ontologyName": self._name,
knora_api + ":attachedToProject": {"@id": self._project},
rdfs + ":label": self._label,
"@context": self._context.toJsonObj(),
}
if self._comment is not None:
tmp[rdfs + ":comment"] = self._comment
return tmp

def create(self) -> "Ontology":
jsonobj = self.toJsonObj(Actions.Create)
result = self._con.post(Ontology.ROUTE, jsonobj)
return Ontology.fromJsonObj(self._con, result)

def read(self) -> "Ontology":
def read(self) -> Ontology:
result = self._con.get(Ontology.ROUTE + "/allentities/" + quote_plus(self._iri) + Ontology.ALL_LANGUAGES)
return Ontology.fromJsonObj(self._con, result)

@staticmethod
def getAllOntologies(con: Connection) -> list["Ontology"]:
def getAllOntologies(con: Connection) -> list[Ontology]:
result = con.get(Ontology.ROUTE + Ontology.METADATA)
return Ontology.allOntologiesFromJsonObj(con, result)

@staticmethod
def getProjectOntologies(con: Connection, project_id: str) -> list["Ontology"]:
def getProjectOntologies(con: Connection, project_id: str) -> list[Ontology]:
if project_id is None:
raise BaseError("Project ID must be defined!")
result = con.get(Ontology.ROUTE + Ontology.METADATA + quote_plus(project_id) + Ontology.ALL_LANGUAGES)
return Ontology.allOntologiesFromJsonObj(con, result)

@staticmethod
def getOntologyFromServer(con: Connection, shortcode: str, name: str) -> "Ontology":
def getOntologyFromServer(con: Connection, shortcode: str, name: str) -> Ontology:
if regex.search(r"[0-9A-F]{4}", shortcode):
result = con.get("/ontology/" + shortcode + "/" + name + "/v2" + Ontology.ALL_LANGUAGES)
else:
Expand Down