From 79eb838250a3cfcf5c20dfc6f9cfc2bab7e2dc3b Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 29 Apr 2020 18:10:42 +0800 Subject: [PATCH] [Update] setting fields automatically generated by serializer --- apps/settings/api.py | 41 +++------------------------ apps/settings/serializers/settings.py | 10 +++---- 2 files changed, 9 insertions(+), 42 deletions(-) diff --git a/apps/settings/api.py b/apps/settings/api.py index f6021f3f2..b8ceaac06 100644 --- a/apps/settings/api.py +++ b/apps/settings/api.py @@ -9,6 +9,7 @@ from rest_framework.views import Response, APIView from django.conf import settings from django.core.mail import send_mail, get_connection from django.utils.translation import ugettext_lazy as _ +from rest_framework import serializers from .utils import ( LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil, @@ -276,45 +277,11 @@ class PublicSettingApi(generics.RetrieveAPIView): class SettingsApi(generics.RetrieveUpdateAPIView): serializer_class = SettingsSerializer - BASIC_CATEGORY = ['SITE_URL', 'USER_GUIDE_URL', 'EMAIL_SUBJECT_PREFIX'] - - EMAIL_CATEGORY = ['EMAIL_HOST', 'EMAIL_PORT', 'EMAIL_HOST_USER', - 'EMAIL_HOST_PASSWORD', 'EMAIL_FROM', 'EMAIL_RECIPIENT', - 'EMAIL_USE_SSL', 'EMAIL_USE_TLS'] - - EMAIL_CONTENT_CATEGORY = ['EMAIL_CUSTOM_USER_CREATED_SUBJECT', 'EMAIL_CUSTOM_USER_CREATED_HONORIFIC', - 'EMAIL_CUSTOM_USER_CREATED_BODY', 'EMAIL_CUSTOM_USER_CREATED_SIGNATURE', ] - - LDAP_CATEGORY = ['AUTH_LDAP_SERVER_URI', 'AUTH_LDAP_BIND_DN', - 'AUTH_LDAP_BIND_PASSWORD', 'AUTH_LDAP_SEARCH_OU', - 'AUTH_LDAP_SEARCH_FILTER', 'AUTH_LDAP_USER_ATTR_MAP', - 'AUTH_LDAP'] - - TERMINAL_CATEGORY = ['TERMINAL_PASSWORD_AUTH', 'TERMINAL_PUBLIC_KEY_AUTH', - 'TERMINAL_HEARTBEAT_INTERVAL', 'TERMINAL_ASSET_LIST_SORT_BY', - 'TERMINAL_ASSET_LIST_PAGE_SIZE', 'TERMINAL_SESSION_KEEP_DURATION', - 'TERMINAL_TELNET_REGEX'] - - SECURITY_CATEGORY = ['SECURITY_MFA_AUTH', 'SECURITY_COMMAND_EXECUTION', - 'SECURITY_SERVICE_ACCOUNT_REGISTRATION', 'SECURITY_LOGIN_LIMIT_COUNT', - 'SECURITY_LOGIN_LIMIT_TIME', 'SECURITY_MAX_IDLE_TIME', - 'SECURITY_PASSWORD_EXPIRATION_TIME', 'SECURITY_PASSWORD_MIN_LENGTH', - 'SECURITY_PASSWORD_UPPER_CASE', 'SECURITY_PASSWORD_LOWER_CASE', - 'SECURITY_PASSWORD_NUMBER', 'SECURITY_PASSWORD_SPECIAL_CHAR'] - - SETTING_CATEGORIES = { - "basic": BASIC_CATEGORY, - 'email': EMAIL_CATEGORY, - 'email_content': EMAIL_CONTENT_CATEGORY, - 'ldap': LDAP_CATEGORY, - 'terminal': TERMINAL_CATEGORY, - 'security': SECURITY_CATEGORY - } def get_object(self): - instance = {category_name: self._get_setting_fields_obj(category_fields) - for category_name, category_fields in self.SETTING_CATEGORIES.items()} - + instance = {category: self._get_setting_fields_obj(list(category_serializer.get_fields())) + for category, category_serializer in self.serializer_class().get_fields().items() + if isinstance(category_serializer, serializers.Serializer)} return ObjectDict(instance) def perform_update(self, serializer): diff --git a/apps/settings/serializers/settings.py b/apps/settings/serializers/settings.py index d8dc8d708..2e2fb2f6b 100644 --- a/apps/settings/serializers/settings.py +++ b/apps/settings/serializers/settings.py @@ -20,7 +20,7 @@ class EmailSettingSerializer(serializers.Serializer): EMAIL_HOST = serializers.CharField(max_length=1024, required=True) EMAIL_PORT = serializers.CharField(max_length=5, required=True) EMAIL_HOST_USER = serializers.CharField(max_length=128, required=True) - EMAIL_HOST_PASSWORD = serializers.CharField(max_length=1024, required=False, write_only=True) + EMAIL_HOST_PASSWORD = serializers.CharField(max_length=1024, write_only=True, required=False, ) EMAIL_FROM = serializers.CharField(max_length=128, allow_blank=True, required=False) EMAIL_RECIPIENT = serializers.CharField(max_length=128, allow_blank=True, required=False) EMAIL_USE_SSL = serializers.BooleanField(required=False) @@ -39,7 +39,7 @@ class LdapSettingSerializer(serializers.Serializer): AUTH_LDAP_SERVER_URI = serializers.CharField(required=True) AUTH_LDAP_BIND_DN = serializers.CharField(required=False) - AUTH_LDAP_BIND_PASSWORD = serializers.CharField(max_length=1024, write_only=True) + AUTH_LDAP_BIND_PASSWORD = serializers.CharField(max_length=1024, write_only=True, required=False) AUTH_LDAP_SEARCH_OU = serializers.CharField(max_length=1024, allow_blank=True, required=False) AUTH_LDAP_SEARCH_FILTER = serializers.CharField(max_length=1024, required=True) AUTH_LDAP_USER_ATTR_MAP = serializers.CharField(max_length=1024, required=True) @@ -63,10 +63,10 @@ class TerminalSettingSerializer(serializers.Serializer): TERMINAL_PASSWORD_AUTH = serializers.BooleanField(required=False) TERMINAL_PUBLIC_KEY_AUTH = serializers.BooleanField(required=False) TERMINAL_HEARTBEAT_INTERVAL = serializers.IntegerField(min_value=5, max_value=99999, required=True) - TERMINAL_ASSET_LIST_SORT_BY = serializers.ChoiceField(SORT_BY_CHOICES) - TERMINAL_ASSET_LIST_PAGE_SIZE = serializers.ChoiceField(PAGE_SIZE_CHOICES) + TERMINAL_ASSET_LIST_SORT_BY = serializers.ChoiceField(SORT_BY_CHOICES, required=False) + TERMINAL_ASSET_LIST_PAGE_SIZE = serializers.ChoiceField(PAGE_SIZE_CHOICES, required=False) TERMINAL_SESSION_KEEP_DURATION = serializers.IntegerField(min_value=1, max_value=99999, required=True) - TERMINAL_TELNET_REGEX = serializers.CharField(required=False, allow_blank=True) + TERMINAL_TELNET_REGEX = serializers.CharField(allow_blank=True, required=False) class SecuritySettingSerializer(serializers.Serializer):