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 'Gather account change information' %}
-
+
+
- {% trans 'Asset' %} |
- {% trans 'Added account' %} |
- {% trans 'Deleted account' %} |
+
+ {% trans 'Asset' %}
+ |
+
+ {% trans 'Added account' %}
+ |
+
+ {% trans 'Deleted account' %}
+ |
{% for name, change in change_info.items %}
- {{ name }} |
- {{ change.add_usernames }} |
- {{ change.remove_usernames }} |
+
+ {{ name | safe }}
+ |
+
+ {{ change.add_usernames | join:" " | safe }}
+ |
+
+ {{ change.remove_usernames | join:" " | safe }}
+ |
{% endfor %}
diff --git a/apps/common/utils/common.py b/apps/common/utils/common.py
index 680e7cc1b..b2c695c74 100644
--- a/apps/common/utils/common.py
+++ b/apps/common/utils/common.py
@@ -13,6 +13,7 @@ from collections import OrderedDict
from functools import wraps
from itertools import chain
+import html2text
import psutil
from django.conf import settings
from django.templatetags.static import static
@@ -421,3 +422,14 @@ def distinct(seq, key=None):
def is_macos():
return platform.system() == 'Darwin'
+
+
+def convert_html_to_markdown(html_str):
+ h = html2text.HTML2Text()
+ h.body_width = 0
+ h.ignore_links = True
+
+ markdown = h.handle(html_str)
+ markdown = markdown.replace('\n\n', '\n')
+ markdown = markdown.replace('\n ', '\n')
+ return markdown
diff --git a/apps/notifications/serializers/site_msgs.py b/apps/notifications/serializers/site_msgs.py
index 351a04eac..199e4dac8 100644
--- a/apps/notifications/serializers/site_msgs.py
+++ b/apps/notifications/serializers/site_msgs.py
@@ -1,6 +1,7 @@
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
+from common.utils import convert_html_to_markdown
from ..models import MessageContent
@@ -16,6 +17,8 @@ class SenderMixin(ModelSerializer):
class MessageContentSerializer(SenderMixin, ModelSerializer):
+ message = serializers.SerializerMethodField()
+
class Meta:
model = MessageContent
fields = [
@@ -24,6 +27,11 @@ class MessageContentSerializer(SenderMixin, ModelSerializer):
'sender',
]
+ @staticmethod
+ def get_message(site_msg):
+ markdown = convert_html_to_markdown(site_msg.message)
+ return markdown
+
class SiteMessageSerializer(SenderMixin, ModelSerializer):
content = MessageContentSerializer(read_only=True)
diff --git a/apps/tickets/handlers/base.py b/apps/tickets/handlers/base.py
index c74ca7a0c..702a012cd 100644
--- a/apps/tickets/handlers/base.py
+++ b/apps/tickets/handlers/base.py
@@ -1,8 +1,7 @@
-import html2text
from django.template.loader import render_to_string
from django.utils.translation import gettext as _
-from common.utils import get_logger
+from common.utils import get_logger, convert_html_to_markdown
from tickets.const import TicketState, TicketType
from tickets.utils import (
send_ticket_processed_mail_to_applicant,
@@ -97,9 +96,8 @@ class BaseHandler:
approve_info = _('{} {} the ticket').format(user_display, state_display)
context = self._diff_prev_approve_context(state)
context.update({'approve_info': approve_info})
- body = self.safe_html_script(
- render_to_string('tickets/ticket_approve_diff.html', context)
- )
+ html_str = render_to_string('tickets/ticket_approve_diff.html', context)
+ body = convert_html_to_markdown(html_str)
data = {
'body': body,
'user': user,
@@ -108,9 +106,3 @@ class BaseHandler:
'state': state
}
return self.ticket.comments.create(**data)
-
- @staticmethod
- def safe_html_script(unsafe_html):
- unsafe_html = unsafe_html.replace('\n', '')
- html_str = html2text.html2text(unsafe_html)
- return html_str