Skip to content

Commit

Permalink
chore(models-group): delete unnecessary action object (#794)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Feb 1, 2024
1 parent 0c1a5e2 commit 24fdc9f
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions src/dsp_tools/commands/project/models/group.py
Expand Up @@ -26,7 +26,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 @@ -174,44 +173,36 @@ def fromJsonObj(cls, con: Connection, json_obj: Any) -> Group:
status=status,
)

def toJsonObj(self, action: Actions) -> dict[str, Any]:
tmp = {}
if action == Actions.Create:
if self._name is None:
raise BaseError("There must be a valid name!")
tmp["name"] = self._name
if not self._descriptions.isEmpty():
tmp["descriptions"] = self._descriptions.toJsonObj()
if self._project is None:
raise BaseError("There must be a valid project!")
tmp["project"] = self._project
if self._selfjoin is None:
raise BaseError("There must be a valid value for selfjoin!")
tmp["selfjoin"] = self._selfjoin
if self._status is None:
raise BaseError("There must be a valid value for status!")
tmp["status"] = self._status
else:
if self._name is not None and "name" in self._changed:
tmp["name"] = self._name
if not self._descriptions.isEmpty() and "descriptions" in self._changed:
tmp["descriptions"] = self._descriptions.toJsonObj()
if self._selfjoin is not None and "selfjoin" in self._changed:
tmp["selfjoin"] = self._selfjoin
return tmp

def create(self) -> Group:
jsonobj = self.toJsonObj(Actions.Create)
jsonobj = self._toJsonObj_create()
result = self._con.post(Group.ROUTE, jsonobj)
return Group.fromJsonObj(self._con, result["group"])

def _toJsonObj_create(self):
tmp = {}
if self._name is None:
raise BaseError("There must be a valid name!")
tmp["name"] = self._name
if not self._descriptions.isEmpty():
tmp["descriptions"] = self._descriptions.toJsonObj()
if self._project is None:
raise BaseError("There must be a valid project!")
tmp["project"] = self._project
if self._selfjoin is None:
raise BaseError("There must be a valid value for selfjoin!")
tmp["selfjoin"] = self._selfjoin
if self._status is None:
raise BaseError("There must be a valid value for status!")
tmp["status"] = self._status
return tmp

def read(self) -> Group:
result = self._con.get(Group.ROUTE_SLASH + quote_plus(self._iri))
return Group.fromJsonObj(self._con, result["group"])

def update(self) -> Optional[Group]:
updated_group = None
jsonobj = self.toJsonObj(Actions.Update)
jsonobj = self._toJsonObj_update()
if jsonobj:
result = self._con.put(Group.ROUTE_SLASH + quote_plus(self._iri), jsonobj)
updated_group = Group.fromJsonObj(self._con, result["group"])
Expand All @@ -221,6 +212,16 @@ def update(self) -> Optional[Group]:
updated_group = Group.fromJsonObj(self._con, result["group"])
return updated_group

def _toJsonObj_update(self) -> dict[str, Any]:
tmp = {}
if self._name is not None and "name" in self._changed:
tmp["name"] = self._name
if not self._descriptions.isEmpty() and "descriptions" in self._changed:
tmp["descriptions"] = self._descriptions.toJsonObj()
if self._selfjoin is not None and "selfjoin" in self._changed:
tmp["selfjoin"] = self._selfjoin
return tmp

def delete(self) -> Group:
result = self._con.delete(Group.ROUTE_SLASH + quote_plus(self._iri))
return Group.fromJsonObj(self._con, result["group"])
Expand Down

0 comments on commit 24fdc9f

Please sign in to comment.