Commit dda367a9 authored by ibuler's avatar ibuler

[Update] 修改common settings配置

parent c9aab608
......@@ -4,11 +4,10 @@ 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 FormDictField, FormEncryptCharField, \
FormEncryptMixin, FormEncryptDictField
FormEncryptMixin
class BaseForm(forms.Form):
......@@ -16,17 +15,17 @@ class BaseForm(forms.Form):
super().__init__(*args, **kwargs)
for name, field in self.fields.items():
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 None and django_value is None:
if db_value is None: # and django_value is None:
continue
if db_value is False or db_value:
if db_value is not None:
if isinstance(db_value, dict):
db_value = json.dumps(db_value)
initial_value = db_value
elif django_value is False or django_value:
initial_value = django_value
# elif django_value is False or django_value:
# initial_value = django_value
else:
initial_value = ''
field.initial = initial_value
......
......@@ -40,11 +40,15 @@ class Setting(models.Model):
return self.name
def __getattr__(self, item):
instances = self.__class__.objects.filter(name=item)
default = getattr(settings, item, None)
try:
instances = self.__class__.objects.filter(name=item)
except Exception:
return default
if len(instances) == 1:
return instances[0].cleaned_value
else:
return None
return default
@property
def cleaned_value(self):
......
......@@ -38,7 +38,7 @@ def reverse(view_name, urlconf=None, args=None, kwargs=None,
if external:
from common.models import common_settings
site_url = common_settings.SITE_URL or settings.SITE_URL
site_url = common_settings.SITE_URL
url = site_url.strip('/') + url
return url
......@@ -389,53 +389,20 @@ def get_request_ip(request):
return login_ip
def get_command_storage_or_create_default_storage():
from common.models import common_settings, Setting
name = 'TERMINAL_COMMAND_STORAGE'
default = {'default': {'TYPE': 'server'}}
try:
command_storage = common_settings.TERMINAL_COMMAND_STORAGE
except Exception:
return default
if command_storage is None:
obj = Setting()
obj.name = name
obj.encrypted = True
obj.cleaned_value = default
obj.save()
if isinstance(command_storage, dict) and not command_storage:
obj = Setting.objects.get(name=name)
value = obj.cleaned_value
value.update(default)
obj.cleaned_value = value
obj.save()
command_storage = common_settings.TERMINAL_COMMAND_STORAGE
return command_storage
def get_replay_storage_or_create_default_storage():
from common.models import common_settings, Setting
name = 'TERMINAL_REPLAY_STORAGE'
default = {'default': {'TYPE': 'server'}}
try:
replay_storage = common_settings.TERMINAL_REPLAY_STORAGE
except Exception:
return default
if replay_storage is None:
obj = Setting()
obj.name = name
obj.encrypted = True
obj.cleaned_value = default
obj.save()
replay_storage = common_settings.TERMINAL_REPLAY_STORAGE
if isinstance(replay_storage, dict) and not replay_storage:
obj = Setting.objects.get(name=name)
value = obj.cleaned_value
value.update(default)
obj.cleaned_value = value
obj.save()
replay_storage = common_settings.TERMINAL_REPLAY_STORAGE
return replay_storage
def get_command_storage_setting():
from common.models import common_settings
default = settings.TERMINAL_COMMAND_STORAGE
value = common_settings.TERMINAL_COMMAND_STORAGE
value.update(default)
return value
def get_replay_storage_setting():
from common.models import common_settings
default = settings.TERMINAL_REPLAY_STORAGE
value = common_settings.TERMINAL_REPLAY_STORAGE
value.update(default)
return value
class TeeObj:
......
......@@ -2,9 +2,7 @@ from django.views.generic import TemplateView
from django.shortcuts import render, redirect
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.conf import settings
from common.models import common_settings
from .forms import EmailSettingForm, LDAPSettingForm, BasicSettingForm, \
TerminalSettingForm, SecuritySettingForm
from common.permissions import SuperUserRequiredMixin
......@@ -97,8 +95,8 @@ class TerminalSettingView(SuperUserRequiredMixin, TemplateView):
template_name = "common/terminal_setting.html"
def get_context_data(self, **kwargs):
command_storage = utils.get_command_storage_or_create_default_storage()
replay_storage = utils.get_replay_storage_or_create_default_storage()
command_storage = utils.get_command_storage_setting()
replay_storage = utils.get_replay_storage_setting()
context = {
'app': _('Settings'),
......
......@@ -471,10 +471,10 @@ TERMINAL_REPLAY_STORAGE = {
}
DEFAULT_PASSWORD_MIN_LENGTH = 6
DEFAULT_LOGIN_LIMIT_COUNT = 7
DEFAULT_LOGIN_LIMIT_TIME = 30 # Unit: minute
DEFAULT_SECURITY_MAX_IDLE_TIME = 30 # Unit: minute
SECURITY_PASSWORD_MIN_LENGTH = 6
SECURITY_LOGIN_LIMIT_COUNT = 7
SECURITY_LOGIN_LIMIT_TIME = 30 # Unit: minute
SECURITY_MAX_IDLE_TIME = 30 # Unit: minute
# Django bootstrap3 setting, more see http://django-bootstrap3.readthedocs.io/en/latest/settings.html
BOOTSTRAP3 = {
......
......@@ -18,7 +18,7 @@ def get_command_storage():
def get_terminal_command_storages():
storage_list = {}
command_storage = utils.get_command_storage_or_create_default_storage()
command_storage = utils.get_command_storage_setting()
for name, params in command_storage.items():
tp = params['TYPE']
......
......@@ -9,14 +9,14 @@ from .models import Terminal
def get_all_command_storage():
from common import utils
command_storage = utils.get_command_storage_or_create_default_storage()
command_storage = utils.get_command_storage_setting()
for k, v in command_storage.items():
yield (k, k)
def get_all_replay_storage():
from common import utils
replay_storage = utils.get_replay_storage_or_create_default_storage()
replay_storage = utils.get_replay_storage_setting()
for k, v in replay_storage.items():
yield (k, k)
......
......@@ -64,8 +64,7 @@ class Terminal(models.Model):
configs.update(self.get_common_storage())
configs.update(self.get_replay_storage())
configs.update({
'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME or
settings.DEFAULT_SECURITY_MAX_IDLE_TIME,
'SECURITY_MAX_IDLE_TIME': common_settings.SECURITY_MAX_IDLE_TIME
})
return configs
......
......@@ -307,8 +307,7 @@ def check_password_rules(password):
lower_field_name = 'SECURITY_PASSWORD_LOWER_CASE'
number_field_name = 'SECURITY_PASSWORD_NUMBER'
special_field_name = 'SECURITY_PASSWORD_SPECIAL_CHAR'
min_length = getattr(common_settings, min_field_name) or \
settings.DEFAULT_PASSWORD_MIN_LENGTH
min_length = getattr(common_settings, min_field_name)
password_setting = Setting.objects.filter(name__startswith='SECURITY_PASSWORD')
if not password_setting:
......@@ -340,8 +339,7 @@ def increase_login_failed_count(username, ip):
count = cache.get(key_limit)
count = count + 1 if count else 1
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \
settings.DEFAULT_LOGIN_LIMIT_TIME
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME
cache.set(key_limit, count, int(limit_time)*60)
......@@ -357,10 +355,8 @@ def is_block_login(username, ip):
key_block = key_prefix_block.format(username)
count = cache.get(key_limit, 0)
limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT or \
settings.DEFAULT_LOGIN_LIMIT_COUNT
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME or \
settings.DEFAULT_LOGIN_LIMIT_TIME
limit_count = common_settings.SECURITY_LOGIN_LIMIT_COUNT
limit_time = common_settings.SECURITY_LOGIN_LIMIT_TIME
if count >= limit_count:
cache.set(key_block, 1, int(limit_time)*60)
......
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