2022-11-04 06:22:38 +00:00
|
|
|
from django.core.cache import cache
|
2023-02-23 08:20:43 +00:00
|
|
|
from django.utils import translation
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_noop, gettext_lazy as _
|
2024-03-11 07:10:15 +00:00
|
|
|
from rest_framework import serializers
|
2020-04-29 03:08:09 +00:00
|
|
|
|
2023-02-23 08:20:43 +00:00
|
|
|
from common.utils import i18n_fmt
|
2021-09-09 06:00:50 +00:00
|
|
|
from .auth import (
|
|
|
|
LDAPSettingSerializer, OIDCSettingSerializer, KeycloakSettingSerializer,
|
|
|
|
CASSettingSerializer, RadiusSettingSerializer, FeiShuSettingSerializer,
|
2021-08-24 06:20:54 +00:00
|
|
|
WeComSettingSerializer, DingTalkSettingSerializer, AlibabaSMSSettingSerializer,
|
2022-11-04 06:22:38 +00:00
|
|
|
TencentSMSSettingSerializer, CMPP2SMSSettingSerializer, AuthSettingSerializer,
|
2023-09-11 10:15:03 +00:00
|
|
|
SAML2SettingSerializer, OAuth2SettingSerializer, PasskeySettingSerializer,
|
2023-05-24 09:32:57 +00:00
|
|
|
CustomSMSSettingSerializer,
|
2021-09-09 06:00:50 +00:00
|
|
|
)
|
2023-07-24 03:52:25 +00:00
|
|
|
from .basic import BasicSettingSerializer
|
2021-09-09 06:00:50 +00:00
|
|
|
from .cleaning import CleaningSerializer
|
2023-08-15 05:45:44 +00:00
|
|
|
from .msg import EmailSettingSerializer, EmailContentSettingSerializer
|
2023-07-24 03:52:25 +00:00
|
|
|
from .other import OtherSettingSerializer
|
|
|
|
from .security import SecuritySettingSerializer
|
|
|
|
from .terminal import TerminalSettingSerializer
|
2021-08-24 06:20:54 +00:00
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
__all__ = [
|
2024-03-11 07:10:15 +00:00
|
|
|
'BaseSerializerWithFieldLabel',
|
2021-09-09 06:00:50 +00:00
|
|
|
'SettingsSerializer',
|
2021-01-26 09:54:12 +00:00
|
|
|
]
|
2020-04-29 03:08:09 +00:00
|
|
|
|
|
|
|
|
2024-03-11 07:10:15 +00:00
|
|
|
class BaseSerializerWithFieldLabel:
|
|
|
|
CACHE_KEY: str
|
|
|
|
ignore_iter_fields = True
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.fields_label_mapping = None
|
|
|
|
|
|
|
|
def extract_fields(self, serializer):
|
|
|
|
fields = {}
|
|
|
|
for field_name, field in serializer.get_fields().items():
|
|
|
|
if isinstance(field, serializers.Serializer):
|
|
|
|
fields.update(self.extract_fields(field))
|
|
|
|
else:
|
|
|
|
fields.update({field_name: field})
|
|
|
|
return fields
|
|
|
|
|
|
|
|
def get_field_label(self, field_name):
|
|
|
|
self.fields_label_mapping = cache.get(self.CACHE_KEY, None)
|
|
|
|
if self.fields_label_mapping is None:
|
|
|
|
self.fields_label_mapping = {}
|
|
|
|
with translation.override('en'):
|
|
|
|
cls = self.__class__
|
|
|
|
base_name = getattr(cls, 'PREFIX_TITLE', cls.__name__)
|
|
|
|
|
|
|
|
for subclass in cls.__bases__:
|
|
|
|
ignore_iter_fields = getattr(subclass, 'ignore_iter_fields', False)
|
|
|
|
if ignore_iter_fields:
|
|
|
|
continue
|
|
|
|
|
|
|
|
prefix = getattr(subclass, 'PREFIX_TITLE', base_name)
|
|
|
|
fields = self.extract_fields(subclass())
|
|
|
|
for name, item in fields.items():
|
|
|
|
label = getattr(item, 'label', '')
|
|
|
|
detail = i18n_fmt(gettext_noop('[%s] %s'), prefix, label)
|
|
|
|
self.fields_label_mapping[name] = detail
|
|
|
|
cache.set(self.CACHE_KEY, self.fields_label_mapping, 3600 * 24)
|
|
|
|
return self.fields_label_mapping.get(field_name, field_name)
|
|
|
|
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
class SettingsSerializer(
|
2024-03-11 07:10:15 +00:00
|
|
|
BaseSerializerWithFieldLabel,
|
2021-01-26 09:54:12 +00:00
|
|
|
BasicSettingSerializer,
|
|
|
|
LDAPSettingSerializer,
|
2022-11-04 06:22:38 +00:00
|
|
|
AuthSettingSerializer,
|
2021-01-26 09:54:12 +00:00
|
|
|
TerminalSettingSerializer,
|
2021-03-24 11:01:35 +00:00
|
|
|
SecuritySettingSerializer,
|
|
|
|
WeComSettingSerializer,
|
|
|
|
DingTalkSettingSerializer,
|
2021-08-12 08:44:06 +00:00
|
|
|
FeiShuSettingSerializer,
|
2021-09-09 06:00:50 +00:00
|
|
|
EmailSettingSerializer,
|
|
|
|
EmailContentSettingSerializer,
|
|
|
|
OtherSettingSerializer,
|
|
|
|
OIDCSettingSerializer,
|
2022-11-04 06:22:38 +00:00
|
|
|
SAML2SettingSerializer,
|
|
|
|
OAuth2SettingSerializer,
|
2021-09-09 06:00:50 +00:00
|
|
|
KeycloakSettingSerializer,
|
|
|
|
CASSettingSerializer,
|
|
|
|
RadiusSettingSerializer,
|
2021-08-24 06:20:54 +00:00
|
|
|
CleaningSerializer,
|
|
|
|
AlibabaSMSSettingSerializer,
|
|
|
|
TencentSMSSettingSerializer,
|
2022-08-09 08:09:20 +00:00
|
|
|
CMPP2SMSSettingSerializer,
|
2023-05-24 09:32:57 +00:00
|
|
|
CustomSMSSettingSerializer,
|
2023-09-11 10:15:03 +00:00
|
|
|
PasskeySettingSerializer
|
2021-01-26 09:54:12 +00:00
|
|
|
):
|
2024-03-11 07:10:15 +00:00
|
|
|
PREFIX_TITLE = _('Setting')
|
2022-11-04 06:22:38 +00:00
|
|
|
CACHE_KEY = 'SETTING_FIELDS_MAPPING'
|