From 3c1fd134ae6cc3ec73824db9485f3347dfbeabdf Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Mon, 15 Sep 2025 10:56:42 +0800 Subject: [PATCH] fix: There is something wrong with the format of the site message --- apps/common/views/template.py | 4 ++++ apps/notifications/api/notifications.py | 19 +++++++++++++------ .../serializers/notifications.py | 7 ++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/apps/common/views/template.py b/apps/common/views/template.py index e7d98fe83..7c4428adf 100644 --- a/apps/common/views/template.py +++ b/apps/common/views/template.py @@ -33,6 +33,10 @@ def _get_data_template_path(template_name: str): return safe_join(settings.DATA_DIR, 'template', rel_path) +def _get_edit_template_path(template_name: str): + return _get_data_template_path(template_name) + '.edit' + + def custom_render_to_string(template_name, context=None, request=None, using=None): # 如果自定的义模板存在,则使用自定义模板,否则使用系统模板 custom_template = _get_data_template_path(template_name) diff --git a/apps/notifications/api/notifications.py b/apps/notifications/api/notifications.py index 36c0097c9..c2f08bfba 100644 --- a/apps/notifications/api/notifications.py +++ b/apps/notifications/api/notifications.py @@ -9,7 +9,7 @@ from rest_framework.views import APIView from common.api import JMSGenericViewSet from common.permissions import OnlySuperUser, IsValidUser -from common.views.template import _get_data_template_path +from common.views.template import _get_data_template_path, _get_edit_template_path from notifications.backends import BACKEND from notifications.models import SystemMsgSubscription, UserMsgSubscription from notifications.notifications import CustomMsgTemplateBase @@ -156,10 +156,10 @@ class TemplateViewSet(JMSGenericViewSet): 'source': None, } - data_path = _get_data_template_path(meta['template_name']) + edit_path = _get_edit_template_path(meta['template_name']) try: - if os.path.exists(data_path): - with open(data_path, 'r', encoding='utf-8') as f: + if os.path.exists(edit_path): + with open(edit_path, 'r', encoding='utf-8') as f: item['content'] = f.read() item['source'] = 'data' else: @@ -183,14 +183,18 @@ class TemplateViewSet(JMSGenericViewSet): serializer = TemplateEditSerializer(data=request.data) serializer.is_valid(raise_exception=True) - template_name = serializer.validated_data['EMAIL_TEMPLATE_NAME'] - content = serializer.validated_data['EMAIL_TEMPLATE_CONTENT'] + template_name = serializer.validated_data['template_name'] + content = serializer.validated_data['template_content'] + render_html = serializer.validated_data['render_html'] data_path = _get_data_template_path(template_name) + edit_path = _get_edit_template_path(template_name) data_dir = os.path.dirname(data_path) try: os.makedirs(data_dir, exist_ok=True) with open(data_path, 'w', encoding='utf-8') as f: + f.write(render_html) + with open(edit_path, 'w', encoding='utf-8') as f: f.write(content) except Exception as e: return Response({'ok': False, 'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) @@ -203,9 +207,12 @@ class TemplateViewSet(JMSGenericViewSet): if not template_name: return Response({'ok': False, 'error': 'template_name is required'}, status=status.HTTP_400_BAD_REQUEST) data_path = _get_data_template_path(template_name) + edit_path = _get_edit_template_path(template_name) try: if os.path.exists(data_path) and os.path.isfile(data_path): os.remove(data_path) + if os.path.exists(edit_path) and os.path.isfile(edit_path): + os.remove(edit_path) except Exception as e: return Response({'ok': False, 'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return Response({'ok': True, 'path': data_path}) diff --git a/apps/notifications/serializers/notifications.py b/apps/notifications/serializers/notifications.py index 12b5ab895..741253ee4 100644 --- a/apps/notifications/serializers/notifications.py +++ b/apps/notifications/serializers/notifications.py @@ -44,10 +44,11 @@ class UserMsgSubscriptionSerializer(BulkModelSerializer): class TemplateEditSerializer(serializers.Serializer): - EMAIL_TEMPLATE_NAME = serializers.CharField(max_length=256) - EMAIL_TEMPLATE_CONTENT = serializers.CharField() + template_name = serializers.CharField(max_length=256) + template_content = serializers.CharField() + render_html = serializers.CharField() - def validate_EMAIL_TEMPLATE_CONTENT(self, value): + def validate_template_content(self, value): safe_engine = Engine(debug=False, libraries={}, builtins=[]) try: safe_engine.from_string(value)