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:
Thomas M Steenholdt 2018-06-21 21:56:51 -02:00
parent b3e04a0569
commit 6c8a3ac36c
4 changed files with 89 additions and 79 deletions

View File

@ -884,8 +884,7 @@ class Domain(db.Model):
domain_obj = Domain.query.filter(Domain.name == domain_name).first() 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 = 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 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(Setting().get('auto_ptr'))
system_auto_ptr = strtobool(system_auto_ptr.value)
self.name = domain_name self.name = domain_name
domain_id = self.get_id_by_name(domain_reverse_name) domain_id = self.get_id_by_name(domain_reverse_name)
if None == domain_id and \ 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 = 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 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(Setting().get('auto_ptr'))
system_auto_ptr = strtobool(system_auto_ptr.value)
if system_auto_ptr or domain_auto_ptr: if system_auto_ptr or domain_auto_ptr:
try: try:
@ -1710,6 +1708,18 @@ class Setting(db.Model):
name = db.Column(db.String(64)) name = db.Column(db.String(64))
value = db.Column(db.String(256)) 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): def __init__(self, id=None, name=None, value=None):
self.id = id self.id = id
self.name = name self.name = name
@ -1722,42 +1732,41 @@ class Setting(db.Model):
self.value = value self.value = value
def set_mainteance(self, mode): def set_mainteance(self, mode):
"""
mode = True/False
"""
mode = str(mode)
maintenance = Setting.query.filter(Setting.name=='maintenance').first() 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: try:
if maintenance:
if maintenance.value != mode: if maintenance.value != mode:
maintenance.value = mode maintenance.value = mode
db.session.commit() db.session.commit()
return True return True
else:
s = Setting(name='maintenance', value=mode)
db.session.add(s)
db.session.commit()
return True
except: except:
logging.error('Cannot set maintenance to {0}'.format(mode)) logging.error('Cannot set maintenance to {0}'.format(mode))
logging.debug(traceback.format_exc()) logging.debug(traceback.format_exec())
db.session.rollback() db.session.rollback()
return False return False
def toggle(self, setting): def toggle(self, setting):
setting = str(setting)
current_setting = Setting.query.filter(Setting.name==setting).first() 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: try:
if current_setting:
if current_setting.value == "True": if current_setting.value == "True":
current_setting.value = "False" current_setting.value = "False"
else: else:
current_setting.value = "True" current_setting.value = "True"
db.session.commit() db.session.commit()
return True return True
else:
logging.error('Setting {0} does not exist'.format(setting))
return False
except: except:
logging.error('Cannot toggle setting {0}'.format(setting)) logging.error('Cannot toggle setting {0}'.format(setting))
logging.debug(traceback.format_exec()) logging.debug(traceback.format_exec())
@ -1765,23 +1774,34 @@ class Setting(db.Model):
return False return False
def set(self, setting, value): def set(self, setting, value):
setting = str(setting)
new_value = str(value)
current_setting = Setting.query.filter(Setting.name==setting).first() 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: try:
if current_setting: current_setting.value = value
current_setting.value = new_value
db.session.commit() db.session.commit()
return True return True
else:
logging.error('Setting {0} does not exist'.format(setting))
return False
except: except:
logging.error('Cannot edit setting {0}'.format(setting)) logging.error('Cannot edit setting {0}'.format(setting))
logging.debug(traceback.format_exec()) logging.debug(traceback.format_exec())
db.session.rollback() db.session.rollback()
return False 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): class DomainTemplate(db.Model):
__tablename__ = "domain_template" __tablename__ = "domain_template"

View File

@ -30,21 +30,21 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for setting in settings %} {% for setting_name, setting_value in settings.items() %}
<tr class="odd "> <tr class="odd ">
<td>{{ setting.name }}</td> <td>{{ setting_name }}</td>
{% if setting.value == "True" or setting.value == "False" %} {% if setting_value == "True" or setting_value == "False" %}
<td>{{ setting.value }}</td> <td>{{ setting_value }}</td>
{% else %} {% else %}
<td><input name="value" id="value" value="{{ setting.value }}"></td> <td><input name="value" id="value" value="{{ setting_value }}"></td>
{% endif %} {% endif %}
<td width="6%"> <td width="6%">
{% if setting.value == "True" or setting.value == "False" %} {% if setting_value == "True" or setting_value == "False" %}
<button type="button" class="btn btn-flat btn-warning setting-toggle-button" id="{{ setting.name }}"> <button type="button" class="btn btn-flat btn-warning setting-toggle-button" id="{{ setting_name }}">
Toggle&nbsp;<i class="fa fa-info"></i> Toggle&nbsp;<i class="fa fa-info"></i>
</button> </button>
{% else %} {% 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&nbsp;<i class="fa fa-info"></i> Save&nbsp;<i class="fa fa-info"></i>
</button> </button>
{% endif %} {% endif %}

View File

@ -45,49 +45,46 @@ else:
@app.context_processor @app.context_processor
def inject_fullscreen_layout_setting(): def inject_fullscreen_layout_setting():
fullscreen_layout_setting = Setting.query.filter(Setting.name == 'fullscreen_layout').first() setting_value = Setting().get('fullscreen_layout')
return dict(fullscreen_layout_setting=strtobool(fullscreen_layout_setting.value)) return dict(fullscreen_layout_setting=strtobool(setting_value))
@app.context_processor @app.context_processor
def inject_record_helper_setting(): def inject_record_helper_setting():
record_helper_setting = Setting.query.filter(Setting.name == 'record_helper').first() setting_value = Setting().get('record_helper')
return dict(record_helper_setting=strtobool(record_helper_setting.value)) return dict(record_helper_setting=strtobool(setting_value))
@app.context_processor @app.context_processor
def inject_login_ldap_first_setting(): def inject_login_ldap_first_setting():
login_ldap_first_setting = Setting.query.filter(Setting.name == 'login_ldap_first').first() setting_value = Setting().get('login_ldap_first')
return dict(login_ldap_first_setting=strtobool(login_ldap_first_setting.value)) return dict(login_ldap_first_setting=strtobool(setting_value))
@app.context_processor @app.context_processor
def inject_default_record_table_size_setting(): def inject_default_record_table_size_setting():
default_record_table_size_setting = Setting.query.filter(Setting.name == 'default_record_table_size').first() setting_value = Setting().get('default_record_table_size')
return dict(default_record_table_size_setting=default_record_table_size_setting.value) return dict(default_record_table_size_setting=setting_value)
@app.context_processor @app.context_processor
def inject_default_domain_table_size_setting(): def inject_default_domain_table_size_setting():
default_domain_table_size_setting = Setting.query.filter(Setting.name == 'default_domain_table_size').first() setting_value = Setting().get('default_domain_table_size')
return dict(default_domain_table_size_setting=default_domain_table_size_setting.value) return dict(default_domain_table_size_setting=setting_value)
@app.context_processor @app.context_processor
def inject_auto_ptr_setting(): def inject_auto_ptr_setting():
auto_ptr_setting = Setting.query.filter(Setting.name == 'auto_ptr').first() setting_value = Setting().get('auto_ptr')
if auto_ptr_setting is None: return dict(auto_ptr_setting=strtobool(setting_value))
return dict(auto_ptr_setting=False)
else:
return dict(auto_ptr_setting=strtobool(auto_ptr_setting.value))
# START USER AUTHENTICATION HANDLER # START USER AUTHENTICATION HANDLER
@app.before_request @app.before_request
def before_request(): def before_request():
# check site maintenance mode first # check site maintenance mode first
maintenance = Setting.query.filter(Setting.name == 'maintenance').first() maintenance = Setting().get('maintenance')
if maintenance and maintenance.value == 'True': if strtobool(maintenance):
return render_template('maintenance.html') return render_template('maintenance.html')
# check if user is anonymous # check if user is anonymous
@ -1307,7 +1304,16 @@ def admin_history():
@admin_role_required @admin_role_required
def admin_settings(): def admin_settings():
if request.method == 'GET': 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) return render_template('admin_settings.html', settings=settings)

View File

@ -6,14 +6,6 @@ from app.models import Role, Setting, DomainTemplate
admin_role = Role(name='Administrator', description='Administrator') admin_role = Role(name='Administrator', description='Administrator')
user_role = Role(name='User', description='User') 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_1 = DomainTemplate(name='basic_template_1', description='Basic Template #1')
template_2 = DomainTemplate(name='basic_template_2', description='Basic Template #2') template_2 = DomainTemplate(name='basic_template_2', description='Basic Template #2')
template_3 = DomainTemplate(name='basic_template_3', description='Basic Template #3') 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(admin_role)
db.session.add(user_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_1)
db.session.add(template_2) db.session.add(template_2)
db.session.add(template_3) db.session.add(template_3)