mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-24 13:00:26 +00:00
fix: There is something wrong with the format of the site message
This commit is contained in:
@@ -33,6 +33,10 @@ def _get_data_template_path(template_name: str):
|
|||||||
return safe_join(settings.DATA_DIR, 'template', rel_path)
|
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):
|
def custom_render_to_string(template_name, context=None, request=None, using=None):
|
||||||
# 如果自定的义模板存在,则使用自定义模板,否则使用系统模板
|
# 如果自定的义模板存在,则使用自定义模板,否则使用系统模板
|
||||||
custom_template = _get_data_template_path(template_name)
|
custom_template = _get_data_template_path(template_name)
|
||||||
|
@@ -9,7 +9,7 @@ from rest_framework.views import APIView
|
|||||||
|
|
||||||
from common.api import JMSGenericViewSet
|
from common.api import JMSGenericViewSet
|
||||||
from common.permissions import OnlySuperUser, IsValidUser
|
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.backends import BACKEND
|
||||||
from notifications.models import SystemMsgSubscription, UserMsgSubscription
|
from notifications.models import SystemMsgSubscription, UserMsgSubscription
|
||||||
from notifications.notifications import CustomMsgTemplateBase
|
from notifications.notifications import CustomMsgTemplateBase
|
||||||
@@ -156,10 +156,10 @@ class TemplateViewSet(JMSGenericViewSet):
|
|||||||
'source': None,
|
'source': None,
|
||||||
}
|
}
|
||||||
|
|
||||||
data_path = _get_data_template_path(meta['template_name'])
|
edit_path = _get_edit_template_path(meta['template_name'])
|
||||||
try:
|
try:
|
||||||
if os.path.exists(data_path):
|
if os.path.exists(edit_path):
|
||||||
with open(data_path, 'r', encoding='utf-8') as f:
|
with open(edit_path, 'r', encoding='utf-8') as f:
|
||||||
item['content'] = f.read()
|
item['content'] = f.read()
|
||||||
item['source'] = 'data'
|
item['source'] = 'data'
|
||||||
else:
|
else:
|
||||||
@@ -183,14 +183,18 @@ class TemplateViewSet(JMSGenericViewSet):
|
|||||||
|
|
||||||
serializer = TemplateEditSerializer(data=request.data)
|
serializer = TemplateEditSerializer(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
template_name = serializer.validated_data['EMAIL_TEMPLATE_NAME']
|
template_name = serializer.validated_data['template_name']
|
||||||
content = serializer.validated_data['EMAIL_TEMPLATE_CONTENT']
|
content = serializer.validated_data['template_content']
|
||||||
|
render_html = serializer.validated_data['render_html']
|
||||||
|
|
||||||
data_path = _get_data_template_path(template_name)
|
data_path = _get_data_template_path(template_name)
|
||||||
|
edit_path = _get_edit_template_path(template_name)
|
||||||
data_dir = os.path.dirname(data_path)
|
data_dir = os.path.dirname(data_path)
|
||||||
try:
|
try:
|
||||||
os.makedirs(data_dir, exist_ok=True)
|
os.makedirs(data_dir, exist_ok=True)
|
||||||
with open(data_path, 'w', encoding='utf-8') as f:
|
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)
|
f.write(content)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({'ok': False, 'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
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:
|
if not template_name:
|
||||||
return Response({'ok': False, 'error': 'template_name is required'}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'ok': False, 'error': 'template_name is required'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
data_path = _get_data_template_path(template_name)
|
data_path = _get_data_template_path(template_name)
|
||||||
|
edit_path = _get_edit_template_path(template_name)
|
||||||
try:
|
try:
|
||||||
if os.path.exists(data_path) and os.path.isfile(data_path):
|
if os.path.exists(data_path) and os.path.isfile(data_path):
|
||||||
os.remove(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:
|
except Exception as e:
|
||||||
return Response({'ok': False, 'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
return Response({'ok': False, 'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||||
return Response({'ok': True, 'path': data_path})
|
return Response({'ok': True, 'path': data_path})
|
||||||
|
@@ -44,10 +44,11 @@ class UserMsgSubscriptionSerializer(BulkModelSerializer):
|
|||||||
|
|
||||||
|
|
||||||
class TemplateEditSerializer(serializers.Serializer):
|
class TemplateEditSerializer(serializers.Serializer):
|
||||||
EMAIL_TEMPLATE_NAME = serializers.CharField(max_length=256)
|
template_name = serializers.CharField(max_length=256)
|
||||||
EMAIL_TEMPLATE_CONTENT = serializers.CharField()
|
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=[])
|
safe_engine = Engine(debug=False, libraries={}, builtins=[])
|
||||||
try:
|
try:
|
||||||
safe_engine.from_string(value)
|
safe_engine.from_string(value)
|
||||||
|
Reference in New Issue
Block a user