mirror of
https://github.com/cwinfo/powerdns-admin.git
synced 2024-11-08 14:40:27 +00:00
Move setting definitions into code (rather than database).
For a setting to be useful, the code has to be able to make sense of it anyway. For this reason it makes sense, that the available settings are defined within the code, rather than in the database, where a missing row has previously caused problems. Instead, settings are now written to the database, when they are changed. So instead of relying on the database initialization process to create all available settings for us in the database, the supported settings and their defaults are now in a `defaults` dict in the Setting class. With this in place, we can stop populating the `setting` table as a part of database initialization and it will be much easier to support new settings in the future (we no longer need to do anything to the database, to achieve that). Another benefit is that any changes to default values will take effect automatically, unless the admin has already modified that setting to his/her liking. To make it easier to get the value of a setting, falling back to defaults etc, a new function `get` has been added to the Setting class. Call it as `Setting().get('setting_name'), and it will take care of returning a setting from the database or return the default value for that setting, if nothing was found. The `get` function returns `None`, if the setting passed to the function, does not exist in the `Setting.defaults` dict - Indicating that we don't know of a setting by that name.
This commit is contained in:
parent
b3e04a0569
commit
6c8a3ac36c
@ -884,8 +884,7 @@ class Domain(db.Model):
|
||||
domain_obj = Domain.query.filter(Domain.name == domain_name).first()
|
||||
domain_auto_ptr = DomainSetting.query.filter(DomainSetting.domain == domain_obj).filter(DomainSetting.setting == 'auto_ptr').first()
|
||||
domain_auto_ptr = strtobool(domain_auto_ptr.value) if domain_auto_ptr else False
|
||||
system_auto_ptr = Setting.query.filter(Setting.name == 'auto_ptr').first()
|
||||
system_auto_ptr = strtobool(system_auto_ptr.value)
|
||||
system_auto_ptr = strtobool(Setting().get('auto_ptr'))
|
||||
self.name = domain_name
|
||||
domain_id = self.get_id_by_name(domain_reverse_name)
|
||||
if None == domain_id and \
|
||||
@ -1468,8 +1467,7 @@ class Record(object):
|
||||
domain_auto_ptr = DomainSetting.query.filter(DomainSetting.domain == domain_obj).filter(DomainSetting.setting == 'auto_ptr').first()
|
||||
domain_auto_ptr = strtobool(domain_auto_ptr.value) if domain_auto_ptr else False
|
||||
|
||||
system_auto_ptr = Setting.query.filter(Setting.name == 'auto_ptr').first()
|
||||
system_auto_ptr = strtobool(system_auto_ptr.value)
|
||||
system_auto_ptr = strtobool(Setting().get('auto_ptr'))
|
||||
|
||||
if system_auto_ptr or domain_auto_ptr:
|
||||
try:
|
||||
@ -1710,6 +1708,18 @@ class Setting(db.Model):
|
||||
name = db.Column(db.String(64))
|
||||
value = db.Column(db.String(256))
|
||||
|
||||
# default settings (serves as list of known settings too):
|
||||
# Note: booleans must be strings because of the way they are stored and used
|
||||
defaults = {
|
||||
'maintenance': 'False',
|
||||
'fullscreen_layout': 'True',
|
||||
'record_helper': 'True',
|
||||
'login_ldap_first': 'True',
|
||||
'default_record_table_size': 15,
|
||||
'default_domain_table_size': 10,
|
||||
'auto_ptr': 'False'
|
||||
}
|
||||
|
||||
def __init__(self, id=None, name=None, value=None):
|
||||
self.id = id
|
||||
self.name = name
|
||||
@ -1722,42 +1732,41 @@ class Setting(db.Model):
|
||||
self.value = value
|
||||
|
||||
def set_mainteance(self, mode):
|
||||
"""
|
||||
mode = True/False
|
||||
"""
|
||||
mode = str(mode)
|
||||
maintenance = Setting.query.filter(Setting.name=='maintenance').first()
|
||||
|
||||
if maintenance is None:
|
||||
value = self.defaults['maintenance']
|
||||
maintenance = Setting(name='maintenance', value=value)
|
||||
db.session.add(maintenance)
|
||||
|
||||
mode = str(mode)
|
||||
|
||||
try:
|
||||
if maintenance:
|
||||
if maintenance.value != mode:
|
||||
maintenance.value = mode
|
||||
db.session.commit()
|
||||
return True
|
||||
else:
|
||||
s = Setting(name='maintenance', value=mode)
|
||||
db.session.add(s)
|
||||
db.session.commit()
|
||||
return True
|
||||
except:
|
||||
logging.error('Cannot set maintenance to {0}'.format(mode))
|
||||
logging.debug(traceback.format_exc())
|
||||
logging.debug(traceback.format_exec())
|
||||
db.session.rollback()
|
||||
return False
|
||||
|
||||
def toggle(self, setting):
|
||||
setting = str(setting)
|
||||
current_setting = Setting.query.filter(Setting.name==setting).first()
|
||||
|
||||
if current_setting is None:
|
||||
value = self.defaults[setting]
|
||||
current_setting = Setting(name=setting, value=value)
|
||||
db.session.add(current_setting)
|
||||
|
||||
try:
|
||||
if current_setting:
|
||||
if current_setting.value == "True":
|
||||
current_setting.value = "False"
|
||||
else:
|
||||
current_setting.value = "True"
|
||||
db.session.commit()
|
||||
return True
|
||||
else:
|
||||
logging.error('Setting {0} does not exist'.format(setting))
|
||||
return False
|
||||
except:
|
||||
logging.error('Cannot toggle setting {0}'.format(setting))
|
||||
logging.debug(traceback.format_exec())
|
||||
@ -1765,23 +1774,34 @@ class Setting(db.Model):
|
||||
return False
|
||||
|
||||
def set(self, setting, value):
|
||||
setting = str(setting)
|
||||
new_value = str(value)
|
||||
current_setting = Setting.query.filter(Setting.name==setting).first()
|
||||
|
||||
if current_setting is None:
|
||||
current_setting = Setting(name=setting, value=None)
|
||||
db.session.add(current_setting)
|
||||
|
||||
value = str(value)
|
||||
|
||||
try:
|
||||
if current_setting:
|
||||
current_setting.value = new_value
|
||||
current_setting.value = value
|
||||
db.session.commit()
|
||||
return True
|
||||
else:
|
||||
logging.error('Setting {0} does not exist'.format(setting))
|
||||
return False
|
||||
except:
|
||||
logging.error('Cannot edit setting {0}'.format(setting))
|
||||
logging.debug(traceback.format_exec())
|
||||
db.session.rollback()
|
||||
return False
|
||||
|
||||
def get(self, setting):
|
||||
if setting in self.defaults:
|
||||
result = self.query.filter(Setting.name == setting).first()
|
||||
if result is not None:
|
||||
return result.value
|
||||
else:
|
||||
return self.defaults[setting]
|
||||
else:
|
||||
logging.error('Unknown setting queried: {0}'.format(setting))
|
||||
|
||||
|
||||
class DomainTemplate(db.Model):
|
||||
__tablename__ = "domain_template"
|
||||
|
@ -30,21 +30,21 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for setting in settings %}
|
||||
{% for setting_name, setting_value in settings.items() %}
|
||||
<tr class="odd ">
|
||||
<td>{{ setting.name }}</td>
|
||||
{% if setting.value == "True" or setting.value == "False" %}
|
||||
<td>{{ setting.value }}</td>
|
||||
<td>{{ setting_name }}</td>
|
||||
{% if setting_value == "True" or setting_value == "False" %}
|
||||
<td>{{ setting_value }}</td>
|
||||
{% else %}
|
||||
<td><input name="value" id="value" value="{{ setting.value }}"></td>
|
||||
<td><input name="value" id="value" value="{{ setting_value }}"></td>
|
||||
{% endif %}
|
||||
<td width="6%">
|
||||
{% if setting.value == "True" or setting.value == "False" %}
|
||||
<button type="button" class="btn btn-flat btn-warning setting-toggle-button" id="{{ setting.name }}">
|
||||
{% if setting_value == "True" or setting_value == "False" %}
|
||||
<button type="button" class="btn btn-flat btn-warning setting-toggle-button" id="{{ setting_name }}">
|
||||
Toggle <i class="fa fa-info"></i>
|
||||
</button>
|
||||
{% else %}
|
||||
<button type="button" class="btn btn-flat btn-warning setting-save-button" id="{{ setting.name }}">
|
||||
<button type="button" class="btn btn-flat btn-warning setting-save-button" id="{{ setting_name }}">
|
||||
Save <i class="fa fa-info"></i>
|
||||
</button>
|
||||
{% endif %}
|
||||
|
42
app/views.py
42
app/views.py
@ -45,49 +45,46 @@ else:
|
||||
|
||||
@app.context_processor
|
||||
def inject_fullscreen_layout_setting():
|
||||
fullscreen_layout_setting = Setting.query.filter(Setting.name == 'fullscreen_layout').first()
|
||||
return dict(fullscreen_layout_setting=strtobool(fullscreen_layout_setting.value))
|
||||
setting_value = Setting().get('fullscreen_layout')
|
||||
return dict(fullscreen_layout_setting=strtobool(setting_value))
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_record_helper_setting():
|
||||
record_helper_setting = Setting.query.filter(Setting.name == 'record_helper').first()
|
||||
return dict(record_helper_setting=strtobool(record_helper_setting.value))
|
||||
setting_value = Setting().get('record_helper')
|
||||
return dict(record_helper_setting=strtobool(setting_value))
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_login_ldap_first_setting():
|
||||
login_ldap_first_setting = Setting.query.filter(Setting.name == 'login_ldap_first').first()
|
||||
return dict(login_ldap_first_setting=strtobool(login_ldap_first_setting.value))
|
||||
setting_value = Setting().get('login_ldap_first')
|
||||
return dict(login_ldap_first_setting=strtobool(setting_value))
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_default_record_table_size_setting():
|
||||
default_record_table_size_setting = Setting.query.filter(Setting.name == 'default_record_table_size').first()
|
||||
return dict(default_record_table_size_setting=default_record_table_size_setting.value)
|
||||
setting_value = Setting().get('default_record_table_size')
|
||||
return dict(default_record_table_size_setting=setting_value)
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_default_domain_table_size_setting():
|
||||
default_domain_table_size_setting = Setting.query.filter(Setting.name == 'default_domain_table_size').first()
|
||||
return dict(default_domain_table_size_setting=default_domain_table_size_setting.value)
|
||||
setting_value = Setting().get('default_domain_table_size')
|
||||
return dict(default_domain_table_size_setting=setting_value)
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def inject_auto_ptr_setting():
|
||||
auto_ptr_setting = Setting.query.filter(Setting.name == 'auto_ptr').first()
|
||||
if auto_ptr_setting is None:
|
||||
return dict(auto_ptr_setting=False)
|
||||
else:
|
||||
return dict(auto_ptr_setting=strtobool(auto_ptr_setting.value))
|
||||
setting_value = Setting().get('auto_ptr')
|
||||
return dict(auto_ptr_setting=strtobool(setting_value))
|
||||
|
||||
|
||||
# START USER AUTHENTICATION HANDLER
|
||||
@app.before_request
|
||||
def before_request():
|
||||
# check site maintenance mode first
|
||||
maintenance = Setting.query.filter(Setting.name == 'maintenance').first()
|
||||
if maintenance and maintenance.value == 'True':
|
||||
maintenance = Setting().get('maintenance')
|
||||
if strtobool(maintenance):
|
||||
return render_template('maintenance.html')
|
||||
|
||||
# check if user is anonymous
|
||||
@ -1307,7 +1304,16 @@ def admin_history():
|
||||
@admin_role_required
|
||||
def admin_settings():
|
||||
if request.method == 'GET':
|
||||
settings = Setting.query.filter(Setting.name != 'maintenance')
|
||||
# start with a copy of the setting defaults (ignore maintenance setting)
|
||||
settings = Setting.defaults.copy()
|
||||
settings.pop('maintenance', None)
|
||||
|
||||
# update settings info with any customizations
|
||||
for s in settings:
|
||||
value = Setting().get(s)
|
||||
if value is not None:
|
||||
settings[s] = value
|
||||
|
||||
return render_template('admin_settings.html', settings=settings)
|
||||
|
||||
|
||||
|
16
init_data.py
16
init_data.py
@ -6,14 +6,6 @@ from app.models import Role, Setting, DomainTemplate
|
||||
admin_role = Role(name='Administrator', description='Administrator')
|
||||
user_role = Role(name='User', description='User')
|
||||
|
||||
setting_1 = Setting(name='maintenance', value='False')
|
||||
setting_2 = Setting(name='fullscreen_layout', value='True')
|
||||
setting_3 = Setting(name='record_helper', value='True')
|
||||
setting_4 = Setting(name='login_ldap_first', value='True')
|
||||
setting_5 = Setting(name='default_record_table_size', value='15')
|
||||
setting_6 = Setting(name='default_domain_table_size', value='10')
|
||||
setting_7 = Setting(name='auto_ptr', value='False')
|
||||
|
||||
template_1 = DomainTemplate(name='basic_template_1', description='Basic Template #1')
|
||||
template_2 = DomainTemplate(name='basic_template_2', description='Basic Template #2')
|
||||
template_3 = DomainTemplate(name='basic_template_3', description='Basic Template #3')
|
||||
@ -21,14 +13,6 @@ template_3 = DomainTemplate(name='basic_template_3', description='Basic Template
|
||||
db.session.add(admin_role)
|
||||
db.session.add(user_role)
|
||||
|
||||
db.session.add(setting_1)
|
||||
db.session.add(setting_2)
|
||||
db.session.add(setting_3)
|
||||
db.session.add(setting_4)
|
||||
db.session.add(setting_5)
|
||||
db.session.add(setting_6)
|
||||
db.session.add(setting_7)
|
||||
|
||||
db.session.add(template_1)
|
||||
db.session.add(template_2)
|
||||
db.session.add(template_3)
|
||||
|
Loading…
Reference in New Issue
Block a user