diff --git a/app/models.py b/app/models.py index 8f3a0e8..f142c9d 100644 --- a/app/models.py +++ b/app/models.py @@ -1052,4 +1052,22 @@ class Setting(db.Model): logging.error('Cannot toggle setting %s' % setting) logging.debug(traceback.format_exec()) db.session.rollback() + return False + + def set(self, setting, value): + setting = str(setting) + new_value = str(value) + current_setting = Setting.query.filter(Setting.name==setting).first() + try: + if current_setting: + current_setting.value = new_value + db.session.commit() + return True + else: + logging.error('Setting %s does not exist' % setting) + return False + except: + logging.error('Cannot edit 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 index d8874f0..a758fc6 100644 --- a/app/templates/admin_settings.html +++ b/app/templates/admin_settings.html @@ -33,15 +33,19 @@ {% for setting in settings %} {{ setting.name }} + {% if setting.value == "True" or setting.value == "False" %} {{ setting.value }} + {% else %} + + {% endif %} {% if setting.value == "True" or setting.value == "False" %} {% else %} - {% endif %} @@ -76,10 +80,11 @@ applyChanges('','/admin/setting/' + setting + '/toggle', false, true) }); - // TODO: allow editing of value field - $(".setting-edit-button").click(function() { + $(".setting-save-button").click(function() { var setting = $(this).prop('id'); - applyChanges('','/admin/setting/' + setting + '/edit', false, true) + var value = $(this).parents('tr').find('#value')[0].value; + var postdata = {'value': value}; + applyChanges(postdata, '/admin/setting/' + setting + '/edit', false, true) }); {% endblock %} diff --git a/app/templates/domain.html b/app/templates/domain.html index c958edc..022d004 100644 --- a/app/templates/domain.html +++ b/app/templates/domain.html @@ -132,9 +132,14 @@ "ordering" : true, "info" : true, "autoWidth" : false, + {% if default_record_table_size_setting in ['5','15','20'] %} "lengthMenu": [ [5, 15, 20, -1], [5, 15, 20, "All"]], - "pageLength": 15, + {% else %} + "lengthMenu": [ [5, 15, 20, {{ default_record_table_size_setting }}, -1], + [5, 15, 20, {{ default_record_table_size_setting }}, "All"]], + {% endif %} + "pageLength": {{ default_record_table_size_setting }}, "language": { "lengthMenu": " _MENU_ records" }, diff --git a/app/views.py b/app/views.py index f5fa030..32ef746 100644 --- a/app/views.py +++ b/app/views.py @@ -27,6 +27,11 @@ 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)) +@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) + # START USER AUTHENTICATION HANDLER @app.before_request def before_request(): @@ -508,6 +513,19 @@ def admin_settings_toggle(setting): else: return make_response(jsonify( { 'status': 'error', 'msg': 'Unable to toggle setting.' } ), 500) +@app.route('/admin/setting//edit', methods=['POST']) +@login_required +@admin_role_required +def admin_settings_edit(setting): + pdata = request.data + jdata = json.loads(pdata) + new_value = jdata['value'] + result = Setting().set(setting, new_value) + if (result): + return make_response(jsonify( { 'status': 'ok', 'msg': 'Toggled setting successfully.' } ), 200) + else: + return make_response(jsonify( { 'status': 'error', 'msg': 'Unable to toggle setting.' } ), 500) + @app.route('/user/profile', methods=['GET', 'POST']) @login_required def user_profile(): diff --git a/create_db.py b/create_db.py index 8b9fd03..dc6ff3f 100755 --- a/create_db.py +++ b/create_db.py @@ -12,11 +12,13 @@ user_role = Role('User', 'User') maintenance_setting = Setting('maintenance', 'False') fullscreen_layout_setting = Setting('fullscreen_layout', 'True') record_helper_setting = Setting('record_helper', 'True') +default_table_size_setting = Setting('default_table_size', '15') db.session.add(admin_role) db.session.add(user_role) db.session.add(maintenance_setting) db.session.add(fullscreen_layout_setting) db.session.add(record_helper_setting) +db.session.add(default_table_size_setting) db.session.commit() if not os.path.exists(SQLALCHEMY_MIGRATE_REPO): api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')