From b9e64747acdf55ff760e478c8f394da6a98340f8 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Tue, 27 Aug 2024 16:46:41 +0800 Subject: [PATCH] perf: View the internal message and convert the content into markdown --- .../automations/gather_accounts/manager.py | 18 ++++++------ .../accounts/asset_account_change_info.html | 29 +++++++++++++------ apps/common/utils/common.py | 12 ++++++++ apps/notifications/serializers/site_msgs.py | 8 +++++ apps/tickets/handlers/base.py | 14 ++------- 5 files changed, 52 insertions(+), 29 deletions(-) diff --git a/apps/accounts/automations/gather_accounts/manager.py b/apps/accounts/automations/gather_accounts/manager.py index f0610e331..e97d78cd0 100644 --- a/apps/accounts/automations/gather_accounts/manager.py +++ b/apps/accounts/automations/gather_accounts/manager.py @@ -95,12 +95,14 @@ class GatherAccountsManager(AccountBasePlaybookManager): return None, None users = User.objects.filter(id__in=recipients) - if not users: + if not users.exists(): return users, None asset_ids = self.asset_username_mapper.keys() - assets = Asset.objects.filter(id__in=asset_ids) + + assets = Asset.objects.filter(id__in=asset_ids).prefetch_related('accounts') gather_accounts = GatheredAccount.objects.filter(asset_id__in=asset_ids, present=True) + asset_id_map = {str(asset.id): asset for asset in assets} asset_id_username = list(assets.values_list('id', 'accounts__username')) asset_id_username.extend(list(gather_accounts.values_list('asset_id', 'username'))) @@ -109,26 +111,24 @@ class GatherAccountsManager(AccountBasePlaybookManager): for asset_id, username in asset_id_username: system_asset_username_mapper[str(asset_id)].add(username) - change_info = {} + change_info = defaultdict(dict) for asset_id, usernames in self.asset_username_mapper.items(): system_usernames = system_asset_username_mapper.get(asset_id) - if not system_usernames: continue add_usernames = usernames - system_usernames remove_usernames = system_usernames - usernames - k = f'{asset_id_map[asset_id]}[{asset_id}]' if not add_usernames and not remove_usernames: continue - change_info[k] = { - 'add_usernames': ', '.join(add_usernames), - 'remove_usernames': ', '.join(remove_usernames), + change_info[str(asset_id_map[asset_id])] = { + 'add_usernames': add_usernames, + 'remove_usernames': remove_usernames } - return users, change_info + return users, dict(change_info) @staticmethod def send_email_if_need(users, change_info): diff --git a/apps/accounts/templates/accounts/asset_account_change_info.html b/apps/accounts/templates/accounts/asset_account_change_info.html index b778b3cd3..c537dffd4 100644 --- a/apps/accounts/templates/accounts/asset_account_change_info.html +++ b/apps/accounts/templates/accounts/asset_account_change_info.html @@ -1,18 +1,29 @@ {% load i18n %} - -
{% trans 'Asset' %} | -{% trans 'Added account' %} | -{% trans 'Deleted account' %} | ++ {% trans 'Asset' %} + | ++ {% trans 'Added account' %} + | ++ {% trans 'Deleted account' %} + |
---|---|---|---|---|---|
{{ name }} | -{{ change.add_usernames }} | -{{ change.remove_usernames }} | ++ {{ name | safe }} + | ++ {{ change.add_usernames | join:" " | safe }} + | ++ {{ change.remove_usernames | join:" " | safe }} + |