From 26c2b5e16936967e82412c001150187cf08673f9 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Mon, 3 Sep 2018 17:27:09 +0700 Subject: [PATCH] Adjustment in setting handler to work without initial DB. Discussed in #350 --- app/lib/utils.py | 9 ++++++ app/models.py | 19 ++++-------- app/templates/admin_setting_basic.html | 21 +++++++------ app/views.py | 23 ++++++++++++-- ...4ed462010_remove_all_settings_in_the_db.py | 30 +++++++++++++++++++ 5 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 migrations/versions/1274ed462010_remove_all_settings_in_the_db.py diff --git a/app/lib/utils.py b/app/lib/utils.py index 9e9b3be..881fddc 100644 --- a/app/lib/utils.py +++ b/app/lib/utils.py @@ -282,3 +282,12 @@ def init_saml_auth(req): settings['organization']['en-US']['url'] = own_url auth = OneLogin_Saml2_Auth(req, settings) return auth + + +def display_setting_state(value): + if value == 1: + return "ON" + elif value == 0: + return "OFF" + else: + return "UNKNOWN" diff --git a/app/models.py b/app/models.py index 505fd62..812fff4 100644 --- a/app/models.py +++ b/app/models.py @@ -1786,7 +1786,6 @@ class Setting(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(64)) value = db.Column(db.Text()) - view = db.Column(db.String(64)) defaults = { 'maintenance': False, @@ -1923,20 +1922,14 @@ class Setting(db.Model): return list(set(self.get_forward_records_allow_to_edit() + self.get_reverse_records_allow_to_edit())) def get_forward_records_allow_to_edit(self): - records = literal_eval(self.get('forward_records_allow_edit')) - return [r for r in records if records[r]] + records = self.get('forward_records_allow_edit') + f_records = literal_eval(records) if isinstance(records, str) else records + return [r for r in f_records if f_records[r]] def get_reverse_records_allow_to_edit(self): - records = literal_eval(self.get('reverse_records_allow_edit')) - return [r for r in records if records[r]] - - def get_view(self, view): - r = {} - settings = Setting.query.filter(Setting.view == view).all() - for setting in settings: - d = setting.__dict__ - r[d['name']] = d['value'] - return r + records = self.get('reverse_records_allow_edit') + r_records = literal_eval(records) if isinstance(records, str) else records + return [r for r in r_records if r_records[r]] class DomainTemplate(db.Model): diff --git a/app/templates/admin_setting_basic.html b/app/templates/admin_setting_basic.html index d5bdb84..2b6834d 100644 --- a/app/templates/admin_setting_basic.html +++ b/app/templates/admin_setting_basic.html @@ -34,23 +34,22 @@ {% for setting in settings %} - {{ setting.name }} - {% if setting.value == "True" or setting.value == "False" %} - {{ setting.value }} - {% else %} - - {% endif %} + {{ setting }} + {% if SETTING.get(setting) in [True, False] %} + {{ SETTING.get(setting)|display_setting_state }} - {% if setting.value == "True" or setting.value == "False" %} - - {% else %} - - {% endif %} + {% endif %} {% endfor %} diff --git a/app/views.py b/app/views.py index 9a9915e..9c51a23 100644 --- a/app/views.py +++ b/app/views.py @@ -34,6 +34,7 @@ app.jinja_env.filters['display_record_name'] = utils.display_record_name app.jinja_env.filters['display_master_name'] = utils.display_master_name app.jinja_env.filters['display_second_to_time'] = utils.display_time app.jinja_env.filters['email_to_gravatar_url'] = utils.email_to_gravatar_url +app.jinja_env.filters['display_setting_state'] = utils.display_setting_state @app.context_processor @@ -1343,7 +1344,20 @@ def admin_history(): @operator_role_required def admin_setting_basic(): if request.method == 'GET': - settings = Setting.query.filter(Setting.view=='basic').all() + settings = ['maintenance', + 'fullscreen_layout', + 'record_helper', + 'login_ldap_first', + 'default_record_table_size', + 'default_domain_table_size', + 'auto_ptr', + 'record_quick_edit', + 'pretty_ipv6_ptr', + 'dnssec_admins_only', + 'allow_user_create_domain', + 'bg_domain_updates', + 'site_name'] + return render_template('admin_setting_basic.html', settings=settings) @@ -1398,8 +1412,11 @@ def admin_setting_pdns(): @operator_role_required def admin_setting_records(): if request.method == 'GET': - f_records = literal_eval(Setting().get('forward_records_allow_edit')) - r_records = literal_eval(Setting().get('reverse_records_allow_edit')) + _fr = Setting().get('forward_records_allow_edit') + _rr = Setting().get('reverse_records_allow_edit') + f_records = literal_eval(_fr) if isinstance(_fr, str) else _fr + r_records = literal_eval(_rr) if isinstance(_rr, str) else _rr + return render_template('admin_setting_records.html', f_records=f_records, r_records=r_records) elif request.method == 'POST': fr = {} diff --git a/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py b/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py new file mode 100644 index 0000000..9c3b7da --- /dev/null +++ b/migrations/versions/1274ed462010_remove_all_settings_in_the_db.py @@ -0,0 +1,30 @@ +"""Change setting.value data type + +Revision ID: 31a4ed468b18 +Revises: 4a666113c7bb +Create Date: 2018-08-21 17:12:30.058782 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '31a4ed468b18' +down_revision = '4a666113c7bb' +branch_labels = None +depends_on = None + + +def upgrade(): + # delete all settings from "setting" table. + # PDA should work without initial settings in the DB + # Once user change the settings from UI, they will be + # written to the DB. + op.execute("DELETE FROM setting") + + # drop view column since we don't need it + op.drop_column('setting', 'view') + +def downgrade(): + op.add_column('setting', sa.Column('view', sa.String(length=64), nullable=True))