Skip to content

Commit

Permalink
chore(models-ontology): delete actions object (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Feb 1, 2024
1 parent f7a2ec9 commit 28299fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 56 deletions.
8 changes: 0 additions & 8 deletions src/dsp_tools/commands/project/models/helpers.py
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
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:
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

0 comments on commit 28299fa

Please sign in to comment.