mirror of https://github.com/jumpserver/jumpserver
refactor: 更改系统消息初始化策略
parent
c0ec0f1343
commit
732f0b55dc
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue