From 732f0b55dcb6d84f33403d652adaeab03f7cb93d Mon Sep 17 00:00:00 2001 From: xinwen Date: Mon, 28 Jun 2021 15:33:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E6=B6=88=E6=81=AF=E5=88=9D=E5=A7=8B=E5=8C=96=E7=AD=96?= =?UTF-8?q?=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/notifications/notifications.py | 10 ------- apps/notifications/signals_handler.py | 43 ++++++++++++++++++++++++++- apps/ops/notifications.py | 3 ++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/apps/notifications/notifications.py b/apps/notifications/notifications.py index 4c4db11d9..bbf9fe7ee 100644 --- a/apps/notifications/notifications.py +++ b/apps/notifications/notifications.py @@ -32,16 +32,6 @@ class MessageType(type): } if issubclass(clz, SystemMessage): system_msgs.append(msg) - try: - if not SystemMsgSubscription.objects.filter(message_type=message_type).exists(): - sub = SystemMsgSubscription.objects.create(message_type=message_type) - clz.post_insert_to_db(sub) - except ProgrammingError as e: - if e.args[0] == 1146: - # 表不存在 - pass - else: - raise elif issubclass(clz, UserMessage): user_msgs.append(msg) diff --git a/apps/notifications/signals_handler.py b/apps/notifications/signals_handler.py index 13ebdc4bc..451377557 100644 --- a/apps/notifications/signals_handler.py +++ b/apps/notifications/signals_handler.py @@ -1,13 +1,20 @@ import json +from importlib import import_module +import inspect from django.utils.functional import LazyObject from django.db.models.signals import post_save +from django.db.models.signals import post_migrate from django.dispatch import receiver +from django.db.utils import DEFAULT_DB_ALIAS +from django.apps import apps as global_apps +from django.apps import AppConfig from common.utils.connection import RedisPubSub from common.utils import get_logger from common.decorator import on_transaction_commit -from .models import SiteMessage +from .models import SiteMessage, SystemMsgSubscription +from .notifications import SystemMessage logger = get_logger(__name__) @@ -41,3 +48,37 @@ def on_site_message_create(sender, instance, created, **kwargs): } data = json.dumps(data) new_site_msg_chan.publish(data) + + +@receiver(post_migrate, dispatch_uid='notifications.signals_handler.create_system_messages') +def create_system_messages(app_config: AppConfig, **kwargs): + try: + notifications_module = import_module('.notifications', app_config.module.__package__) + + for name, obj in notifications_module.__dict__.items(): + if name.startswith('_'): + continue + + if not inspect.isclass(obj): + continue + + if not issubclass(obj, SystemMessage): + continue + + attrs = obj.__dict__ + if 'message_type_label' not in attrs: + continue + + if 'category' not in attrs: + continue + + if 'category_label' not in attrs: + continue + + message_type = obj.get_message_type() + sub, created = SystemMsgSubscription.objects.get_or_create(message_type=message_type) + if created: + obj.post_insert_to_db(sub) + logger.info(f'Create SystemMsgSubscription: package={app_config.module.__package__} type={message_type}') + except ModuleNotFoundError: + pass diff --git a/apps/ops/notifications.py b/apps/ops/notifications.py index 61e9d5630..4a65d8a4e 100644 --- a/apps/ops/notifications.py +++ b/apps/ops/notifications.py @@ -3,6 +3,7 @@ from django.utils.translation import gettext_lazy as _ from notifications.notifications import SystemMessage from notifications.models import SystemMsgSubscription from users.models import User +from notifications.backends import BACKEND __all__ = ('ServerPerformanceMessage',) @@ -24,3 +25,5 @@ class ServerPerformanceMessage(SystemMessage): def post_insert_to_db(cls, subscription: SystemMsgSubscription): admins = User.objects.filter(role=User.ROLE.ADMIN) subscription.users.add(*admins) + subscription.receive_backends = [BACKEND.EMAIL] + subscription.save()