2025-05-07 10:09:45 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: maxkb
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: system_setting.py
|
|
|
|
|
|
@date:2024/3/19 16:01
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
from drf_spectacular.utils import extend_schema
|
2025-06-05 06:08:24 +00:00
|
|
|
|
from networkx.algorithms.traversal import dfs_successors
|
2025-05-07 10:09:45 +00:00
|
|
|
|
from rest_framework.request import Request
|
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
|
|
|
|
from common.auth import TokenAuth
|
|
|
|
|
|
from common.auth.authentication import has_permissions
|
|
|
|
|
|
from common.constants.permission_constants import PermissionConstants
|
|
|
|
|
|
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
2025-06-05 06:08:24 +00:00
|
|
|
|
from common.log.log import log
|
2025-05-07 10:09:45 +00:00
|
|
|
|
from common.result import result
|
2025-06-05 06:08:24 +00:00
|
|
|
|
from common.utils.common import encryption
|
2025-05-07 10:09:45 +00:00
|
|
|
|
from models_provider.api.model import DefaultModelResponse
|
|
|
|
|
|
from system_manage.api.email_setting import EmailSettingAPI
|
|
|
|
|
|
from system_manage.serializers.email_setting import EmailSettingSerializer
|
|
|
|
|
|
|
2025-06-10 06:50:25 +00:00
|
|
|
|
|
2025-06-05 06:08:24 +00:00
|
|
|
|
def encryption_str(_value):
|
|
|
|
|
|
if isinstance(_value, str):
|
|
|
|
|
|
return encryption(_value)
|
|
|
|
|
|
return _value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_email_details(request):
|
|
|
|
|
|
path = request.path
|
|
|
|
|
|
body = request.data
|
|
|
|
|
|
query = request.query_params
|
|
|
|
|
|
email_host_password = body.get('email_host_password', '')
|
|
|
|
|
|
return {
|
|
|
|
|
|
'path': path,
|
|
|
|
|
|
'body': {**body, 'email_host_password': encryption_str(email_host_password)},
|
|
|
|
|
|
'query': query
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-07 10:09:45 +00:00
|
|
|
|
|
|
|
|
|
|
class SystemSetting(APIView):
|
|
|
|
|
|
class Email(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
|
|
|
|
|
summary=_('Create or update email settings'),
|
|
|
|
|
|
description=_('Create or update email settings'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Create or update email settings'), # type: ignore
|
2025-05-07 10:09:45 +00:00
|
|
|
|
request=EmailSettingAPI.get_request(),
|
|
|
|
|
|
responses=EmailSettingAPI.get_response(),
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_('Email Settings')]) # type: ignore
|
2025-06-05 06:08:24 +00:00
|
|
|
|
@log(menu='Email settings', operate='Create or update email settings',
|
2025-06-16 08:31:28 +00:00
|
|
|
|
get_details=get_email_details)
|
2025-05-07 10:09:45 +00:00
|
|
|
|
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
|
|
|
|
|
|
def put(self, request: Request):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
EmailSettingSerializer.Create(
|
|
|
|
|
|
data=request.data).update_or_save())
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(
|
|
|
|
|
|
methods=['POST'],
|
|
|
|
|
|
summary=_('Test email settings'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Test email settings'), # type: ignore
|
2025-05-07 10:09:45 +00:00
|
|
|
|
request=EmailSettingAPI.get_request(),
|
|
|
|
|
|
responses=DefaultModelResponse.get_response(),
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_('Email Settings')] # type: ignore
|
2025-05-07 10:09:45 +00:00
|
|
|
|
)
|
|
|
|
|
|
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
|
2025-06-10 06:50:25 +00:00
|
|
|
|
@log(menu='Email settings', operate='Test email settings',
|
2025-06-16 08:31:28 +00:00
|
|
|
|
get_details=get_email_details
|
2025-06-05 06:08:24 +00:00
|
|
|
|
)
|
2025-05-07 10:09:45 +00:00
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
EmailSettingSerializer.Create(
|
|
|
|
|
|
data=request.data).is_valid())
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
|
|
|
|
|
summary=_('Get email settings'),
|
|
|
|
|
|
description=_('Get email settings'),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_('Get email settings'), # type: ignore
|
2025-05-07 10:09:45 +00:00
|
|
|
|
responses=DefaultModelResponse.get_response(),
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_('Email Settings')]) # type: ignore
|
2025-05-07 10:09:45 +00:00
|
|
|
|
@has_permissions(PermissionConstants.EMAIL_SETTING_READ)
|
|
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
EmailSettingSerializer.one())
|