1) Fixed import/export servers issue in server mode.

2) Fixed an issue where files are not showing as per the selected format in
   the file dialog when opened the first time.
This commit is contained in:
Akshay Joshi 2022-01-07 21:29:17 +05:30
parent af6ff20020
commit 4ecd05e33b
3 changed files with 60 additions and 20 deletions

View File

@ -1985,7 +1985,7 @@ define([
let response = getFileFormat(this.config.options.allowed_file_types); let response = getFileFormat(this.config.options.allowed_file_types);
let lastSelectedFormat = response.responseJSON.info; let lastSelectedFormat = response.responseJSON.info;
if (_.isUndefined(lastSelectedFormat)) if (_.isUndefined(lastSelectedFormat) || allowed_types.indexOf(lastSelectedFormat) < 0)
set_type = allowed_types[0]; set_type = allowed_types[0];
else else
set_type = lastSelectedFormat; set_type = lastSelectedFormat;

View File

@ -24,6 +24,8 @@ from pgadmin.utils.ajax import make_json_response, internal_server_error
from pgadmin.model import ServerGroup, Server from pgadmin.model import ServerGroup, Server
from pgadmin.utils import clear_database_servers, dump_database_servers,\ from pgadmin.utils import clear_database_servers, dump_database_servers,\
load_database_servers, validate_json_data load_database_servers, validate_json_data
from urllib.parse import unquote
from pgadmin.utils.paths import get_storage_directory
MODULE_NAME = 'import_export_servers' MODULE_NAME = 'import_export_servers'
@ -130,9 +132,20 @@ def load_servers():
if 'filename' in data: if 'filename' in data:
filename = data['filename'] filename = data['filename']
if filename is not None and os.path.exists(filename): file_path = unquote(filename)
# retrieve storage directory path
storage_manager_path = get_storage_directory()
if storage_manager_path:
# generate full path of file
file_path = os.path.join(
storage_manager_path,
file_path.lstrip('/').lstrip('\\')
)
if file_path is not None and os.path.exists(file_path):
try: try:
with open(filename, 'r') as j: with open(file_path, 'r') as j:
data = json.loads(j.read()) data = json.loads(j.read())
# Validate the json file and data # Validate the json file and data

View File

@ -22,8 +22,8 @@ from threading import Lock
from .paths import get_storage_directory from .paths import get_storage_directory
from .preferences import Preferences from .preferences import Preferences
from pgadmin.utils.constants import UTILITIES_ARRAY, USER_NOT_FOUND from pgadmin.utils.constants import UTILITIES_ARRAY, USER_NOT_FOUND
from pgadmin.model import db, User, Version, ServerGroup, Server, \ from pgadmin.model import db, User, ServerGroup, Server
SCHEMA_VERSION as CURRENT_SCHEMA_VERSION from urllib.parse import unquote
ADD_SERVERS_MSG = "Added %d Server Group(s) and %d Server(s)." ADD_SERVERS_MSG = "Added %d Server Group(s) and %d Server(s)."
@ -450,20 +450,36 @@ def dump_database_servers(output_file, selected_servers,
object_dict["Servers"] = server_dict object_dict["Servers"] = server_dict
f = None # retrieve storage directory path
try: storage_manager_path = get_storage_directory()
f = open(output_file, "w")
except Exception as e:
return _handle_error("Error opening output file %s: [%d] %s" %
(output_file, e.errno, e.strerror), from_setup)
try: # generate full path of file
f.write(json.dumps(object_dict, indent=4)) file_path = unquote(output_file)
except Exception as e:
return _handle_error("Error writing output file %s: [%d] %s" %
(output_file, e.errno, e.strerror), from_setup)
f.close() from pgadmin.misc.file_manager import Filemanager
try:
Filemanager.check_access_permission(storage_manager_path, file_path)
except Exception as e:
return _handle_error(str(e), from_setup)
if storage_manager_path is not None:
file_path = os.path.join(
storage_manager_path,
file_path.lstrip('/').lstrip('\\')
)
# write to file
file_content = json.dumps(object_dict, indent=4)
error_str = gettext("Error: {0}")
try:
with open(file_path, 'w') as output_file:
output_file.write(file_content)
except IOError as e:
err_msg = error_str.format(e.strerror)
return _handle_error(err_msg, from_setup)
except Exception as e:
err_msg = error_str.format(e.strerror)
return _handle_error(err_msg, from_setup)
msg = "Configuration for %s servers dumped to %s." % \ msg = "Configuration for %s servers dumped to %s." % \
(servers_dumped, output_file) (servers_dumped, output_file)
@ -541,15 +557,26 @@ def load_database_servers(input_file, selected_servers,
load_user=current_user, from_setup=False): load_user=current_user, from_setup=False):
"""Load server groups and servers. """Load server groups and servers.
""" """
# retrieve storage directory path
storage_manager_path = get_storage_directory()
# generate full path of file
file_path = unquote(input_file)
if storage_manager_path:
# generate full path of file
file_path = os.path.join(
storage_manager_path,
file_path.lstrip('/').lstrip('\\')
)
try: try:
with open(input_file) as f: with open(file_path) as f:
data = json.load(f) data = json.load(f)
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:
return _handle_error("Error parsing input file %s: %s" % return _handle_error("Error parsing input file %s: %s" %
(input_file, e), from_setup) (file_path, e), from_setup)
except Exception as e: except Exception as e:
return _handle_error("Error reading input file %s: [%d] %s" % return _handle_error("Error reading input file %s: [%d] %s" %
(input_file, e.errno, e.strerror), from_setup) (file_path, e.errno, e.strerror), from_setup)
f.close() f.close()