mpeltriaux
e7031d0bc2
* adds restorable delete functionality to Team model * refactors minor code model parts by introducing DeletableObjectMixin * only non-deleted Teams can be chosen for sharing * deleted Teams can be restored using the proper function on the backend admin * deleted Teams do not provide * adds migration
137 lines
3.7 KiB
Python
137 lines
3.7 KiB
Python
from django.db import models
|
|
|
|
from konova.models import UuidModel, DeletableObjectMixin
|
|
from konova.utils.mailer import Mailer
|
|
from user.models import UserActionLogEntry
|
|
|
|
|
|
class Team(UuidModel, DeletableObjectMixin):
|
|
""" Groups users in self managed teams. Can be used for multi-sharing of data
|
|
|
|
"""
|
|
name = models.CharField(max_length=500, null=True, blank=True)
|
|
description = models.TextField(null=True, blank=True)
|
|
users = models.ManyToManyField("user.User", blank=True, related_name="teams")
|
|
admins = models.ManyToManyField("user.User", blank=True, related_name="+")
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def mark_as_deleted(self, user):
|
|
""" Creates an UserAction entry and stores it in the correct field
|
|
|
|
Args:
|
|
user (User): The performing user
|
|
|
|
Returns:
|
|
|
|
"""
|
|
delete_action = UserActionLogEntry.get_deleted_action(user, "Team deleted")
|
|
self.deleted = delete_action
|
|
self.save()
|
|
|
|
def send_mail_shared_access_given_team(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of given shared access
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_access_given_team(obj_identifier, obj_title, self)
|
|
|
|
def send_mail_shared_access_removed(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of removed shared access
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_access_removed_team(obj_identifier, obj_title, self)
|
|
|
|
def send_mail_shared_data_unrecorded(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of unrecorded data
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_data_unrecorded_team(obj_identifier, obj_title, self)
|
|
|
|
def send_mail_shared_data_recorded(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of unrecorded data
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_data_recorded_team(obj_identifier, obj_title, self)
|
|
|
|
def send_mail_shared_data_checked(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of checked data
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_data_checked_team(obj_identifier, obj_title, self)
|
|
|
|
def send_mail_shared_data_deleted(self, obj_identifier, obj_title):
|
|
""" Sends a mail to the team members in case of deleted data
|
|
|
|
Args:
|
|
obj_identifier ():
|
|
obj_title ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
mailer.send_mail_shared_data_deleted_team(obj_identifier, obj_title, self)
|
|
|
|
def remove_user(self, user):
|
|
""" Removes a user from the team
|
|
|
|
Args:
|
|
user (User): The user to be removed
|
|
|
|
Returns:
|
|
|
|
"""
|
|
self.users.remove(user)
|
|
self.admins.remove(user)
|
|
self.save()
|
|
|
|
def is_user_admin(self, user) -> bool:
|
|
""" Returns whether a given user is an admin of the team
|
|
|
|
Args:
|
|
user (User): The user
|
|
|
|
Returns:
|
|
user_is_admin (bool): Whether the user is an admin or not
|
|
"""
|
|
user_is_admin = self.admins.filter(
|
|
id=user.id
|
|
).exists()
|
|
return user_is_admin
|