Adjustment in setting handler to work without initial DB. Discussed in #350

This commit is contained in:
Khanh Ngo 2018-09-03 17:27:09 +07:00
parent c7689e7ce7
commit 26c2b5e169
No known key found for this signature in database
GPG Key ID: B9AE3BAF6D5A7B22
5 changed files with 75 additions and 27 deletions

View File

@ -282,3 +282,12 @@ def init_saml_auth(req):
settings['organization']['en-US']['url'] = own_url settings['organization']['en-US']['url'] = own_url
auth = OneLogin_Saml2_Auth(req, settings) auth = OneLogin_Saml2_Auth(req, settings)
return auth return auth
def display_setting_state(value):
if value == 1:
return "ON"
elif value == 0:
return "OFF"
else:
return "UNKNOWN"

View File

@ -1786,7 +1786,6 @@ class Setting(db.Model):
id = db.Column(db.Integer, primary_key = True) id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(64)) name = db.Column(db.String(64))
value = db.Column(db.Text()) value = db.Column(db.Text())
view = db.Column(db.String(64))
defaults = { defaults = {
'maintenance': False, '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())) 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): def get_forward_records_allow_to_edit(self):
records = literal_eval(self.get('forward_records_allow_edit')) records = self.get('forward_records_allow_edit')
return [r for r in records if records[r]] 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): def get_reverse_records_allow_to_edit(self):
records = literal_eval(self.get('reverse_records_allow_edit')) records = self.get('reverse_records_allow_edit')
return [r for r in records if records[r]] r_records = literal_eval(records) if isinstance(records, str) else records
return [r for r in r_records if r_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
class DomainTemplate(db.Model): class DomainTemplate(db.Model):

View File

@ -34,23 +34,22 @@
<tbody> <tbody>
{% for setting in settings %} {% for setting in settings %}
<tr class="odd "> <tr class="odd ">
<td>{{ setting.name }}</td> <td>{{ setting }}</td>
{% if setting.value == "True" or setting.value == "False" %} {% if SETTING.get(setting) in [True, False] %}
<td>{{ setting.value }}</td> <td>{{ SETTING.get(setting)|display_setting_state }}</td>
{% else %}
<td><input name="value" id="value" value="{{ setting.value }}"></td>
{% endif %}
<td width="6%"> <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 }}">
<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 %} </td>
<button type="button" class="btn btn-flat btn-warning setting-save-button" id="{{ setting.name }}"> {% else %}
<td><input name="value" id="value" value="{{ SETTING.get(setting) }}"></td>
<td width="6%">
<button type="button" class="btn btn-flat btn-warning setting-save-button" id="{{ setting }}">
Save&nbsp;<i class="fa fa-info"></i> Save&nbsp;<i class="fa fa-info"></i>
</button> </button>
{% endif %}
</td> </td>
{% endif %}
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>

View File

@ -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_master_name'] = utils.display_master_name
app.jinja_env.filters['display_second_to_time'] = utils.display_time 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['email_to_gravatar_url'] = utils.email_to_gravatar_url
app.jinja_env.filters['display_setting_state'] = utils.display_setting_state
@app.context_processor @app.context_processor
@ -1343,7 +1344,20 @@ def admin_history():
@operator_role_required @operator_role_required
def admin_setting_basic(): def admin_setting_basic():
if request.method == 'GET': 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) return render_template('admin_setting_basic.html', settings=settings)
@ -1398,8 +1412,11 @@ def admin_setting_pdns():
@operator_role_required @operator_role_required
def admin_setting_records(): def admin_setting_records():
if request.method == 'GET': if request.method == 'GET':
f_records = literal_eval(Setting().get('forward_records_allow_edit')) _fr = Setting().get('forward_records_allow_edit')
r_records = literal_eval(Setting().get('reverse_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) return render_template('admin_setting_records.html', f_records=f_records, r_records=r_records)
elif request.method == 'POST': elif request.method == 'POST':
fr = {} fr = {}

View File

@ -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))