Skip to content

Commit

Permalink
chore(models-listnode): delete actions object (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Feb 1, 2024
1 parent c5b9872 commit f7a2ec9
Showing 1 changed file with 34 additions and 47 deletions.
81 changes: 34 additions & 47 deletions src/dsp_tools/commands/project/models/listnode.py
Expand Up @@ -25,7 +25,6 @@
from typing import Any, Optional, Union
from urllib.parse import quote_plus

from dsp_tools.commands.project.models.helpers import Actions
from dsp_tools.commands.project.models.model import Model
from dsp_tools.commands.project.models.project import Project
from dsp_tools.models.exceptions import BaseError
Expand Down Expand Up @@ -366,64 +365,36 @@ def fromJsonObj(cls, con: Connection, json_obj: Any) -> ListNode:
rootNodeIri=rootNodeIri,
)

def toJsonObj(self, action: Actions, listIri: str | None = None) -> dict[str, Any]:
"""
Internal method! Should not be used directly!
Creates a JSON-object from the ListNode instance that can be used to call DSP-API
:param action: Action the object is used for (Action.CREATE or Action.UPDATE)
:param listIri: The IRI of the list node, only used for update action
:return: JSON-object
"""

tmp = {}

if action == Actions.Create:
if self._project is None:
raise BaseError("There must be a project id given!")
tmp["projectIri"] = self._project
if self._label.isEmpty():
raise BaseError("There must be a valid ListNode label!")
tmp["labels"] = self._label.toJsonObj()
if self._comments:
tmp["comments"] = self._comments.toJsonObj()
if self._name:
tmp["name"] = self._name
if self._parent:
tmp["parentNodeIri"] = self._parent

elif action == Actions.Update:
if self.iri is None:
raise BaseError("There must be a node iri given!")
tmp["listIri"] = listIri
if self._project is None:
raise BaseError("There must be a project id given!")
tmp["projectIri"] = self._project
if not self._label.isEmpty() and "label" in self._changed:
tmp["labels"] = self._label.toJsonObj()
if not self._comments.isEmpty() and "comments" in self._changed:
tmp["comments"] = self._comments.toJsonObj()
if self._name and "name" in self._changed:
tmp["name"] = self._name

return tmp

def create(self) -> ListNode:
"""
Create a new List
:return: JSON-object from DSP-API
"""
jsonobj = self.toJsonObj(Actions.Create)
jsonobj = self._toJsonObj_create()
if self._parent:
result = self._con.post(ListNode.ROUTE_SLASH + quote_plus(self._parent), jsonobj)
return ListNode.fromJsonObj(self._con, result["nodeinfo"])
else:
result = self._con.post(ListNode.ROUTE, jsonobj)
return ListNode.fromJsonObj(self._con, result["list"]["listinfo"])

def _toJsonObj_create(self):
tmp = {}
if self._project is None:
raise BaseError("There must be a project id given!")
tmp["projectIri"] = self._project
if self._label.isEmpty():
raise BaseError("There must be a valid ListNode label!")
tmp["labels"] = self._label.toJsonObj()
if self._comments:
tmp["comments"] = self._comments.toJsonObj()
if self._name:
tmp["name"] = self._name
if self._parent:
tmp["parentNodeIri"] = self._parent
return tmp

def read(self) -> Any:
"""
Read a project from DSP-API
Expand All @@ -446,14 +417,30 @@ def update(self) -> Union[Any, None]:
:return: JSON-object from DSP-API reflecting the update
"""

jsonobj = self.toJsonObj(Actions.Update, self.iri)
jsonobj = self._toJsonObj_update()
if jsonobj:
result = self._con.put(ListNode.ROUTE_SLASH + quote_plus(self.iri), jsonobj)
pprint(result)
return ListNode.fromJsonObj(self._con, result["listinfo"])
else:
return None

def _toJsonObj_update(self) -> dict[str, Any]:
tmp = {}
if self.iri is None:
raise BaseError("There must be a node iri given!")
tmp["listIri"] = self.iri
if self._project is None:
raise BaseError("There must be a project id given!")
tmp["projectIri"] = self._project
if not self._label.isEmpty() and "label" in self._changed:
tmp["labels"] = self._label.toJsonObj()
if not self._comments.isEmpty() and "comments" in self._changed:
tmp["comments"] = self._comments.toJsonObj()
if self._name and "name" in self._changed:
tmp["name"] = self._name
return tmp

def delete(self) -> None:
"""
Delete the given ListNode
Expand Down

0 comments on commit f7a2ec9

Please sign in to comment.