From 2c4966c678a7361f5d224bfe93e8aa53c8da201b Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 3 Sep 2018 15:33:35 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9form?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/fields.py | 7 ++++++- apps/common/forms.py | 47 ++++++++++++++----------------------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/apps/common/fields.py b/apps/common/fields.py index bf41b33aa..8afcdf8d9 100644 --- a/apps/common/fields.py +++ b/apps/common/fields.py @@ -13,7 +13,7 @@ from .utils import get_signer signer = get_signer() -class DictField(forms.Field): +class FormDictField(forms.Field): widget = forms.Textarea def to_python(self, value): @@ -23,6 +23,7 @@ class DictField(forms.Field): # RadioSelect will provide. Because bool("True") == bool('1') == True, # we don't need to handle that explicitly. if isinstance(value, six.string_types): + value = value.replace("'", '"') try: value = json.loads(value) return value @@ -81,3 +82,7 @@ class FormEncryptMixin: class FormEncryptCharField(FormEncryptMixin, forms.CharField): pass + + +class FormEncryptDictField(FormEncryptMixin, FormDictField): + pass diff --git a/apps/common/forms.py b/apps/common/forms.py index 0b1459293..046c64443 100644 --- a/apps/common/forms.py +++ b/apps/common/forms.py @@ -1,45 +1,32 @@ # -*- coding: utf-8 -*- # import json - from django import forms from django.utils.translation import ugettext_lazy as _ from django.db import transaction from django.conf import settings from .models import Setting, common_settings -from .fields import DictField, FormEncryptCharField, FormEncryptMixin - - -# def to_model_value(value): -# try: -# return json.dumps(value) -# except json.JSONDecodeError: -# return None -# -# -# def to_form_value(value): -# try: -# data = json.loads(value) -# if isinstance(data, dict): -# data = value -# return data -# except json.JSONDecodeError: -# return "" +from .fields import FormDictField, FormEncryptCharField, \ + FormEncryptMixin, FormEncryptDictField class BaseForm(forms.Form): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - # db_settings = Setting.objects.all() for name, field in self.fields.items(): db_value = getattr(common_settings, name) django_value = getattr(settings, name) if hasattr(settings, name) else None if db_value is False or db_value: - field.initial = db_value + if isinstance(db_value, dict): + db_value = json.dumps(db_value) + initial_value = db_value elif django_value is False or django_value: - field.initial = django_value + initial_value = django_value + else: + initial_value = '' + field.initial = initial_value def save(self, category="default"): if not self.is_bound: @@ -67,7 +54,6 @@ class BaseForm(forms.Form): setting.encrypted = encrypted setting.cleaned_value = value setting.save() - return setting class BasicSettingForm(BaseForm): @@ -126,15 +112,12 @@ class LDAPSettingForm(BaseForm): label=_("User search filter"), initial='(cn=%(user)s)', help_text=_("Choice may be (cn|uid|sAMAccountName)=%(user)s)") ) - AUTH_LDAP_USER_ATTR_MAP = DictField( + AUTH_LDAP_USER_ATTR_MAP = FormDictField( label=_("User attr map"), - initial=json.dumps({ - "username": "cn", - "name": "sn", - "email": "mail" - }), help_text=_( - "User attr map present how to map LDAP user attr to jumpserver, username,name,email is jumpserver attr") + "User attr map present how to map LDAP user attr to jumpserver, " + "username,name,email is jumpserver attr" + ) ) # AUTH_LDAP_GROUP_SEARCH_OU = CONFIG.AUTH_LDAP_GROUP_SEARCH_OU # AUTH_LDAP_GROUP_SEARCH_FILTER = CONFIG.AUTH_LDAP_GROUP_SEARCH_FILTER @@ -161,13 +144,13 @@ class TerminalSettingForm(BaseForm): TERMINAL_PUBLIC_KEY_AUTH = forms.BooleanField( initial=True, required=False, label=_("Public key auth") ) - TERMINAL_COMMAND_STORAGE = DictField( + TERMINAL_COMMAND_STORAGE = FormEncryptDictField( label=_("Command storage"), help_text=_( "Set terminal storage setting, `default` is the using as default," "You can set other storage and some terminal using" ) ) - TERMINAL_REPLAY_STORAGE = DictField( + TERMINAL_REPLAY_STORAGE = FormEncryptDictField( label=_("Replay storage"), help_text=_( "Set replay storage setting, `default` is the using as default," "You can set other storage and some terminal using"