pref: 修改使用的消息内容 (#7061)

* perf:  再次优化通知

* pref: 修改使用的消息内容

* perf: 修复url地址

Co-authored-by: ibuler <ibuler@qq.com>
This commit is contained in:
fit2bot
2021-10-22 20:06:16 +08:00
committed by GitHub
parent a0db2f6ef8
commit c244cf5f43
29 changed files with 570 additions and 420 deletions

View File

@@ -12,7 +12,10 @@ from notifications.serializers import (
UserMsgSubscriptionSerializer,
)
__all__ = ('BackendListView', 'SystemMsgSubscriptionViewSet', 'UserMsgSubscriptionViewSet')
__all__ = (
'BackendListView', 'SystemMsgSubscriptionViewSet',
'UserMsgSubscriptionViewSet', 'get_all_test_messages'
)
class BackendListView(APIView):
@@ -80,3 +83,43 @@ class UserMsgSubscriptionViewSet(ListModelMixin,
queryset = UserMsgSubscription.objects.all()
serializer_class = UserMsgSubscriptionSerializer
permission_classes = (IsObjectOwner | IsSuperUser, OnlySuperUserCanList)
def get_all_test_messages(request):
import textwrap
from ..notifications import Message
from django.shortcuts import HttpResponse
msgs_cls = Message.get_all_sub_messages()
html_data = '<h3>HTML 格式 </h3>'
text_data = '<h3>Text 格式</h3>'
for msg_cls in msgs_cls:
try:
msg = msg_cls.gen_test_msg()
if not msg:
continue
msg_html = msg.html_msg_with_sign['message']
msg_text = msg.text_msg_with_sign['message']
except NotImplementedError:
msg_html = msg_text = '没有实现方法'
except Exception as e:
msg_html = msg_text = 'Error: ' + str(e)
html_data += """
<h3>{}</h3>
{}
<hr />
""".format(msg_cls.__name__, msg_html)
text_data += textwrap.dedent("""
<h3>{}</h3>
<pre>
{}
</pre>
<br/>
<hr />
""").format(msg_cls.__name__, msg_text)
return HttpResponse(html_data + text_data)

View File

@@ -2,22 +2,23 @@ import traceback
from html2text import HTML2Text
from typing import Iterable
from itertools import chain
import textwrap
from celery import shared_task
from django.utils.translation import gettext_lazy as _
from common.utils.timezone import local_now
from common.utils import lazyproperty
from settings.utils import get_login_title
from users.models import User
from notifications.backends import BACKEND
from .models import SystemMsgSubscription, UserMsgSubscription
__all__ = ('SystemMessage', 'UserMessage', 'system_msgs')
__all__ = ('SystemMessage', 'UserMessage', 'system_msgs', 'Message')
system_msgs = []
user_msgs = []
all_msgs = []
class MessageType(type):
@@ -55,7 +56,6 @@ class Message(metaclass=MessageType):
- publish 该方法的实现与消息订阅的表结构有关
- send_msg
"""
message_type_label: str
category: str
category_label: str
@@ -84,16 +84,13 @@ class Message(metaclass=MessageType):
backend = BACKEND(backend)
if not backend.is_enable:
continue
get_msg_method = getattr(self, f'get_{backend}_msg', self.get_common_msg)
try:
msg = get_msg_method()
except NotImplementedError:
continue
msg = get_msg_method()
client = backend.client()
client.send_msg(users, **msg)
except Exception:
except NotImplementedError:
continue
except:
traceback.print_exc()
@classmethod
@@ -111,10 +108,7 @@ class Message(metaclass=MessageType):
@staticmethod
def get_common_msg() -> dict:
return {
'subject': '',
'message': ''
}
return {'subject': '', 'message': ''}
def get_html_msg(self) -> dict:
return self.get_common_msg()
@@ -133,11 +127,39 @@ class Message(metaclass=MessageType):
@lazyproperty
def text_msg(self) -> dict:
return self.get_text_msg()
msg = self.get_text_msg()
return msg
@lazyproperty
def html_msg(self) -> dict:
return self.get_html_msg()
msg = self.get_html_msg()
return msg
@lazyproperty
def html_msg_with_sign(self):
msg = self.get_html_msg()
msg['message'] = textwrap.dedent("""
{}
<br />
<br />
<small>{}</small>
""").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()
# --------------------------------------------------------------
# 支持不同发送消息的方式定义自己的消息内容,比如有些支持 html 标签
@@ -159,16 +181,16 @@ class Message(metaclass=MessageType):
return self.text_msg
def get_email_msg(self) -> dict:
return self.html_msg
return self.html_msg_with_sign
def get_site_msg_msg(self) -> dict:
return self.html_msg
def get_sms_msg(self) -> dict:
return self.text_msg
return self.text_msg_with_sign
@classmethod
def test_all_messages(cls):
def get_all_sub_messages(cls):
def get_subclasses(cls):
"""returns all subclasses of argument, cls"""
if issubclass(cls, type):
@@ -180,6 +202,12 @@ class Message(metaclass=MessageType):
return subclasses
messages_cls = get_subclasses(cls)
return messages_cls
@classmethod
def test_all_messages(cls):
messages_cls = cls.get_all_sub_messages()
for _cls in messages_cls:
try:
msg = _cls.send_test_msg()
@@ -225,6 +253,11 @@ class UserMessage(Message):
sub = UserMsgSubscription.objects.get(user=self.user)
self.send_msg([self.user], sub.receive_backends)
@classmethod
def get_test_user(cls):
from users.models import User
return User.objects.all().first()
@classmethod
def gen_test_msg(cls):
raise NotImplementedError

View File

@@ -1,6 +1,7 @@
from rest_framework_bulk.routes import BulkRouter
from django.urls import path
from django.conf import settings
from notifications import api
@@ -14,3 +15,8 @@ router.register('site-message', api.SiteMessageViewSet, 'site-message')
urlpatterns = [
path('backends/', api.BackendListView.as_view(), name='backends')
] + router.urls
if settings.DEBUG:
urlpatterns += [
path('debug-msgs/', api.get_all_test_messages, name='debug-all-msgs')
]