From 98ddd7302b6fa441dee6618b073fbcab004aad0b Mon Sep 17 00:00:00 2001 From: Ivan Filippov Date: Fri, 29 Apr 2016 15:36:37 -0600 Subject: [PATCH] Add initial support for PDNS-Admin settings management. --- app/models.py | 19 +++++ app/templates/admin_settings.html | 137 ++++++++++++++++++++++++++++++ app/templates/base.html | 3 +- app/views.py | 23 +++++ create_db.py | 2 + 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 app/templates/admin_settings.html diff --git a/app/models.py b/app/models.py index fe16422..9d0fbc8 100644 --- a/app/models.py +++ b/app/models.py @@ -957,3 +957,22 @@ class Setting(db.Model): db.session.rollback() return False + def toggle(self, setting): + setting = str(setting) + current_setting = Setting.query.filter(Setting.name==setting).first() + 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 %s does not exist' % setting) + return False + except: + logging.error('Cannot toggle setting %s' % setting) + logging.debug(traceback.format_exec()) + db.session.rollback() + return False \ No newline at end of file diff --git a/app/templates/admin_settings.html b/app/templates/admin_settings.html new file mode 100644 index 0000000..c828315 --- /dev/null +++ b/app/templates/admin_settings.html @@ -0,0 +1,137 @@ +{% extends "base.html" %} {% block title %} +DNS Control Panel - Settings +{% endblock %} {% block dashboard_stat %} + +
+

+ Settings PowerDNS-Admin settings +

+ +
+{% endblock %} {% block content %} +
+
+
+
+
+

Settings Management

+
+
+ + + + + + + + + + {% for setting in settings %} + + + + + + {% endfor %} + +
NameValueChange
{{ setting.name }}{{ setting.value }} + {% if setting.value == "True" or setting.value == "False" %} + + {% else %} + + {% endif %} +
+
+ +
+ +
+ +
+ +
+{% endblock %} +{% block extrascripts %} + +{% endblock %} +{% block modals %} + + + + +{% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html index 3df766b..e9ba295 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -38,7 +38,7 @@ {% endblock %} - +
{% block pageheader %}
@@ -128,6 +128,7 @@
  • Admin Console
  • User
  • History
  • +
  • Settings
  • {% endif %} diff --git a/app/views.py b/app/views.py index 04f5916..f269d40 100644 --- a/app/views.py +++ b/app/views.py @@ -11,11 +11,17 @@ from werkzeug import secure_filename from lib import utils from app import app, login_manager from .models import User, Role, Domain, DomainUser, Record, Server, History, Anonymous, Setting +from distutils.util import strtobool jinja2.filters.FILTERS['display_record_name'] = utils.display_record_name jinja2.filters.FILTERS['display_master_name'] = utils.display_master_name jinja2.filters.FILTERS['display_second_to_time'] = utils.display_time +@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)) + # START USER AUTHENTICATION HANDLER @app.before_request def before_request(): @@ -438,6 +444,23 @@ def admin_history(): histories = History.query.all() return render_template('admin_history.html', histories=histories) +@app.route('/admin/settings', methods=['GET']) +@login_required +@admin_role_required +def admin_settings(): + if request.method == 'GET': + settings = Setting.query.filter(Setting.name != 'maintenance') + return render_template('admin_settings.html', settings=settings) + +@app.route('/admin/setting//toggle', methods=['POST']) +@login_required +@admin_role_required +def admin_settings_toggle(setting): + result = Setting().toggle(setting) + if (result): + return make_response(jsonify( { 'status': 'ok', 'msg': 'Toggled setting successfully.' } ), 200) + else: + return make_response(jsonify( { 'status': 'error', 'msg': 'Can toggle setting.' } ), 500) @app.route('/user/profile', methods=['GET', 'POST']) @login_required diff --git a/create_db.py b/create_db.py index f4ea620..7cbce74 100755 --- a/create_db.py +++ b/create_db.py @@ -10,9 +10,11 @@ db.create_all() admin_role = Role('Administrator', 'Administrator') user_role = Role('User', 'User') maintenance_setting = Setting('maintenance', 'False') +fullscreen_layout_setting = Setting('fullscreen_layout', 'True') db.session.add(admin_role) db.session.add(user_role) db.session.add(maintenance_setting) +db.session.add(fullscreen_layout_setting) db.session.commit() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')