Commit 2c4966c6 authored by ibuler's avatar ibuler

[Update] 修改form

parent d1390a1c
...@@ -13,7 +13,7 @@ from .utils import get_signer ...@@ -13,7 +13,7 @@ from .utils import get_signer
signer = get_signer() signer = get_signer()
class DictField(forms.Field): class FormDictField(forms.Field):
widget = forms.Textarea widget = forms.Textarea
def to_python(self, value): def to_python(self, value):
...@@ -23,6 +23,7 @@ class DictField(forms.Field): ...@@ -23,6 +23,7 @@ class DictField(forms.Field):
# RadioSelect will provide. Because bool("True") == bool('1') == True, # RadioSelect will provide. Because bool("True") == bool('1') == True,
# we don't need to handle that explicitly. # we don't need to handle that explicitly.
if isinstance(value, six.string_types): if isinstance(value, six.string_types):
value = value.replace("'", '"')
try: try:
value = json.loads(value) value = json.loads(value)
return value return value
...@@ -81,3 +82,7 @@ class FormEncryptMixin: ...@@ -81,3 +82,7 @@ class FormEncryptMixin:
class FormEncryptCharField(FormEncryptMixin, forms.CharField): class FormEncryptCharField(FormEncryptMixin, forms.CharField):
pass pass
class FormEncryptDictField(FormEncryptMixin, FormDictField):
pass
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import json import json
from django import forms from django import forms
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db import transaction from django.db import transaction
from django.conf import settings from django.conf import settings
from .models import Setting, common_settings from .models import Setting, common_settings
from .fields import DictField, FormEncryptCharField, FormEncryptMixin from .fields import FormDictField, FormEncryptCharField, \
FormEncryptMixin, FormEncryptDictField
# 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 ""
class BaseForm(forms.Form): class BaseForm(forms.Form):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# db_settings = Setting.objects.all()
for name, field in self.fields.items(): for name, field in self.fields.items():
db_value = getattr(common_settings, name) db_value = getattr(common_settings, name)
django_value = getattr(settings, name) if hasattr(settings, name) else None django_value = getattr(settings, name) if hasattr(settings, name) else None
if db_value is False or db_value: 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: 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"): def save(self, category="default"):
if not self.is_bound: if not self.is_bound:
...@@ -67,7 +54,6 @@ class BaseForm(forms.Form): ...@@ -67,7 +54,6 @@ class BaseForm(forms.Form):
setting.encrypted = encrypted setting.encrypted = encrypted
setting.cleaned_value = value setting.cleaned_value = value
setting.save() setting.save()
return setting
class BasicSettingForm(BaseForm): class BasicSettingForm(BaseForm):
...@@ -126,15 +112,12 @@ class LDAPSettingForm(BaseForm): ...@@ -126,15 +112,12 @@ class LDAPSettingForm(BaseForm):
label=_("User search filter"), initial='(cn=%(user)s)', label=_("User search filter"), initial='(cn=%(user)s)',
help_text=_("Choice may be (cn|uid|sAMAccountName)=%(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"), label=_("User attr map"),
initial=json.dumps({
"username": "cn",
"name": "sn",
"email": "mail"
}),
help_text=_( 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_OU = CONFIG.AUTH_LDAP_GROUP_SEARCH_OU
# AUTH_LDAP_GROUP_SEARCH_FILTER = CONFIG.AUTH_LDAP_GROUP_SEARCH_FILTER # AUTH_LDAP_GROUP_SEARCH_FILTER = CONFIG.AUTH_LDAP_GROUP_SEARCH_FILTER
...@@ -161,13 +144,13 @@ class TerminalSettingForm(BaseForm): ...@@ -161,13 +144,13 @@ class TerminalSettingForm(BaseForm):
TERMINAL_PUBLIC_KEY_AUTH = forms.BooleanField( TERMINAL_PUBLIC_KEY_AUTH = forms.BooleanField(
initial=True, required=False, label=_("Public key auth") initial=True, required=False, label=_("Public key auth")
) )
TERMINAL_COMMAND_STORAGE = DictField( TERMINAL_COMMAND_STORAGE = FormEncryptDictField(
label=_("Command storage"), help_text=_( label=_("Command storage"), help_text=_(
"Set terminal storage setting, `default` is the using as default," "Set terminal storage setting, `default` is the using as default,"
"You can set other storage and some terminal using" "You can set other storage and some terminal using"
) )
) )
TERMINAL_REPLAY_STORAGE = DictField( TERMINAL_REPLAY_STORAGE = FormEncryptDictField(
label=_("Replay storage"), help_text=_( label=_("Replay storage"), help_text=_(
"Set replay storage setting, `default` is the using as default," "Set replay storage setting, `default` is the using as default,"
"You can set other storage and some terminal using" "You can set other storage and some terminal using"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment