2015-02-11 04:00:57 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2016-01-18 08:48:14 -06:00
|
|
|
# Copyright (C) 2013 - 2016, The pgAdmin Development Team
|
2015-02-11 04:00:57 -06:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
"""Utility functions for storing and retrieving user configuration settings."""
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
import traceback
|
2016-06-21 08:12:14 -05:00
|
|
|
|
2016-06-14 09:01:15 -05:00
|
|
|
from flask import Response, request, render_template, url_for
|
2016-06-21 08:12:14 -05:00
|
|
|
from flask.ext.babel import gettext
|
|
|
|
from flask.ext.login import current_user
|
2015-06-29 01:58:41 -05:00
|
|
|
from flask.ext.security import login_required
|
|
|
|
from pgadmin.utils import PgAdminModule
|
2016-06-21 08:12:14 -05:00
|
|
|
from pgadmin.utils.ajax import make_json_response, bad_request
|
2016-06-14 09:01:15 -05:00
|
|
|
from pgadmin.utils.menu import MenuItem
|
2016-06-21 08:12:14 -05:00
|
|
|
|
|
|
|
from pgadmin.model import db, Setting
|
2015-06-29 01:58:41 -05:00
|
|
|
|
|
|
|
MODULE_NAME = 'settings'
|
2015-02-11 04:00:57 -06:00
|
|
|
|
2016-06-14 09:01:15 -05:00
|
|
|
|
|
|
|
class SettingsModule(PgAdminModule):
|
|
|
|
def get_own_javascripts(self):
|
|
|
|
return [{
|
|
|
|
'name': 'pgadmin.settings',
|
|
|
|
'path': url_for('settings.index') + 'settings',
|
|
|
|
'when': None
|
|
|
|
}]
|
|
|
|
|
|
|
|
def get_own_menuitems(self):
|
|
|
|
return {
|
|
|
|
'file_items': [
|
|
|
|
MenuItem(name='mnu_resetlayout',
|
|
|
|
priority=999,
|
|
|
|
module="pgAdmin.Settings",
|
|
|
|
callback='show',
|
|
|
|
icon='fa fa-retweet',
|
|
|
|
label=gettext('Reset Layout'))
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2016-06-14 09:01:15 -05:00
|
|
|
blueprint = SettingsModule(MODULE_NAME, __name__)
|
|
|
|
|
|
|
|
|
2015-02-11 04:00:57 -06:00
|
|
|
def store_setting(setting, value):
|
|
|
|
"""Set a configuration setting for the current user."""
|
|
|
|
data = Setting(user_id=current_user.id, setting=setting, value=value)
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2015-02-11 04:00:57 -06:00
|
|
|
db.session.merge(data)
|
|
|
|
db.session.commit()
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2016-06-14 09:01:15 -05:00
|
|
|
|
2015-02-11 04:00:57 -06:00
|
|
|
def get_setting(setting, default=None):
|
2015-06-29 01:58:41 -05:00
|
|
|
"""Retrieve a configuration setting for the current user, or return the
|
2015-02-11 04:00:57 -06:00
|
|
|
default value specified by the caller."""
|
|
|
|
data = Setting.query.filter_by(user_id=current_user.id, setting=setting).first()
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2015-02-11 04:00:57 -06:00
|
|
|
if not data or data.value is None:
|
|
|
|
return default
|
|
|
|
else:
|
2015-06-29 01:58:41 -05:00
|
|
|
return data.value
|
|
|
|
|
2016-06-14 09:01:15 -05:00
|
|
|
|
|
|
|
@blueprint.route("/")
|
|
|
|
@login_required
|
|
|
|
def index():
|
|
|
|
return bad_request(errormsg=_("This URL can not be called directly."))
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
@blueprint.route("/settings.js")
|
|
|
|
@login_required
|
|
|
|
def script():
|
|
|
|
"""Render the required Javascript"""
|
|
|
|
return Response(response=render_template("settings/settings.js"),
|
|
|
|
status=200,
|
|
|
|
mimetype="application/javascript")
|
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
@blueprint.route("/store", methods=['POST'])
|
|
|
|
@blueprint.route("/store/<setting>/<value>", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def store(setting=None, value=None):
|
|
|
|
"""Store a configuration setting, or if this is a POST request and a
|
|
|
|
count value is present, store multiple settings at once."""
|
|
|
|
success = 1
|
|
|
|
errormsg = ''
|
|
|
|
|
|
|
|
try:
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'count' in request.form:
|
|
|
|
for x in range(int(request.form['count'])):
|
2016-06-21 08:21:06 -05:00
|
|
|
store_setting(request.form['setting%d' % (x + 1)], request.form['value%d' % (x + 1)])
|
2015-06-29 01:58:41 -05:00
|
|
|
else:
|
|
|
|
store_setting(request.form['setting'], request.form['value'])
|
|
|
|
else:
|
|
|
|
store_setting(setting, value)
|
|
|
|
except Exception as e:
|
|
|
|
success = 0
|
|
|
|
errormsg = e.message
|
|
|
|
|
2015-11-13 10:59:16 -06:00
|
|
|
try:
|
|
|
|
info = traceback.format_exc()
|
|
|
|
except Exception as e:
|
|
|
|
info = str(e)
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
return make_json_response(success=success,
|
|
|
|
errormsg=errormsg,
|
2015-11-13 10:59:16 -06:00
|
|
|
info=info,
|
2015-06-29 01:58:41 -05:00
|
|
|
result=request.form)
|
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
@blueprint.route("/get", methods=['POST'])
|
|
|
|
@blueprint.route("/get/<setting>", methods=['GET'])
|
|
|
|
@blueprint.route("/get/<setting>/<default>", methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def get(setting=None, default=None):
|
|
|
|
"""Get a configuration setting."""
|
|
|
|
if request.method == 'POST':
|
|
|
|
setting = request.form['setting']
|
|
|
|
default = request.form['default']
|
|
|
|
|
|
|
|
success = 1
|
|
|
|
errormsg = ''
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = get_setting(setting, default)
|
|
|
|
except Exception as e:
|
|
|
|
success = 0
|
|
|
|
errormsg = e.message
|
|
|
|
|
2015-11-13 10:59:16 -06:00
|
|
|
try:
|
|
|
|
info = traceback.format_exc()
|
|
|
|
except Exception as e:
|
|
|
|
info = str(e)
|
|
|
|
|
2015-06-29 01:58:41 -05:00
|
|
|
return make_json_response(success=success,
|
|
|
|
errormsg=errormsg,
|
2015-11-13 10:59:16 -06:00
|
|
|
info=info,
|
2015-06-29 01:58:41 -05:00
|
|
|
result=request.form)
|
2016-06-10 10:55:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/reset_layout", methods=['DELETE'])
|
|
|
|
@login_required
|
|
|
|
def reset_layout():
|
|
|
|
"""Reset configuration setting"""
|
2016-06-14 09:01:15 -05:00
|
|
|
|
|
|
|
# There can be only one record at most
|
|
|
|
data = Setting.query.filter_by(user_id=current_user.id).first()
|
|
|
|
try:
|
|
|
|
if data is not None:
|
|
|
|
db.session.delete(data)
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
return make_json_response(
|
2016-06-21 08:21:06 -05:00
|
|
|
status=410, success=0, errormsg=str(e)
|
|
|
|
)
|
2016-06-10 10:55:58 -05:00
|
|
|
|
|
|
|
return make_json_response(result=request.form)
|