2021-05-31 09:20:38 +00:00
|
|
|
|
import traceback
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from html2text import HTML2Text
|
|
|
|
|
from typing import Iterable
|
2021-05-31 09:20:38 +00:00
|
|
|
|
from itertools import chain
|
2021-10-22 12:06:16 +00:00
|
|
|
|
import textwrap
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
from celery import shared_task
|
2021-09-13 09:16:20 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from common.utils.timezone import local_now
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from common.utils import lazyproperty
|
2021-10-22 12:06:16 +00:00
|
|
|
|
from settings.utils import get_login_title
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from users.models import User
|
2021-05-31 09:20:38 +00:00
|
|
|
|
from notifications.backends import BACKEND
|
2021-08-24 06:20:54 +00:00
|
|
|
|
from .models import SystemMsgSubscription, UserMsgSubscription
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
|
__all__ = ('SystemMessage', 'UserMessage', 'system_msgs', 'Message')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
system_msgs = []
|
|
|
|
|
user_msgs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MessageType(type):
|
|
|
|
|
def __new__(cls, name, bases, attrs: dict):
|
|
|
|
|
clz = type.__new__(cls, name, bases, attrs)
|
|
|
|
|
|
|
|
|
|
if 'message_type_label' in attrs \
|
|
|
|
|
and 'category' in attrs \
|
|
|
|
|
and 'category_label' in attrs:
|
|
|
|
|
message_type = clz.get_message_type()
|
|
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
|
'message_type': message_type,
|
|
|
|
|
'message_type_label': attrs['message_type_label'],
|
|
|
|
|
'category': attrs['category'],
|
|
|
|
|
'category_label': attrs['category_label'],
|
|
|
|
|
}
|
|
|
|
|
if issubclass(clz, SystemMessage):
|
|
|
|
|
system_msgs.append(msg)
|
|
|
|
|
elif issubclass(clz, UserMessage):
|
|
|
|
|
user_msgs.append(msg)
|
|
|
|
|
|
|
|
|
|
return clz
|
|
|
|
|
|
|
|
|
|
|
2022-11-15 08:29:40 +00:00
|
|
|
|
@shared_task(verbose_name=_('Publish the station message'))
|
2021-05-31 09:20:38 +00:00
|
|
|
|
def publish_task(msg):
|
|
|
|
|
msg.publish()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Message(metaclass=MessageType):
|
|
|
|
|
"""
|
|
|
|
|
这里封装了什么?
|
|
|
|
|
封装不同消息的模板,提供统一的发送消息的接口
|
|
|
|
|
- publish 该方法的实现与消息订阅的表结构有关
|
|
|
|
|
- send_msg
|
|
|
|
|
"""
|
|
|
|
|
message_type_label: str
|
|
|
|
|
category: str
|
|
|
|
|
category_label: str
|
2021-10-20 11:45:37 +00:00
|
|
|
|
text_msg_ignore_links = True
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_message_type(cls):
|
|
|
|
|
return cls.__name__
|
|
|
|
|
|
|
|
|
|
def publish_async(self):
|
|
|
|
|
return publish_task.delay(self)
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
def publish(self):
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def send_msg(self, users: Iterable, backends: Iterable = BACKEND):
|
2021-09-09 12:12:52 +00:00
|
|
|
|
backends = set(backends)
|
|
|
|
|
backends.add(BACKEND.SITE_MSG) # 站内信必须发
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
for backend in backends:
|
|
|
|
|
try:
|
|
|
|
|
backend = BACKEND(backend)
|
2021-08-24 06:20:54 +00:00
|
|
|
|
if not backend.is_enable:
|
|
|
|
|
continue
|
2021-05-31 09:20:38 +00:00
|
|
|
|
get_msg_method = getattr(self, f'get_{backend}_msg', self.get_common_msg)
|
2021-10-22 12:06:16 +00:00
|
|
|
|
msg = get_msg_method()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
client = backend.client()
|
2021-08-24 06:20:54 +00:00
|
|
|
|
client.send_msg(users, **msg)
|
2021-10-22 12:06:16 +00:00
|
|
|
|
except NotImplementedError:
|
|
|
|
|
continue
|
|
|
|
|
except:
|
2021-05-31 09:20:38 +00:00
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
2021-12-15 07:01:46 +00:00
|
|
|
|
def send_test_msg(cls, ding=True, wecom=False):
|
2021-10-20 11:45:37 +00:00
|
|
|
|
msg = cls.gen_test_msg()
|
|
|
|
|
if not msg:
|
|
|
|
|
return
|
|
|
|
|
|
2021-09-10 09:54:34 +00:00
|
|
|
|
from users.models import User
|
|
|
|
|
users = User.objects.filter(username='admin')
|
2021-10-20 11:45:37 +00:00
|
|
|
|
backends = []
|
|
|
|
|
if ding:
|
|
|
|
|
backends.append(BACKEND.DINGTALK)
|
2021-12-15 07:01:46 +00:00
|
|
|
|
if wecom:
|
|
|
|
|
backends.append(BACKEND.WECOM)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
msg.send_msg(users, backends)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def get_common_msg() -> dict:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
return {'subject': '', 'message': ''}
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-10-20 11:45:37 +00:00
|
|
|
|
return self.get_common_msg()
|
|
|
|
|
|
2021-12-15 07:01:46 +00:00
|
|
|
|
def get_markdown_msg(self) -> dict:
|
|
|
|
|
h = HTML2Text()
|
|
|
|
|
h.body_width = 300
|
|
|
|
|
msg = self.get_html_msg()
|
|
|
|
|
content = msg['message']
|
|
|
|
|
msg['message'] = h.handle(content)
|
|
|
|
|
return msg
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
def get_text_msg(self) -> dict:
|
|
|
|
|
h = HTML2Text()
|
2021-12-15 07:01:46 +00:00
|
|
|
|
h.body_width = 90
|
2021-10-20 11:45:37 +00:00
|
|
|
|
msg = self.get_html_msg()
|
|
|
|
|
content = msg['message']
|
|
|
|
|
h.ignore_links = self.text_msg_ignore_links
|
|
|
|
|
msg['message'] = h.handle(content)
|
|
|
|
|
return msg
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def common_msg(self) -> dict:
|
2021-05-31 09:20:38 +00:00
|
|
|
|
return self.get_common_msg()
|
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def text_msg(self) -> dict:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
msg = self.get_text_msg()
|
|
|
|
|
return msg
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
2021-12-15 07:01:46 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def markdown_msg(self):
|
|
|
|
|
return self.get_markdown_msg()
|
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def html_msg(self) -> dict:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
msg = self.get_html_msg()
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def html_msg_with_sign(self):
|
|
|
|
|
msg = self.get_html_msg()
|
|
|
|
|
msg['message'] = textwrap.dedent("""
|
|
|
|
|
{}
|
2021-10-25 02:08:49 +00:00
|
|
|
|
<small>
|
2021-10-22 12:06:16 +00:00
|
|
|
|
<br />
|
|
|
|
|
—
|
|
|
|
|
<br />
|
2021-10-25 02:08:49 +00:00
|
|
|
|
{}
|
|
|
|
|
</small>
|
2021-10-22 12:06:16 +00:00
|
|
|
|
""").format(msg['message'], self.signature)
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def text_msg_with_sign(self):
|
|
|
|
|
msg = self.get_text_msg()
|
|
|
|
|
msg['message'] = textwrap.dedent("""
|
|
|
|
|
{}
|
|
|
|
|
—
|
|
|
|
|
{}
|
|
|
|
|
""").format(msg['message'], self.signature)
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
|
def signature(self):
|
|
|
|
|
return get_login_title()
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
2021-08-24 06:20:54 +00:00
|
|
|
|
# --------------------------------------------------------------
|
|
|
|
|
# 支持不同发送消息的方式定义自己的消息内容,比如有些支持 html 标签
|
|
|
|
|
def get_dingtalk_msg(self) -> dict:
|
2021-09-13 09:16:20 +00:00
|
|
|
|
# 钉钉相同的消息一天只能发一次,所以给所有消息添加基于时间的序号,使他们不相同
|
2021-12-15 07:01:46 +00:00
|
|
|
|
message = self.markdown_msg['message']
|
2021-10-20 11:45:37 +00:00
|
|
|
|
time = local_now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
|
suffix = '\n{}: {}'.format(_('Time'), time)
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
|
|
|
|
return {
|
2021-12-15 07:01:46 +00:00
|
|
|
|
'subject': self.markdown_msg['subject'],
|
2021-09-13 09:16:20 +00:00
|
|
|
|
'message': message + suffix
|
|
|
|
|
}
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def get_wecom_msg(self) -> dict:
|
2021-12-15 07:01:46 +00:00
|
|
|
|
return self.markdown_msg
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def get_feishu_msg(self) -> dict:
|
2021-09-13 09:16:20 +00:00
|
|
|
|
return self.text_msg
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def get_email_msg(self) -> dict:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
return self.html_msg_with_sign
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def get_site_msg_msg(self) -> dict:
|
2021-09-13 09:16:20 +00:00
|
|
|
|
return self.html_msg
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
def get_sms_msg(self) -> dict:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
return self.text_msg_with_sign
|
2021-10-20 11:45:37 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2021-10-22 12:06:16 +00:00
|
|
|
|
def get_all_sub_messages(cls):
|
2021-10-20 11:45:37 +00:00
|
|
|
|
def get_subclasses(cls):
|
|
|
|
|
"""returns all subclasses of argument, cls"""
|
|
|
|
|
if issubclass(cls, type):
|
|
|
|
|
subclasses = cls.__subclasses__(cls)
|
|
|
|
|
else:
|
|
|
|
|
subclasses = cls.__subclasses__()
|
|
|
|
|
for subclass in subclasses:
|
|
|
|
|
subclasses.extend(get_subclasses(subclass))
|
|
|
|
|
return subclasses
|
|
|
|
|
|
|
|
|
|
messages_cls = get_subclasses(cls)
|
2021-10-22 12:06:16 +00:00
|
|
|
|
return messages_cls
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2021-12-15 07:01:46 +00:00
|
|
|
|
def test_all_messages(cls, ding=True, wecom=False):
|
2021-10-22 12:06:16 +00:00
|
|
|
|
messages_cls = cls.get_all_sub_messages()
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
for _cls in messages_cls:
|
|
|
|
|
try:
|
2021-12-15 07:01:46 +00:00
|
|
|
|
_cls.send_test_msg(ding=ding, wecom=wecom)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
except NotImplementedError:
|
|
|
|
|
continue
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SystemMessage(Message):
|
|
|
|
|
def publish(self):
|
|
|
|
|
subscription = SystemMsgSubscription.objects.get(
|
|
|
|
|
message_type=self.get_message_type()
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 只发送当前有效后端
|
|
|
|
|
receive_backends = subscription.receive_backends
|
|
|
|
|
receive_backends = BACKEND.filter_enable_backends(receive_backends)
|
|
|
|
|
|
|
|
|
|
users = [
|
|
|
|
|
*subscription.users.all(),
|
|
|
|
|
*chain(*[g.users.all() for g in subscription.groups.all()])
|
|
|
|
|
]
|
|
|
|
|
self.send_msg(users, receive_backends)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def post_insert_to_db(cls, subscription: SystemMsgSubscription):
|
|
|
|
|
pass
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
class UserMessage(Message):
|
2021-08-24 06:20:54 +00:00
|
|
|
|
user: User
|
|
|
|
|
|
|
|
|
|
def __init__(self, user):
|
|
|
|
|
self.user = user
|
|
|
|
|
|
|
|
|
|
def publish(self):
|
|
|
|
|
"""
|
|
|
|
|
发送消息到每个用户配置的接收方式上
|
|
|
|
|
"""
|
|
|
|
|
sub = UserMsgSubscription.objects.get(user=self.user)
|
|
|
|
|
self.send_msg([self.user], sub.receive_backends)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get_test_user(cls):
|
|
|
|
|
from users.models import User
|
|
|
|
|
return User.objects.all().first()
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
raise NotImplementedError
|