Skip to content

Commit f9245e4

Browse files
Fix some more review comments.
1 parent c443529 commit f9245e4

File tree

20 files changed

+145
-111
lines changed

20 files changed

+145
-111
lines changed

web/pgadmin/authenticate/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ def authenticate(self):
285285
source.get_source_name())
286286

287287
status, msg = source.authenticate(self.form)
288-
print(status)
289-
print(msg)
290288

291289
if status:
292290
self.set_current_source(source.get_source_name())
@@ -297,17 +295,6 @@ def authenticate(self):
297295
current_app.logger.debug(
298296
"Authentication initiated via source: %s is failed." %
299297
source.get_source_name())
300-
current_user = User.query.filter_by(username=username,
301-
auth_source=src).first()
302-
# get list with auth source src
303-
# iterate over it and find if user present with that
304-
if len(users) > 0 and current_user:
305-
print('User may be coming first time so continue with all possible')
306-
break
307-
elif len(users) > 0:
308-
print('User already present with other aut source break it')
309-
break
310-
311298
return status, msg
312299

313300
def login(self):

web/pgadmin/browser/static/js/browser.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,26 @@ define('pgadmin.browser', [
299299
},
300300

301301
restore_pgadmin_state: function () {
302-
getApiInstance().get(
302+
getApiInstance({'Content-Encoding': 'gzip'}).get(
303303
url_for('settings.get_application_state')
304304
).then((res)=> {
305305
if(res.data.success && res.data.data.result.length > 0){
306306
_.each(res.data.data.result, function(tool_state){
307307
let tool_name = tool_state.tool_name;
308308
let tool_data = tool_state.tool_data;
309-
let sql_id = `${tool_name}-${getRandomInt(1, 9999999)}`;
309+
let tool_data_id = `${tool_name}-${getRandomInt(1, 9999999)}`;
310310

311311
if (tool_name == 'sqleditor'){
312-
localStorage.setItem(sql_id, tool_data);
313-
showQueryTool.relaunchSqlTool(tool_state, sql_id);
312+
localStorage.setItem(tool_data_id, tool_data);
313+
showQueryTool.relaunchSqlTool(tool_state, tool_data_id);
314314
}else if(tool_name == 'psql'){
315315
pgAdmin.Tools.Psql.openPsqlTool(null, null, tool_state);
316316
}else if(tool_name == 'ERD'){
317-
localStorage.setItem(sql_id, tool_data);
318-
pgAdmin.Tools.ERD.showErdTool(null, null, false, sql_id, tool_state);
317+
localStorage.setItem(tool_data_id, tool_data);
318+
pgAdmin.Tools.ERD.showErdTool(null, null, false, tool_data_id, tool_state);
319319
}else if(tool_name == 'schema_diff'){
320-
localStorage.setItem(sql_id, tool_data);
321-
pgAdmin.Tools.SchemaDiff.launchSchemaDiff(sql_id);
320+
localStorage.setItem(tool_data_id, tool_data);
321+
pgAdmin.Tools.SchemaDiff.launchSchemaDiff(tool_data_id);
322322
}
323323
});
324324

web/pgadmin/preferences/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import json
1717
from flask import render_template, Response, request, session, current_app
1818
from flask_babel import gettext
19+
20+
from pgadmin.settings import delete_tool_data
1921
from pgadmin.user_login_check import pga_login_required
2022
from pgadmin.utils import PgAdminModule
2123
from pgadmin.utils.ajax import success_return, \
@@ -238,6 +240,9 @@ def save():
238240
data['mid'], data['category_id'], data['id'], data['value'])
239241
sgm.get_nodes(sgm)
240242

243+
if data['name'] == 'save_app_state' and not data['value']:
244+
delete_tool_data()
245+
241246
if not res:
242247
return internal_server_error(errormsg=msg)
243248

web/pgadmin/settings/__init__.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,14 @@ def save_application_state():
274274
did: database id
275275
"""
276276
data = json.loads(request.data)
277-
id = data['trans_id']
277+
trans_id = data['trans_id']
278278
fernet = Fernet(current_app.config['SECRET_KEY'].encode())
279279
tool_data = fernet.encrypt(json.dumps(data['tool_data']).encode())
280280
connection_info = data['connection_info'] \
281281
if 'connection_info' in data else None
282282
try:
283283
data_entry = ApplicationState(
284-
uid=current_user.id, id=id,connection_info=connection_info,
284+
uid=current_user.id, id=trans_id,connection_info=connection_info,
285285
tool_name=data['tool_name'], tool_data=tool_data)
286286

287287
db.session.merge(data_entry)
@@ -335,10 +335,16 @@ def delete_application_state():
335335
if request.data:
336336
data = json.loads(request.data)
337337
trans_id = int(data['panelId'].split('_')[-1])
338-
return delete_tool_data(trans_id)
338+
status, msg = delete_tool_data(trans_id)
339+
return make_json_response(
340+
data={
341+
'status': status,
342+
'msg': msg,
343+
}
344+
)
339345

340346

341-
def delete_tool_data(trans_id):
347+
def delete_tool_data(trans_id=None):
342348
try:
343349
if trans_id:
344350
results = db.session \
@@ -354,17 +360,7 @@ def delete_tool_data(trans_id):
354360
for result in results:
355361
db.session.delete(result)
356362
db.session.commit()
357-
return make_json_response(
358-
data={
359-
'status': True,
360-
'msg': 'Success',
361-
}
362-
)
363+
return True, 'Success'
363364
except Exception as e:
364365
db.session.rollback()
365-
return make_json_response(
366-
data={
367-
'status': False,
368-
'msg': str(e),
369-
}
370-
)
366+
return False, str(e)

web/pgadmin/settings/static/ApplicationStateProvider.jsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,50 @@ import React, { useContext, useMemo } from 'react';
1010
import PropTypes from 'prop-types';
1111
import getApiInstance from '../../static/js/api_instance';
1212
import url_for from 'sources/url_for';
13+
import { getBrowser } from '../../static/js/utils';
14+
import usePreferences from '../../preferences/static/js/store';
1315

1416
const ApplicationStateContext = React.createContext();
1517

1618
export const useApplicationState = ()=>useContext(ApplicationStateContext);
1719

18-
export function retrieveDataFromLocalStorgae(sqlId){
19-
let sqlValue = JSON.parse(localStorage.getItem(sqlId));
20-
localStorage.removeItem(sqlId);
21-
return sqlValue;
20+
export function getToolData(localStorageId){
21+
let toolDataJson = JSON.parse(localStorage.getItem(localStorageId));
22+
localStorage.removeItem(localStorageId);
23+
return toolDataJson;
2224
}
2325

26+
27+
2428
export function ApplicationStateProvider({children}){
25-
const saveToolData = (data) =>{
26-
getApiInstance().post(
29+
const preferencesStore = usePreferences();
30+
const save_app_state = preferencesStore?.getPreferencesForModule('misc')?.save_app_state;
31+
const open_new_tab = preferencesStore?.getPreferencesForModule('browser')?.new_browser_tab_open;
32+
33+
const saveToolData = (toolName, connectionInfo, transId, tool_data) =>{
34+
let data = {
35+
'tool_name': toolName,
36+
'connection_info': connectionInfo,
37+
'trans_id': transId,
38+
'tool_data': tool_data
39+
};
40+
getApiInstance({'Content-Encoding': 'gzip'}).post(
2741
url_for('settings.save_application_state'),
2842
JSON.stringify(data),
2943
).catch((error)=>{console.error(error);});
3044
};
3145

46+
const enableSaveToolData = (toolName)=>{
47+
let tool_mapping = {'sqleditor': 'qt', 'schema_diff': 'schema_diff', 'psql': 'psql_tool', 'ERD': 'erd_tool'};
48+
if(open_new_tab?.includes(tool_mapping[toolName])){
49+
return save_app_state && getBrowser().name == 'Electron';
50+
}
51+
return save_app_state;
52+
};
53+
3254
const value = useMemo(()=>({
3355
saveToolData,
56+
enableSaveToolData
3457
}), []);
3558

3659
return <ApplicationStateContext.Provider value={value}>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PropTypes from 'prop-types';
2+
import React from 'react';
3+
import gettext from 'sources/gettext';
4+
5+
export default function ErrorView({error, panelId, panelDocker, pgAdmin, toolName}){
6+
let header = gettext('Error while restore');
7+
let err_msg = gettext(`Unable to restore data for ${toolName} due to ${error}. On clicking the ok button, panel will be closed.`);
8+
pgAdmin.Browser.notifier.alert(header, err_msg, () => {
9+
if (panelId) {
10+
panelDocker.close(panelId, true);
11+
}
12+
});
13+
return <></>;
14+
}
15+
16+
ErrorView.propTypes = {
17+
error: PropTypes.string,
18+
panelId: PropTypes.string,
19+
panelDocker: PropTypes.object,
20+
pgAdmin: PropTypes.object,
21+
toolName: PropTypes.string,
22+
};

web/pgadmin/static/js/custom_hooks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ export function usePrevious(value) {
7878

7979
export function useDelayDebounce(callback, args, delay) {
8080
useEffect(() => {
81+
if (args === undefined || args === null) return;
82+
8183
const delayDebounceFn = setTimeout(() => {
8284
if (args) {
8385
callback(args);
8486
}
8587
}, delay);
8688
return () => clearTimeout(delayDebounceFn);
87-
}, [args]);
89+
}, [args, callback, delay]);
8890
}
8991

9092
export function useOnScreen(ref) {

web/pgadmin/tools/erd/static/js/ERDModule.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ export default class ERDModule {
8787
}
8888

8989
// Callback to draw ERD Tool for objects
90-
showErdTool(_data, treeIdentifier, gen=false, sqlId=null, tooState=null) {
90+
showErdTool(_data, treeIdentifier, gen=false, sqlId=null, toolState=null) {
9191
let parentData = null;
9292
let panelTitle = null;
9393
if(sqlId){
94-
let connection_info = tooState.connection_info;
94+
let connection_info = toolState.connection_info;
9595
panelTitle = connection_info.title;
96+
9697
parentData = {
9798
server_group: {
9899
_id: connection_info.sgid || 0,

web/pgadmin/tools/erd/static/js/erd_tool/components/ERDTool.jsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { styled } from '@mui/material/styles';
3838
import BeforeUnload from './BeforeUnload';
3939
import { isMac } from '../../../../../../static/js/keyboard_shortcuts';
4040
import { downloadBase64UrlData } from '../../../../../../static/js/download_utils';
41-
import { retrieveDataFromLocalStorgae } from '../../../../../../settings/static/ApplicationStateProvider';
41+
import { getToolData } from '../../../../../../settings/static/ApplicationStateProvider';
4242

4343
/* Custom react-diagram action for keyboard events */
4444
export class KeyboardShortcutAction extends Action {
@@ -194,13 +194,11 @@ export default class ERDTool extends React.Component {
194194
},
195195
'linksUpdated': () => {
196196
this.setState({dirty: true});
197-
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true);
198-
this.eventBus.fireEvent(ERD_EVENTS.SAVE_ERD_TOOL_DATA, {'tool_data':this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int)});
197+
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int));
199198
},
200199
'nodesUpdated': ()=>{
201200
this.setState({dirty: true});
202-
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true);
203-
this.eventBus.fireEvent(ERD_EVENTS.SAVE_ERD_TOOL_DATA, {'tool_data':this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int)});
201+
this.eventBus.fireEvent(ERD_EVENTS.DIRTY, true, this.diagram.serialize(this.props.pgAdmin.Browser.utils.app_version_int));
204202
},
205203
'showNote': (event)=>{
206204
this.showNote(event.node);
@@ -357,7 +355,7 @@ export default class ERDTool extends React.Component {
357355

358356

359357
if(this.props.params.sql_id){
360-
let sqlValue = retrieveDataFromLocalStorgae(this.props.params.sql_id);
358+
let sqlValue = getToolData(this.props.params.sql_id);
361359
if (sqlValue) {
362360
this.diagram.deserialize(sqlValue);
363361
}

web/pgadmin/tools/erd/static/js/erd_tool/components/MainToolBar.jsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { MagicIcon, SQLFileIcon } from '../../../../../../static/js/components/E
3838
import { useModal } from '../../../../../../static/js/helpers/ModalProvider';
3939
import { withColorPicker } from '../../../../../../static/js/helpers/withColorPicker';
4040
import { useApplicationState } from '../../../../../../settings/static/ApplicationStateProvider';
41-
import usePreferences from '../../../../../../preferences/static/js/store';
41+
import { useDelayDebounce } from '../../../../../../static/js/custom_hooks';
4242

4343
const StyledBox = styled(Box)(({theme}) => ({
4444
padding: '2px 4px',
@@ -64,8 +64,7 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
6464
});
6565
const [showDetails, setShowDetails] = useState(true);
6666

67-
const {saveToolData} = useApplicationState();
68-
const preferencesStore = usePreferences();
67+
const {saveToolData, enableSaveToolData} = useApplicationState();
6968
const {openMenuName, toggleMenu, onMenuClose} = usePgMenuGroup();
7069
const saveAsMenuRef = React.useRef(null);
7170
const sqlMenuRef = React.useRef(null);
@@ -134,17 +133,12 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
134133
[ERD_EVENTS.ANY_ITEM_SELECTED, (selected)=>{
135134
setDisableButton('drop-table', !selected);
136135
}],
137-
[ERD_EVENTS.DIRTY, (isDirty)=>{
136+
[ERD_EVENTS.DIRTY, (isDirty, data)=>{
137+
const save_app_state = enableSaveToolData('ERD');
138138
isDirtyRef.current = isDirty;
139139
setDisableButton('save', !isDirty);
140-
}],
141-
[ERD_EVENTS.SAVE_ERD_TOOL_DATA, (data)=>{
142-
const save_app_state = preferencesStore?.getPreferencesForModule('misc')?.save_app_state;
143140
if(save_app_state){
144-
saveToolData({'tool_name': 'ERD',
145-
'trans_id': connectionInfo.trans_id,
146-
'connection_info': connectionInfo,
147-
...data});
141+
setSaveData(data);
148142
}
149143
}],
150144
];
@@ -158,6 +152,11 @@ export function MainToolBar({preferences, eventBus, fillColor, textColor, notati
158152
};
159153
}, []);
160154

155+
const [saveData, setSaveData] = useState(null);
156+
useDelayDebounce((saveData)=>{
157+
saveToolData('ERD', connectionInfo, connectionInfo.trans_id, saveData);
158+
}, saveData, 500);
159+
161160
useEffect(()=>{
162161
const showSql = ()=>{
163162
eventBus.fireEvent(ERD_EVENTS.SHOW_SQL, checkedMenuItems['sql_with_drop']);

0 commit comments

Comments
 (0)