diff --git a/app/models.py b/app/models.py index ce0cd86..c0d3457 100644 --- a/app/models.py +++ b/app/models.py @@ -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) + if maintenance.value != mode: + maintenance.value = mode db.session.commit() - return True + 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 + if current_setting.value == "True": + current_setting.value = "False" else: - logging.error('Setting {0} does not exist'.format(setting)) - return False + current_setting.value = "True" + db.session.commit() + return True 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 - db.session.commit() - return True - else: - logging.error('Setting {0} does not exist'.format(setting)) - return False + current_setting.value = value + db.session.commit() + return True 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" diff --git a/app/templates/admin_settings.html b/app/templates/admin_settings.html index 990b672..3eafc46 100644 --- a/app/templates/admin_settings.html +++ b/app/templates/admin_settings.html @@ -30,21 +30,21 @@ - {% for setting in settings %} + {% for setting_name, setting_value in settings.items() %} - {{ setting.name }} - {% if setting.value == "True" or setting.value == "False" %} - {{ setting.value }} + {{ setting_name }} + {% if setting_value == "True" or setting_value == "False" %} + {{ setting_value }} {% else %} - + {% endif %} - {% if setting.value == "True" or setting.value == "False" %} - {% else %} - {% endif %} diff --git a/app/views.py b/app/views.py index 8ee719e..539b063 100644 --- a/app/views.py +++ b/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) diff --git a/init_data.py b/init_data.py index dbd3277..f0573f7 100755 --- a/init_data.py +++ b/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)