Skip to content

Commit

Permalink
chore(models-user-project): delete dead code (#800)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com>
  • Loading branch information
Nora-Olivia-Ammann and jnussbaum committed Feb 1, 2024
1 parent 3413d2f commit 12813bc
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 499 deletions.
4 changes: 0 additions & 4 deletions src/dsp_tools/commands/project/models/ontology.py
Expand Up @@ -14,10 +14,6 @@
associated PropertyClasses and ResourceClasses as well as the assignments.
* Access the information that has been provided to 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
* Call the ``delete``-method on the instance
"""
from __future__ import annotations

Expand Down
19 changes: 1 addition & 18 deletions src/dsp_tools/commands/project/models/project.py
Expand Up @@ -13,11 +13,7 @@
UPDATE:
* You need an instance of an existing Project 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`` given, or use any instance that has the iri set
* Call the ``delete``-method on the instance
* Call the ``update`` method on the instance
In addition there is a static methods ``getAllProjects`` which returns a list of all projects
"""
Expand Down Expand Up @@ -80,9 +76,6 @@ class Project(Model):
update : DSP project information object
Updates the changed attributes and returns the updated information from the project as it is in DSP
delete : DSP result code
Deletes a project and returns the result code
getAllprojects [static]: List of all projects
Returns a list of all projects available
Expand Down Expand Up @@ -386,16 +379,6 @@ def _toJsonObj_update(self) -> dict[str, str]:
tmp["status"] = self._status
return tmp

def delete(self) -> Project:
"""
Delete the given DSP project
:return: DSP response
"""

result = self._con.delete(Project.IRI + quote_plus(self._iri))
return Project.fromJsonObj(self._con, result["project"])

@staticmethod
def getAllProjects(con: Connection) -> list[Project]:
"""
Expand Down
222 changes: 0 additions & 222 deletions src/dsp_tools/commands/project/models/user.py
Expand Up @@ -10,15 +10,6 @@
* Call the ``read``-method on the instance
* Access the information that has been ptovided to the instance
UPDATE:
* You need an instance of an existing User 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`` given, or use any instance that has the iri set
* Call the ``delete``-method on the instance
In addition there is a static methods ``getAllProjects`` which returns a list of all projects
"""

Expand Down Expand Up @@ -72,7 +63,6 @@ class User(Model):
in_groups : set[str]
Set of group IRI's the user is member of [readonly].
Use ``addToGroup``and ``rmFromGroup`` to modify group membership
in_projects : set[str]
Set of project IRI's the user belongs to
Expand All @@ -88,30 +78,6 @@ class User(Model):
read : DSP user information object
Read user data
update : DSP user information object
Updates the changed attributes of a user and returns the updated information as it is in DSP
delete : DSP result code
Deletes a user and returns the result code
addToGroup : None
Add the user to the given group (will be executed when calling ``update``)
rmFromGroup : None
Remove a user from a group (will be executed when calling ``update``)
addToProject : None
adds a user to a project, optional as project administrator (will be executed when calling ``update``)
rmFromProject : None
removes a user from a project (will be executed when calling ``update``)
makeProjectAdmin : None
Promote user to project admin of given project
unmakeProjectAdmin : None
Revoke project admin flog for user for given project
getAllUsers : list of user
Get a list of all users
Expand Down Expand Up @@ -207,11 +173,6 @@ def __init__(
raise BaseError("In_groups must be a set of strings or None!")

self._sysadmin = None if sysadmin is None else bool(sysadmin)
self._add_to_project = {}
self._rm_from_project = {}
self._change_admin = {}
self._add_to_group = set()
self._rm_from_group = set()

@property
def iri(self) -> Optional[str]:
Expand Down Expand Up @@ -324,107 +285,11 @@ def in_groups(self) -> set[str]:
"""Set of group IRI's the user is member of"""
return self._in_groups

def addToGroup(self, value: str) -> None:
"""
Add the user to the given group (executed at next update)
:param value: IRI of the group
:return: None
"""

if value in self._rm_from_group:
self._rm_from_group.pop(value)
elif value not in self._in_groups:
self._add_to_group.add(value)
self._changed.add("in_groups")
else:
raise BaseError("Already member of this group!")

def rmFromGroup(self, value: str) -> None:
"""
Remove the user from the given group (executed at next update)
:param value: Group IRI
:return: None
"""

if value in self._add_to_group:
self._add_to_group.discard(value)
elif value in self._in_groups:
self._rm_from_group.add(value)
self._changed.add("in_groups")
else:
raise BaseError("User is not in groups!")

@property
def in_projects(self) -> dict[str, bool]:
"""dict with project-IRI as key, boolean(True=project admin) as value"""
return self._in_projects

def addToProject(self, value: str, padmin: bool = False) -> None:
"""
Add the user to the given project (executed at next update)
:param value: project IRI
:param padmin: True, if user should be project admin, False otherwise
:return: None
"""

if value in self._rm_from_project:
self._rm_from_project.pop(value)
elif value not in self._in_projects:
self._add_to_project[value] = padmin
self._changed.add("in_projects")
else:
raise BaseError("Already member of this project!")

def rmFromProject(self, value: str) -> None:
"""
Remove the user from the given project (executed at next update)
:param value: Project IRI
:return: None
"""

if value in self._add_to_project:
self._add_to_project.pop(value)
elif value in self._in_projects:
self._rm_from_project[value] = self._in_projects[value]
self._changed.add("in_projects")
else:
raise BaseError("Project is not in list of member projects!")

def makeProjectAdmin(self, value: str) -> None:
"""
Make the user project administrator in the given project (executed at next update)
:param value: Project IRI
:return: None
"""

if value in self._in_projects:
self._change_admin[value] = True
self._changed.add("in_projects")
elif value in self._add_to_project:
self._add_to_project[value] = True
else:
raise BaseError("User is not member of project!")

def unmakeProjectAdmin(self, value: str) -> None:
"""
Revoke project administrator right for the user from the given project (executed at next update)
:param value: Project IRI
:return: None
"""
if value in self._in_projects:
self._change_admin[value] = False
self._changed.add("in_projects")
elif value in self._add_to_project:
self._add_to_project[value] = False
else:
raise BaseError("User is not member of project!")

@classmethod
def _make_fromJsonObj(cls, con: Connection, json_obj: dict[str, Any]) -> User:
User._check_if_jsonObj_has_required_info(json_obj)
Expand Down Expand Up @@ -546,93 +411,6 @@ def read(self) -> User:
raise BaseError("Either user-iri or email is required!")
return User._make_fromJsonObj(self._con, result["user"])

def update(self, requesterPassword: Optional[str] = None) -> User:
"""
Update the user info in DSP with the modified data in this user instance
:param requesterPassword: Old password if a user wants to change it's own password
:return: JSON-object from DSP
"""

jsonobj = self._toJsonObj_update()
if jsonobj:
self._con.put(User.IRI + quote_plus(self._iri) + "/BasicUserInformation", jsonobj)

self._update_changed_values(requesterPassword)
self._update_project_membership()
self._change_admin_rights()
self._update_group_membership()
user = User(con=self._con, iri=self._iri).read()
return user

def _update_group_membership(self):
for p in self._add_to_group:
self._con.post(User.IRI + quote_plus(self._iri) + User.GROUP_MEMBERSHIPS + quote_plus(p))
for p in self._rm_from_group:
self._con.delete(User.IRI + quote_plus(self._iri) + User.GROUP_MEMBERSHIPS + quote_plus(p))

def _change_admin_rights(self):
for p in self._change_admin.items():
if p[0] not in self._in_projects:
raise BaseError("user must be member of project!")
if p[1]:
self._con.post(User.IRI + quote_plus(self._iri) + User.PROJECT_ADMIN_MEMBERSHIPS + quote_plus(p[0]))
else:
self._con.delete(User.IRI + quote_plus(self._iri) + User.PROJECT_ADMIN_MEMBERSHIPS + quote_plus(p[0]))

def _update_project_membership(self):
for p in self._add_to_project.items():
self._con.post(User.IRI + quote_plus(self._iri) + User.PROJECT_MEMBERSHIPS + quote_plus(p[0]))
if p[1]:
self._con.post(User.IRI + quote_plus(self._iri) + User.PROJECT_ADMIN_MEMBERSHIPS + quote_plus(p[0]))
for p in self._rm_from_project:
if self._in_projects.get(p) is not None and self._in_projects[p]:
self._con.delete(User.IRI + quote_plus(self._iri) + User.PROJECT_ADMIN_MEMBERSHIPS + quote_plus(p))
self._con.delete(User.IRI + quote_plus(self._iri) + User.PROJECT_MEMBERSHIPS + quote_plus(p))

def _update_changed_values(self, requesterPassword: Optional[str] = None) -> None:
if "status" in self._changed:
jsonobj = {"status": self._status}
self._con.put(User.IRI + quote_plus(self._iri) + "/Status", jsonobj)
if "password" in self._changed:
if requesterPassword is None:
raise BaseError("Requester's password is missing!")
jsonobj = {"requesterPassword": requesterPassword, "newPassword": self._password}
self._con.put(User.IRI + quote_plus(self._iri) + "/Password", jsonobj)
if "sysadmin" in self._changed:
jsonobj = {"systemAdmin": self._sysadmin}
self._con.put(User.IRI + quote_plus(self._iri) + "/SystemAdmin", jsonobj)

def _toJsonObj_update(self) -> dict[str, Any]:
tmp = {}
tmp_changed = False
if self._username is not None and "username" in self._changed:
tmp["username"] = self._username
tmp_changed = self._username
if self._email is not None and "email" in self._changed:
tmp["email"] = self._email
tmp_changed = True
if self._givenName is not None and "givenName" in self._changed:
tmp["givenName"] = self._givenName
tmp_changed = True
if self._familyName is not None and "familyName" in self._changed:
tmp["familyName"] = self._familyName
tmp_changed = True
if self._lang is not None and "lang" in self._changed:
tmp["lang"] = self._lang.value
tmp_changed = True
if not tmp_changed:
tmp = {}
return tmp

def delete(self) -> User:
"""
Delete the user in nore (NOT YET IMPLEMENTED)
:return: None
"""
result = self._con.delete(User.IRI + quote_plus(self._iri))
return User._make_fromJsonObj(self._con, result["user"])

@staticmethod
def getAllUsers(con: Connection) -> list[Any]:
"""
Expand Down
32 changes: 0 additions & 32 deletions test/e2e/commands/project/test_group.py
Expand Up @@ -78,38 +78,6 @@ def test_group_read(self) -> None:
self.assertTrue(group.status)
self.assertTrue(group.selfjoin)

def test_Group_update(self) -> None:
"""
Update an existing group
:return: None
"""
group = Group(
con=self.con,
name="Group update",
descriptions=LangString({Languages.EN: "This is group update"}),
project=self.test_project,
status=True,
selfjoin=False,
)
group = group.create()

group.name = "Group update - modified"
group.descriptions = LangString({"en": "This is group update - modified"})
group.selfjoin = True
group.status = False
updated_group = group.update()

self.assertIsNotNone(updated_group)
updated_group = cast(Group, updated_group)
self.assertEqual(updated_group.name, "Group update - modified")
self.assertCountEqual(
cast(list[dict[str, str]], updated_group.descriptions.toJsonObj()),
[{"language": "en", "value": "This is group update - modified"}],
)
self.assertEqual(updated_group.project, self.test_project)
self.assertFalse(updated_group.status)
self.assertTrue(updated_group.selfjoin)

def test_Group_delete(self) -> None:
"""
Mark an existing group as deleted (it will not be deleted completely from the triplestore, but status set to
Expand Down
24 changes: 0 additions & 24 deletions test/e2e/commands/project/test_project.py
Expand Up @@ -116,30 +116,6 @@ def test_project_update(self) -> None:
self.assertEqual(updated_project.selfjoin, True)
self.assertEqual(updated_project.status, False)

def test_project_delete(self) -> None:
project = Project(
con=self.con,
shortcode="0FF2",
shortname="delete_project",
longname="Delete Project",
description=LangString({Languages.EN: "Project to be deleted", Languages.DE: "Lösch-Projekt"}),
keywords={"test", "project", "delete"},
selfjoin=False,
status=True,
logo=self.logo_file,
).create()

deleted_project = project.delete()

self.assertEqual(deleted_project.shortcode, "0FF2")
self.assertEqual(deleted_project.shortname, "delete_project")
self.assertEqual(deleted_project.longname, "Delete Project")
self.assertEqual(deleted_project.description["en"], "Project to be deleted")
self.assertEqual(deleted_project.description["de"], "Lösch-Projekt")
self.assertEqual(deleted_project.selfjoin, False)
self.assertEqual(deleted_project.status, False)
self.assertEqual(deleted_project.keywords, {"test", "project", "delete"})


if __name__ == "__main__":
pytest.main([__file__])

0 comments on commit 12813bc

Please sign in to comment.