MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / load_servers

Function load_servers

web/pgadmin/tools/import_export_servers/__init__.py:96–156  ·  view source on GitHub ↗

This function is used to load the servers from the json file.

()

Source from the content-addressed store, hash-verified

94@blueprint.route('/load_servers', methods=['POST'], endpoint='load_servers')
95@pga_login_required
96def load_servers():
97 """
98 This function is used to load the servers from the json file.
99 """
100 filename = None
101 groups = {}
102 all_servers = []
103
104 data = request.form if request.form else json.loads(request.data.decode())
105 if 'filename' in data:
106 filename = data['filename']
107
108 file_path = unquote(filename)
109
110 try:
111 file_path = filename_with_file_manager_path(file_path)
112 except PermissionError as e:
113 return unauthorized(errormsg=str(e))
114 except Exception as e:
115 return bad_request(errormsg=str(e))
116
117 if file_path and os.path.exists(file_path):
118 try:
119 with open(file_path, 'r') as j:
120 data = json.loads(j.read())
121
122 # Validate the json file and data
123 errmsg = validate_json_data(
124 data, current_user.has_role("Administrator"))
125 if errmsg is not None:
126 return internal_server_error(errmsg)
127
128 if 'Servers' in data:
129 for server in data["Servers"]:
130 obj = data["Servers"][server]
131 server_id = server + '_' + str(
132 secrets.choice(range(1, 9999)))
133
134 if obj['Group'] in groups:
135 groups[obj['Group']]['children'].append(
136 {'value': server_id,
137 'label': obj['Name']})
138 else:
139 groups[obj['Group']] = \
140 {'value': obj['Group'], 'label': obj['Group'],
141 'children': [{
142 'value': server_id,
143 'label': obj['Name']}]}
144 else:
145 return internal_server_error(
146 _('The specified file is not in the correct format.'))
147
148 for item in groups:
149 all_servers.append(groups[item])
150 except Exception:
151 return internal_server_error(
152 _('Unable to load the specified file.'))
153 else:

Callers

nothing calls this directly

Calls 9

unauthorizedFunction · 0.90
bad_requestFunction · 0.90
validate_json_dataFunction · 0.90
internal_server_errorFunction · 0.90
make_json_responseFunction · 0.90
decodeMethod · 0.80
appendMethod · 0.80
existsMethod · 0.45

Tested by

no test coverage detected