Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3df279

Browse files
committed
add confirm message if not saved when switch to webdav mode
reload script from database when leaving webdav mode
1 parent 2c21d1d commit a3df279

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

pyspider/webui/debug.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ def save(project):
206206
return 'ok', 200
207207

208208

209+
@app.route('/debug/<project>/get')
210+
def get_script(project):
211+
if not verify_project_name(project):
212+
return 'project name is not allowed!', 400
213+
projectdb = app.config['projectdb']
214+
info = projectdb.get(project, fields=['name', 'script'])
215+
return json.dumps(utils.unicode_obj(info)), \
216+
200, {'Content-Type': 'application/json'}
217+
218+
209219
@app.route('/helper.js')
210220
def resizer_js():
211221
host = request.headers['Host']

pyspider/webui/static/debug.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ window.SelectorHelper = (function() {
1212
features.forEach(function(f) {
1313
if (f.selected)
1414
element_name += f.name;
15-
})
15+
});
1616
if (element_name === '') {
1717
return p.tag;
1818
}
@@ -586,15 +586,39 @@ window.Debugger = (function() {
586586

587587
webdav_mode: false,
588588
toggle_webdav_mode: function(button) {
589-
this.webdav_mode = !this.webdav_mode;
590-
if (this.webdav_mode) {
589+
if (!this.webdav_mode) {
590+
if (this.not_saved) {
591+
if (!confirm("You have not saved changes. Ignore changes and switch to WebDav mode.")) {
592+
return;
593+
}
594+
this.not_saved = false;
595+
}
591596
this.python_editor_elem.hide();
592597
this.splitter.trigger('fullsize', 'prev');
593598
$(button).addClass('active');
599+
this.webdav_mode = !this.webdav_mode;
594600
} else {
595-
this.python_editor_elem.show();
596-
this.splitter.trigger('init');
597-
$(button).removeClass('active');
601+
// leaving webdav mode, reload script
602+
var _this = this;
603+
$.ajax({
604+
type: "GET",
605+
url: location.pathname + '/get',
606+
success: function (data) {
607+
_this.splitter.trigger('init');
608+
_this.python_editor_elem.show();
609+
_this.python_editor.setValue(data.script);
610+
_this.not_saved = false;
611+
$(button).removeClass('active');
612+
_this.webdav_mode = !_this.webdav_mode;
613+
},
614+
error: function() {
615+
alert('Loading script from database error. Script may out-of-date.');
616+
_this.python_editor_elem.show();
617+
_this.splitter.trigger('init');
618+
$(button).removeClass('active');
619+
_this.webdav_mode = !_this.webdav_mode;
620+
},
621+
});
598622
}
599623
},
600624
};

tests/test_webui.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_35_run_http_task(self):
153153
})
154154
self.assertEqual(rv.status_code, 200)
155155
data = json.loads(utils.text(rv.data))
156-
self.assertIn(b'follows', rv.data)
156+
self.assertIn('follows', data)
157157

158158
def test_40_save(self):
159159
rv = self.app.post('/debug/test_project/save', data={
@@ -162,6 +162,13 @@ def test_40_save(self):
162162
self.assertEqual(rv.status_code, 200)
163163
self.assertIn(b'ok', rv.data)
164164

165+
def test_42_get(self):
166+
rv = self.app.get('/debug/test_project/get')
167+
self.assertEqual(rv.status_code, 200)
168+
data = json.loads(utils.text(rv.data))
169+
self.assertIn('script', data)
170+
self.assertEqual(data['script'], self.script_content)
171+
165172
def test_45_run_with_saved_script(self):
166173
rv = self.app.post('/debug/test_project/run', data={
167174
'webdav_mode': 'true',

0 commit comments

Comments
 (0)