2015-02-11 04:00:57 -06:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2022-01-04 02:24:25 -06:00
|
|
|
# Copyright (C) 2013 - 2022, 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
|
2018-10-08 04:03:19 -05:00
|
|
|
import json
|
2016-06-21 08:12:14 -05:00
|
|
|
|
2018-10-08 04:03:19 -05:00
|
|
|
from flask import Response, request, render_template, url_for, current_app
|
2021-11-24 05:52:57 -06:00
|
|
|
from flask_babel import gettext
|
2016-07-22 10:25:23 -05:00
|
|
|
from flask_login import current_user
|
|
|
|
from flask_security import login_required
|
2015-06-29 01:58:41 -05:00
|
|
|
from pgadmin.utils import PgAdminModule
|
2018-10-08 04:03:19 -05:00
|
|
|
from pgadmin.utils.ajax import make_json_response, bad_request,\
|
|
|
|
success_return, internal_server_error
|
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
|
2020-08-20 09:56:51 -05:00
|
|
|
from pgadmin.utils.constants import MIMETYPE_APP_JS
|
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': [
|
2018-02-19 05:12:35 -06:00
|
|
|
MenuItem(
|
|
|
|
name='mnu_resetlayout',
|
2019-05-31 10:51:30 -05:00
|
|
|
priority=998,
|
2018-02-19 05:12:35 -06:00
|
|
|
module="pgAdmin.Settings",
|
|
|
|
callback='show',
|
|
|
|
icon='fa fa-retweet',
|
|
|
|
label=gettext('Reset Layout')
|
|
|
|
)
|
2016-06-14 09:01:15 -05:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-06-12 01:31:22 -05:00
|
|
|
def get_exposed_url_endpoints(self):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
list: a list of url endpoints exposed to the client.
|
|
|
|
"""
|
2018-02-19 05:12:35 -06:00
|
|
|
return [
|
2018-10-08 04:03:19 -05:00
|
|
|
'settings.store', 'settings.store_bulk', 'settings.reset_layout',
|
2019-02-14 03:18:08 -06:00
|
|
|
'settings.save_tree_state', 'settings.get_tree_state',
|
2021-04-08 01:53:57 -05:00
|
|
|
'settings.reset_tree_state',
|
|
|
|
'settings.save_file_format_setting',
|
|
|
|
'settings.get_file_format_setting'
|
2018-02-19 05:12:35 -06:00
|
|
|
]
|
2017-06-12 01:31:22 -05:00
|
|
|
|
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
|
|
|
|
2019-03-26 10:08:45 -05:00
|
|
|
def get_setting(setting, default=''):
|
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."""
|
2018-02-19 05:12:35 -06:00
|
|
|
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():
|
2020-04-08 03:44:25 -05:00
|
|
|
return bad_request(errormsg=gettext("This URL cannot 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,
|
2020-08-20 09:56:51 -05:00
|
|
|
mimetype=MIMETYPE_APP_JS)
|
2015-06-29 01:58:41 -05:00
|
|
|
|
2016-06-21 08:21:06 -05:00
|
|
|
|
2017-06-12 01:31:22 -05:00
|
|
|
@blueprint.route("/store", methods=['POST'], endpoint='store_bulk')
|
|
|
|
@blueprint.route("/store/<setting>/<value>", methods=['PUT'], endpoint='store')
|
2015-06-29 01:58:41 -05:00
|
|
|
@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'])):
|
2018-02-19 05:12:35 -06: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
|
|
|
|
2017-06-15 22:27:23 -05:00
|
|
|
@blueprint.route("/layout", methods=['DELETE'], endpoint='reset_layout')
|
2016-06-10 10:55:58 -05:00
|
|
|
@login_required
|
|
|
|
def reset_layout():
|
|
|
|
"""Reset configuration setting"""
|
2016-06-14 09:01:15 -05:00
|
|
|
|
|
|
|
try:
|
2019-03-26 10:08:45 -05:00
|
|
|
db.session.query(Setting) \
|
|
|
|
.filter(Setting.user_id == current_user.id)\
|
|
|
|
.filter((Setting.setting == 'Browser/Layout') |
|
|
|
|
(Setting.setting == 'SQLEditor/Layout') |
|
|
|
|
(Setting.setting == 'Debugger/Layout'))\
|
|
|
|
.delete()
|
|
|
|
|
|
|
|
db.session.commit()
|
2016-06-14 09:01:15 -05:00
|
|
|
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)
|
2018-10-08 04:03:19 -05:00
|
|
|
|
|
|
|
|
2019-02-14 03:22:22 -06:00
|
|
|
@blueprint.route("/reset_tree_state", methods=['DELETE'],
|
|
|
|
endpoint='reset_tree_state')
|
2019-02-14 03:18:08 -06:00
|
|
|
@login_required
|
|
|
|
def reset_tree_state():
|
|
|
|
"""Reset the saved tree state."""
|
|
|
|
|
2019-02-14 03:22:22 -06:00
|
|
|
data = Setting.query.filter_by(user_id=current_user.id,
|
|
|
|
setting='browser_tree_state').first()
|
2019-02-14 03:18:08 -06:00
|
|
|
try:
|
|
|
|
if data is not None:
|
|
|
|
db.session.delete(data)
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as e:
|
|
|
|
return make_json_response(
|
|
|
|
status=410, success=0, errormsg=str(e)
|
|
|
|
)
|
|
|
|
|
|
|
|
return success_return()
|
|
|
|
|
|
|
|
|
2018-10-08 04:03:19 -05:00
|
|
|
@blueprint.route("/save_tree_state/", endpoint="save_tree_state",
|
|
|
|
methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def save_browser_tree_state():
|
|
|
|
"""Save the browser tree state."""
|
|
|
|
data = request.form if request.form else request.data.decode('utf-8')
|
|
|
|
old_data = get_setting('browser_tree_state')
|
|
|
|
|
|
|
|
if old_data and old_data != 'null':
|
|
|
|
if data:
|
|
|
|
data = json.loads(data)
|
|
|
|
|
|
|
|
old_data = json.loads(old_data)
|
|
|
|
|
|
|
|
old_data.update(data)
|
|
|
|
new_data = json.dumps(old_data)
|
|
|
|
else:
|
|
|
|
new_data = data
|
|
|
|
|
|
|
|
try:
|
|
|
|
store_setting('browser_tree_state', new_data)
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return internal_server_error(errormsg=str(e))
|
|
|
|
|
|
|
|
return success_return()
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/get_tree_state/", endpoint="get_tree_state",
|
|
|
|
methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def get_browser_tree_state():
|
|
|
|
"""Get the browser tree state."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = get_setting('browser_tree_state')
|
|
|
|
except Exception as e:
|
|
|
|
current_app.logger.exception(e)
|
|
|
|
return internal_server_error(errormsg=str(e))
|
|
|
|
|
|
|
|
return Response(response=data,
|
|
|
|
status=200,
|
|
|
|
mimetype="application/json")
|
2021-04-08 01:53:57 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _get_dialog_type(file_type):
|
|
|
|
"""
|
|
|
|
This function return dialog type
|
|
|
|
:param file_type:
|
|
|
|
:return: dialog type.
|
|
|
|
"""
|
|
|
|
if 'pgerd' in file_type:
|
|
|
|
return 'erd_file_type'
|
|
|
|
elif 'backup' in file_type:
|
|
|
|
return 'backup_file_type'
|
|
|
|
elif 'csv' in file_type and 'txt' in file_type:
|
|
|
|
return 'import_export_file_type'
|
|
|
|
elif 'csv' in file_type and 'txt' not in file_type:
|
|
|
|
return 'storage_manager_file_type'
|
|
|
|
else:
|
|
|
|
return 'sqleditor_file_format'
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/save_file_format_setting/",
|
|
|
|
endpoint="save_file_format_setting",
|
|
|
|
methods=['POST'])
|
|
|
|
@login_required
|
|
|
|
def save_file_format_setting():
|
|
|
|
"""
|
|
|
|
This function save the selected file format.
|
|
|
|
:return: save file format response
|
|
|
|
"""
|
|
|
|
data = request.form if request.form else json.loads(
|
|
|
|
request.data.decode('utf-8'))
|
|
|
|
file_type = _get_dialog_type(data['allowed_file_types'])
|
|
|
|
|
|
|
|
store_setting(file_type, data['selectedFormat'])
|
|
|
|
return make_json_response(success=True,
|
|
|
|
info=data,
|
|
|
|
result=request.form)
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/get_file_format_setting/",
|
|
|
|
endpoint="get_file_format_setting",
|
|
|
|
methods=['GET'])
|
|
|
|
@login_required
|
|
|
|
def get_file_format_setting():
|
|
|
|
"""
|
|
|
|
This function return the last selected file format
|
|
|
|
:return: last selected file format
|
|
|
|
"""
|
|
|
|
data = dict()
|
|
|
|
for k, v in request.args.items():
|
|
|
|
try:
|
|
|
|
data[k] = json.loads(v, encoding='utf-8')
|
|
|
|
except (ValueError, TypeError, KeyError):
|
|
|
|
data[k] = v
|
|
|
|
|
|
|
|
file_type = _get_dialog_type(list(data.values()))
|
|
|
|
|
|
|
|
data = Setting.query.filter_by(
|
|
|
|
user_id=current_user.id, setting=file_type).first()
|
|
|
|
if data is None:
|
|
|
|
return make_json_response(success=True, info='*')
|
|
|
|
else:
|
|
|
|
return make_json_response(success=True, info=data.value)
|