2021-05-31 09:20:38 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
__all__ = ('SystemMsgSubscription', 'UserMsgSubscription')
|
|
|
|
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
class UserMsgSubscription(JMSBaseModel):
|
2021-09-09 12:12:52 +00:00
|
|
|
user = models.OneToOneField('users.User', related_name='user_msg_subscription', on_delete=models.CASCADE)
|
2021-05-31 09:20:38 +00:00
|
|
|
receive_backends = models.JSONField(default=list)
|
|
|
|
|
|
|
|
def __str__(self):
|
2021-08-24 06:20:54 +00:00
|
|
|
return f'{self.user} subscription: {self.receive_backends}'
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
class SystemMsgSubscription(JMSBaseModel):
|
2021-05-31 09:20:38 +00:00
|
|
|
message_type = models.CharField(max_length=128, unique=True)
|
|
|
|
users = models.ManyToManyField('users.User', related_name='system_msg_subscriptions')
|
|
|
|
groups = models.ManyToManyField('users.UserGroup', related_name='system_msg_subscriptions')
|
|
|
|
receive_backends = models.JSONField(default=list)
|
|
|
|
|
|
|
|
message_type_label = ''
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'{self.message_type}'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def receivers(self):
|
|
|
|
from notifications.backends import BACKEND
|
|
|
|
|
|
|
|
users = [user for user in self.users.all()]
|
|
|
|
|
|
|
|
for group in self.groups.all():
|
|
|
|
for user in group.users.all():
|
|
|
|
users.append(user)
|
|
|
|
|
|
|
|
receive_backends = self.receive_backends
|
|
|
|
receviers = []
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
recevier = {'name': str(user), 'id': user.id}
|
|
|
|
for backend in receive_backends:
|
|
|
|
recevier[backend] = bool(BACKEND(backend).get_account(user))
|
|
|
|
receviers.append(recevier)
|
|
|
|
|
|
|
|
return receviers
|