From 7d24b40cc953a887436e9e35f88eb9ca379211f0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 15 Apr 2021 17:18:36 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=94=20Document=20Project=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sophon/core/models.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/sophon/core/models.py b/sophon/core/models.py index ca5c6af..28dc9fb 100644 --- a/sophon/core/models.py +++ b/sophon/core/models.py @@ -357,9 +357,20 @@ class Project(models.Model): ) def get_contributors(self): + """ + :return: All the contributors (:attr:`.owner` + :attr:`.collaborators`) of the project. + """ + return {self.owner, *self.collaborators.values()} - def can_be_viewed_by(self, user): + def can_be_viewed_by(self, user) -> bool: + """ + Check whether an user should be allowed to **view** the project details. + + :param user: The user to check permissions for. + :return: :data:`True` if the user can view the details, or :data:`False` if they cannot. + """ + if self.visibility == "PUBLIC": return True elif self.visibility == "INTERNAL": @@ -369,10 +380,24 @@ class Project(models.Model): else: raise ValueError(f"Unknown visibility value: {self.visibility}") - def can_be_edited_by(self, user): + def can_be_edited_by(self, user) -> bool: + """ + Check whether an user should be allowed to **edit** the project details. + + :param user: The user to check permissions for. + :return: :data:`True` if the user can edit the details, or :data:`False` if they cannot. + """ + return user in self.get_contributors() - def can_be_administrated_by(self, user): + def can_be_administrated_by(self, user) -> bool: + """ + Check whether an user should be allowed to **administrate** the project. + + :param user: The user to check permissions for. + :return: :data:`True` if the user can administrate the project, or :data:`False` if they cannot. + """ + return user == self.owner def __str__(self):