2021-05-31 09:20:38 +00:00
|
|
|
from django.db import models
|
2022-11-04 06:22:38 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2022-12-05 07:03:21 +00:00
|
|
|
from common.db.models import JMSBaseModel, CASCADE_SIGNAL_SKIP
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
__all__ = ('SystemMsgSubscription', 'UserMsgSubscription')
|
|
|
|
|
|
|
|
|
2022-04-07 10:51:35 +00:00
|
|
|
class UserMsgSubscription(JMSBaseModel):
|
2022-11-04 06:22:38 +00:00
|
|
|
user = models.OneToOneField(
|
|
|
|
'users.User', related_name='user_msg_subscription', on_delete=CASCADE_SIGNAL_SKIP,
|
|
|
|
verbose_name=_('User')
|
|
|
|
)
|
|
|
|
receive_backends = models.JSONField(default=list, verbose_name=_('receive backend'))
|
2022-12-20 12:23:42 +00:00
|
|
|
comment = ''
|
2022-11-04 06:22:38 +00:00
|
|
|
|
2022-12-05 10:54:12 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('User message')
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
def __str__(self):
|
2022-11-04 06:22:38 +00:00
|
|
|
return _('{} subscription').format(self.user)
|
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)
|
2022-12-20 12:23:42 +00:00
|
|
|
comment = ''
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
message_type_label = ''
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('System message')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
def set_message_type_label(self):
|
|
|
|
# 采用手动调用,没设置成 property 的方式
|
|
|
|
# 因为目前只有界面修改时会用到这个属性,避免实例化时占用资源计算
|
|
|
|
from ..notifications import system_msgs
|
|
|
|
msg_label = ''
|
|
|
|
for msg in system_msgs:
|
|
|
|
if msg.get('message_type') == self.message_type:
|
|
|
|
msg_label = msg.get('message_type_label', '')
|
|
|
|
break
|
|
|
|
self.message_type_label = msg_label
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
@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
|
2022-11-04 06:22:38 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f'{self.message_type_label}' or f'{self.message_type}'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.__str__()
|