##// END OF EJS Templates
redirect url after notebook rename
Zachary Sailer -
Show More
@@ -1,105 +1,104 b''
1 """Tornado handlers for the notebooks web service.
1 """Tornado handlers for the notebooks web service.
2
2
3 Authors:
3 Authors:
4
4
5 * Zach Sailer
5 * Zach Sailer
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2008-2011 The IPython Development Team
9 # Copyright (C) 2008-2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from tornado import web
19 from tornado import web
20
20
21 from zmq.utils import jsonapi
21 from zmq.utils import jsonapi
22
22
23 from IPython.utils.jsonutil import date_default
23 from IPython.utils.jsonutil import date_default
24
25 from ...base.handlers import IPythonHandler, authenticate_unless_readonly
24 from ...base.handlers import IPythonHandler, authenticate_unless_readonly
26
25
27 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
28 # Session web service handlers
27 # Session web service handlers
29 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
30
29
31
30
32
31
33 class SessionRootHandler(IPythonHandler):
32 class SessionRootHandler(IPythonHandler):
34
33
35 @authenticate_unless_readonly
34 @authenticate_unless_readonly
36 def get(self):
35 def get(self):
37 sm = self.session_manager
36 sm = self.session_manager
38 nbm = self.notebook_manager
37 nbm = self.notebook_manager
39 km = self.kernel_manager
38 km = self.kernel_manager
40 sessions = sm.list_sessions()
39 sessions = sm.list_sessions()
41 self.finish(jsonapi.dumps(sessions))
40 self.finish(jsonapi.dumps(sessions))
42
41
43 @web.authenticated
42 @web.authenticated
44 def post(self):
43 def post(self):
45 sm = self.session_manager
44 sm = self.session_manager
46 nbm = self.notebook_manager
45 nbm = self.notebook_manager
47 km = self.kernel_manager
46 km = self.kernel_manager
48 notebook_path = self.get_argument('notebook_path', default=None)
47 notebook_path = self.get_argument('notebook_path', default=None)
49 notebook_name, path = nbm.named_notebook_path(notebook_path)
48 notebook_name, path = nbm.named_notebook_path(notebook_path)
50 session_id, model = sm.get_session(notebook_name, path)
49 session_id, model = sm.get_session(notebook_name, path)
51 if model == None:
50 if model == None:
52 kernel_id = km.start_kernel()
51 kernel_id = km.start_kernel()
53 kernel = km.kernel_model(kernel_id, self.ws_url)
52 kernel = km.kernel_model(kernel_id, self.ws_url)
54 model = sm.session_model(session_id, notebook_name, path, kernel)
53 model = sm.session_model(session_id, notebook_name, path, kernel)
55 self.finish(jsonapi.dumps(model))
54 self.finish(jsonapi.dumps(model))
56
55
57 class SessionHandler(IPythonHandler):
56 class SessionHandler(IPythonHandler):
58
57
59 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
58 SUPPORTED_METHODS = ('GET', 'PATCH', 'DELETE')
60
59
61 @authenticate_unless_readonly
60 @authenticate_unless_readonly
62 def get(self, session_id):
61 def get(self, session_id):
63 sm = self.session_manager
62 sm = self.session_manager
64 model = sm.get_session_from_id(session_id)
63 model = sm.get_session_from_id(session_id)
65 self.finish(jsonapi.dumps(model))
64 self.finish(jsonapi.dumps(model))
66
65
67 @web.authenticated
66 @web.authenticated
68 def patch(self, session_id):
67 def patch(self, session_id):
69 sm = self.session_manager
68 sm = self.session_manager
70 nbm = self.notebook_manager
69 nbm = self.notebook_manager
71 km = self.kernel_manager
70 km = self.kernel_manager
72 notebook_path = self.request.body
71 notebook_path = self.request.body
73 notebook_name, path = nbm.named_notebook_path(notebook_path)
72 notebook_name, path = nbm.named_notebook_path(notebook_path)
74 kernel_id = sm.get_kernel_from_session(session_id)
73 kernel_id = sm.get_kernel_from_session(session_id)
75 kernel = km.kernel_model(kernel_id, self.ws_url)
74 kernel = km.kernel_model(kernel_id, self.ws_url)
76 sm.delete_mapping_for_session(session_id)
75 sm.delete_mapping_for_session(session_id)
77 model = sm.session_model(session_id, notebook_name, path, kernel)
76 model = sm.session_model(session_id, notebook_name, path, kernel)
78 self.finish(jsonapi.dumps(model))
77 self.finish(jsonapi.dumps(model))
79
78
80 @web.authenticated
79 @web.authenticated
81 def delete(self, session_id):
80 def delete(self, session_id):
82 sm = self.session_manager
81 sm = self.session_manager
83 nbm = self.notebook_manager
82 nbm = self.notebook_manager
84 km = self.kernel_manager
83 km = self.kernel_manager
85 kernel_id = sm.get_kernel_from_session(session_id)
84 kernel_id = sm.get_kernel_from_session(session_id)
86 km.shutdown_kernel(kernel_id)
85 km.shutdown_kernel(kernel_id)
87 sm.delete_mapping_for_session(session_id)
86 sm.delete_mapping_for_session(session_id)
88 self.set_status(204)
87 self.set_status(204)
89 self.finish()
88 self.finish()
90
89
91
90
92 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
93 # URL to handler mappings
92 # URL to handler mappings
94 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
95
94
96 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
95 _session_id_regex = r"(?P<session_id>\w+-\w+-\w+-\w+-\w+)"
97
96
98 default_handlers = [
97 default_handlers = [
99 (r"api/sessions/%s" % _session_id_regex, SessionHandler),
98 (r"api/sessions/%s" % _session_id_regex, SessionHandler),
100 (r"api/sessions", SessionRootHandler)
99 (r"api/sessions", SessionRootHandler)
101 ]
100 ]
102
101
103
102
104
103
105
104
@@ -1,161 +1,166 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2008-2011 The IPython Development Team
2 // Copyright (C) 2008-2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // SaveWidget
9 // SaveWidget
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var utils = IPython.utils;
15 var utils = IPython.utils;
16
16
17 var SaveWidget = function (selector) {
17 var SaveWidget = function (selector) {
18 this.selector = selector;
18 this.selector = selector;
19 if (this.selector !== undefined) {
19 if (this.selector !== undefined) {
20 this.element = $(selector);
20 this.element = $(selector);
21 this.style();
21 this.style();
22 this.bind_events();
22 this.bind_events();
23 }
23 }
24 };
24 };
25
25
26
26
27 SaveWidget.prototype.style = function () {
27 SaveWidget.prototype.style = function () {
28 };
28 };
29
29
30
30
31 SaveWidget.prototype.bind_events = function () {
31 SaveWidget.prototype.bind_events = function () {
32 var that = this;
32 var that = this;
33 this.element.find('span#notebook_name').click(function () {
33 this.element.find('span#notebook_name').click(function () {
34 that.rename_notebook();
34 that.rename_notebook();
35 });
35 });
36 this.element.find('span#notebook_name').hover(function () {
36 this.element.find('span#notebook_name').hover(function () {
37 $(this).addClass("ui-state-hover");
37 $(this).addClass("ui-state-hover");
38 }, function () {
38 }, function () {
39 $(this).removeClass("ui-state-hover");
39 $(this).removeClass("ui-state-hover");
40 });
40 });
41 $([IPython.events]).on('notebook_loaded.Notebook', function () {
41 $([IPython.events]).on('notebook_loaded.Notebook', function () {
42 that.update_notebook_name();
42 that.update_notebook_name();
43 that.update_document_title();
43 that.update_document_title();
44 });
44 });
45 $([IPython.events]).on('notebook_saved.Notebook', function () {
45 $([IPython.events]).on('notebook_saved.Notebook', function () {
46 that.update_notebook_name();
46 that.update_notebook_name();
47 that.update_document_title();
47 that.update_document_title();
48 });
48 });
49 $([IPython.events]).on('notebook_renamed.Notebook', function () {
49 $([IPython.events]).on('notebook_renamed.Notebook', function () {
50 that.update_notebook_name();
50 that.update_address_bar();
51 that.update_document_title();
52 });
51 });
53 $([IPython.events]).on('notebook_save_failed.Notebook', function () {
52 $([IPython.events]).on('notebook_save_failed.Notebook', function () {
54 that.set_save_status('Autosave Failed!');
53 that.set_save_status('Autosave Failed!');
55 });
54 });
56 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
55 $([IPython.events]).on('checkpoints_listed.Notebook', function (event, data) {
57 that.set_last_checkpoint(data[0]);
56 that.set_last_checkpoint(data[0]);
58 });
57 });
59
58
60 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
59 $([IPython.events]).on('checkpoint_created.Notebook', function (event, data) {
61 that.set_last_checkpoint(data);
60 that.set_last_checkpoint(data);
62 });
61 });
63 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
62 $([IPython.events]).on('set_dirty.Notebook', function (event, data) {
64 that.set_autosaved(data.value);
63 that.set_autosaved(data.value);
65 });
64 });
66 };
65 };
67
66
68
67
69 SaveWidget.prototype.rename_notebook = function () {
68 SaveWidget.prototype.rename_notebook = function () {
70 var that = this;
69 var that = this;
71 var dialog = $('<div/>').append(
70 var dialog = $('<div/>').append(
72 $("<p/>").addClass("rename-message")
71 $("<p/>").addClass("rename-message")
73 .html('Enter a new notebook name:')
72 .html('Enter a new notebook name:')
74 ).append(
73 ).append(
75 $("<br/>")
74 $("<br/>")
76 ).append(
75 ).append(
77 $('<input/>').attr('type','text').attr('size','25')
76 $('<input/>').attr('type','text').attr('size','25')
78 .val(IPython.notebook.get_notebook_name())
77 .val(IPython.notebook.get_notebook_name())
79 );
78 );
80 IPython.dialog.modal({
79 IPython.dialog.modal({
81 title: "Rename Notebook",
80 title: "Rename Notebook",
82 body: dialog,
81 body: dialog,
83 buttons : {
82 buttons : {
84 "Cancel": {},
83 "Cancel": {},
85 "OK": {
84 "OK": {
86 class: "btn-primary",
85 class: "btn-primary",
87 click: function () {
86 click: function () {
88 var new_name = $(this).find('input').val();
87 var new_name = $(this).find('input').val();
89 if (!IPython.notebook.test_notebook_name(new_name)) {
88 if (!IPython.notebook.test_notebook_name(new_name)) {
90 $(this).find('.rename-message').html(
89 $(this).find('.rename-message').html(
91 "Invalid notebook name. Notebook names must "+
90 "Invalid notebook name. Notebook names must "+
92 "have 1 or more characters and can contain any characters " +
91 "have 1 or more characters and can contain any characters " +
93 "except :/\\. Please enter a new notebook name:"
92 "except :/\\. Please enter a new notebook name:"
94 );
93 );
95 return false;
94 return false;
96 } else {
95 } else {
97 IPython.notebook.notebook_rename(new_name);
96 IPython.notebook.notebook_rename(new_name);
98 }
97 }
99 }}
98 }}
100 },
99 },
101 open : function (event, ui) {
100 open : function (event, ui) {
102 var that = $(this);
101 var that = $(this);
103 // Upon ENTER, click the OK button.
102 // Upon ENTER, click the OK button.
104 that.find('input[type="text"]').keydown(function (event, ui) {
103 that.find('input[type="text"]').keydown(function (event, ui) {
105 if (event.which === utils.keycodes.ENTER) {
104 if (event.which === utils.keycodes.ENTER) {
106 that.find('.btn-primary').first().click();
105 that.find('.btn-primary').first().click();
107 return false;
106 return false;
108 }
107 }
109 });
108 });
110 that.find('input[type="text"]').focus().select();
109 that.find('input[type="text"]').focus().select();
111 }
110 }
112 });
111 });
113 }
112 }
114
113
115
114
116 SaveWidget.prototype.update_notebook_name = function () {
115 SaveWidget.prototype.update_notebook_name = function () {
117 var nbname = IPython.notebook.get_notebook_name();
116 var nbname = IPython.notebook.get_notebook_name();
118 this.element.find('span#notebook_name').html(nbname);
117 this.element.find('span#notebook_name').html(nbname);
119 };
118 };
120
119
121
120
122 SaveWidget.prototype.update_document_title = function () {
121 SaveWidget.prototype.update_document_title = function () {
123 var nbname = IPython.notebook.get_notebook_name();
122 var nbname = IPython.notebook.get_notebook_name();
124 document.title = nbname;
123 document.title = nbname;
125 };
124 };
125
126 SaveWidget.prototype.update_address_bar = function(){
127 var nbname = IPython.notebook.notebook_name;
128 var path = IPython.notebook.notebookPath();
129 window.location = path + nbname;
130 }
126
131
127
132
128 SaveWidget.prototype.set_save_status = function (msg) {
133 SaveWidget.prototype.set_save_status = function (msg) {
129 this.element.find('span#autosave_status').html(msg);
134 this.element.find('span#autosave_status').html(msg);
130 }
135 }
131
136
132 SaveWidget.prototype.set_checkpoint_status = function (msg) {
137 SaveWidget.prototype.set_checkpoint_status = function (msg) {
133 this.element.find('span#checkpoint_status').html(msg);
138 this.element.find('span#checkpoint_status').html(msg);
134 }
139 }
135
140
136 SaveWidget.prototype.set_last_checkpoint = function (checkpoint) {
141 SaveWidget.prototype.set_last_checkpoint = function (checkpoint) {
137 if (!checkpoint) {
142 if (!checkpoint) {
138 this.set_checkpoint_status("");
143 this.set_checkpoint_status("");
139 return;
144 return;
140 }
145 }
141 var d = new Date(checkpoint.last_modified);
146 var d = new Date(checkpoint.last_modified);
142 this.set_checkpoint_status(
147 this.set_checkpoint_status(
143 "Last Checkpoint: " + d.format('mmm dd HH:MM')
148 "Last Checkpoint: " + d.format('mmm dd HH:MM')
144 );
149 );
145 }
150 }
146
151
147 SaveWidget.prototype.set_autosaved = function (dirty) {
152 SaveWidget.prototype.set_autosaved = function (dirty) {
148 if (dirty) {
153 if (dirty) {
149 this.set_save_status("(unsaved changes)");
154 this.set_save_status("(unsaved changes)");
150 } else {
155 } else {
151 this.set_save_status("(autosaved)");
156 this.set_save_status("(autosaved)");
152 }
157 }
153 };
158 };
154
159
155
160
156 IPython.SaveWidget = SaveWidget;
161 IPython.SaveWidget = SaveWidget;
157
162
158 return IPython;
163 return IPython;
159
164
160 }(IPython));
165 }(IPython));
161
166
@@ -1,105 +1,105 b''
1 """Tornado handlers for the tree view.
1 """Tornado handlers for the tree view.
2
2
3 Authors:
3 Authors:
4
4
5 * Brian Granger
5 * Brian Granger
6 """
6 """
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2011 The IPython Development Team
9 # Copyright (C) 2011 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from tornado import web
19 from tornado import web
20 from ..base.handlers import IPythonHandler
20 from ..base.handlers import IPythonHandler
21 from urllib import quote, unquote
21 from urllib import quote, unquote
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Handlers
24 # Handlers
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27
27
28 class ProjectDashboardHandler(IPythonHandler):
28 class ProjectDashboardHandler(IPythonHandler):
29
29
30 @web.authenticated
30 @web.authenticated
31 def get(self):
31 def get(self):
32 self.write(self.render_template('tree.html',
32 self.write(self.render_template('tree.html',
33 project=self.project,
33 project=self.project,
34 project_component=self.project.split('/'),
34 project_component=self.project.split('/'),
35 notebook_path= "''"
35 notebook_path= "''"
36 ))
36 ))
37
37
38
38
39 class ProjectPathDashboardHandler(IPythonHandler):
39 class ProjectPathDashboardHandler(IPythonHandler):
40
40
41 @authenticate_unless_readonly
41 @authenticate_unless_readonly
42 def get(self, notebook_path):
42 def get(self, notebook_path):
43 nbm = self.notebook_manager
43 nbm = self.notebook_manager
44 name, path = nbm.named_notebook_path(notebook_path)
44 name, path = nbm.named_notebook_path(notebook_path)
45 if name != None:
45 if name != None:
46 if path == None:
46 if path == None:
47 self.redirect(self.base_project_url + 'notebooks/' + quote(name))
47 self.redirect(self.base_project_url + 'notebooks/' + quote(name))
48 else:
48 else:
49 self.redirect(self.base_project_url + 'notebooks/' + path + quote(name))
49 self.redirect(self.base_project_url + 'notebooks/' + path + quote(name))
50 else:
50 else:
51 project = self.project + '/' + notebook_path
51 project = self.project + '/' + notebook_path
52 self.write(self.render_template('tree.html',
52 self.write(self.render_template('tree.html',
53 project=project,
53 project=project,
54 project_component=project.split('/'),
54 project_component=project.split('/'),
55 notebook_path=path,
55 notebook_path=path,
56 notebook_name=quote(name)))
56 notebook_name=name))
57
57
58
58
59 class TreeRedirectHandler(IPythonHandler):
59 class TreeRedirectHandler(IPythonHandler):
60
60
61 @authenticate_unless_readonly
61 @authenticate_unless_readonly
62 def get(self):
62 def get(self):
63 url = self.base_project_url + 'tree'
63 url = self.base_project_url + 'tree'
64 self.redirect(url)
64 self.redirect(url)
65
65
66 class TreePathRedirectHandler(IPythonHandler):
66 class TreePathRedirectHandler(IPythonHandler):
67
67
68 @authenticate_unless_readonly
68 @authenticate_unless_readonly
69 def get(self, notebook_path):
69 def get(self, notebook_path):
70 url = self.base_project_url + 'tree/'+ notebook_path
70 url = self.base_project_url + 'tree/'+ notebook_path
71 self.redirect(url)
71 self.redirect(url)
72
72
73 class ProjectRedirectHandler(IPythonHandler):
73 class ProjectRedirectHandler(IPythonHandler):
74
74
75 @authenticate_unless_readonly
75 @authenticate_unless_readonly
76 def get(self):
76 def get(self):
77 url = self.base_project_url + 'tree'
77 url = self.base_project_url + 'tree'
78 self.redirect(url)
78 self.redirect(url)
79
79
80 class NewFolderHandler(IPythonHandler):
80 class NewFolderHandler(IPythonHandler):
81
81
82 @authenticate_unless_readonly
82 @authenticate_unless_readonly
83 def get(self, notebook_path):
83 def get(self, notebook_path):
84 nbm = self.notebook_manager
84 nbm = self.notebook_manager
85 name, path = nbm.named_notebook_path(notebook_path)
85 name, path = nbm.named_notebook_path(notebook_path)
86 nbm.add_new_folder(path)
86 nbm.add_new_folder(path)
87 url = self.base_project_url + 'tree/' + notebook_path
87 url = self.base_project_url + 'tree/' + notebook_path
88 self.redirect(url)
88 self.redirect(url)
89
89
90
90
91 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
92 # URL to handler mappings
92 # URL to handler mappings
93 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
94
94
95
95
96 _notebook_path_regex = r"(?P<notebook_path>.+)"
96 _notebook_path_regex = r"(?P<notebook_path>.+)"
97
97
98 default_handlers = [
98 default_handlers = [
99 (r"/tree/%s/-new" %_notebook_path_regex, NewFolderHandler),
99 (r"/tree/%s/-new" %_notebook_path_regex, NewFolderHandler),
100 (r"/tree/%s/" % _notebook_path_regex, TreePathRedirectHandler),
100 (r"/tree/%s/" % _notebook_path_regex, TreePathRedirectHandler),
101 (r"/tree/%s" % _notebook_path_regex, ProjectPathDashboardHandler),
101 (r"/tree/%s" % _notebook_path_regex, ProjectPathDashboardHandler),
102 (r"/tree", ProjectDashboardHandler),
102 (r"/tree", ProjectDashboardHandler),
103 (r"/tree/", TreeRedirectHandler),
103 (r"/tree/", TreeRedirectHandler),
104 (r"/", ProjectRedirectHandler)
104 (r"/", ProjectRedirectHandler)
105 ]
105 ]
General Comments 0
You need to be logged in to leave comments. Login now