173 lines
5.4 KiB
Python
173 lines
5.4 KiB
Python
from django.db import models
|
|
|
|
from konova.models import UuidModel, DeletableObjectMixin
|
|
from konova.utils.mailer import Mailer
|
|
from user.enums import UserNotificationEnum
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_GAINED.value]
|
|
)
|
|
mailer.send_mail_shared_access_given_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_ACCESS_REMOVED.value]
|
|
)
|
|
mailer.send_mail_shared_access_removed_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value]
|
|
)
|
|
mailer.send_mail_shared_data_unrecorded_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_RECORDED.value]
|
|
)
|
|
mailer.send_mail_shared_data_recorded_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_CHECKED.value]
|
|
)
|
|
mailer.send_mail_shared_data_checked_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
def send_mail_deduction_changed(self, obj_identifier, obj_title, data_changes):
|
|
""" Sends a mail to the team members in case of changed deduction values
|
|
|
|
Args:
|
|
obj_identifier (str): Identifier of the main object
|
|
obj_title (str): Title of the main object
|
|
data_changes (dict): Contains the old|new changes of the deduction changes
|
|
|
|
Returns:
|
|
|
|
"""
|
|
mailer = Mailer()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_DEDUCTION_CHANGES.value]
|
|
)
|
|
mailer.send_mail_deduction_changed_team(obj_identifier, obj_title, self, data_changes, users_to_notify)
|
|
|
|
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()
|
|
users_to_notify = self.users.filter(
|
|
notifications__in=[UserNotificationEnum.NOTIFY_ON_SHARED_DATA_DELETED.value]
|
|
)
|
|
mailer.send_mail_shared_data_deleted_team(obj_identifier, obj_title, self, users_to_notify)
|
|
|
|
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
|