##// END OF EJS Templates
allow requesting contents without body...
Min RK -
Show More
@@ -117,13 +117,19 b' class ContentsHandler(IPythonHandler):'
117 format = self.get_query_argument('format', default=None)
117 format = self.get_query_argument('format', default=None)
118 if format not in {None, 'text', 'base64'}:
118 if format not in {None, 'text', 'base64'}:
119 raise web.HTTPError(400, u'Format %r is invalid' % format)
119 raise web.HTTPError(400, u'Format %r is invalid' % format)
120
120 content = self.get_query_argument('content', default='1')
121 model = yield gen.maybe_future(self.contents_manager.get(path=path, type=type, format=format))
121 if content not in {'0', '1'}:
122 if model['type'] == 'directory':
122 raise web.HTTPError(400, u'Content %r is invalid' % content)
123 content = int(content)
124
125 model = yield gen.maybe_future(self.contents_manager.get(
126 path=path, type=type, format=format, content=content,
127 ))
128 if model['type'] == 'directory' and content:
123 # group listing by type, then by name (case-insensitive)
129 # group listing by type, then by name (case-insensitive)
124 # FIXME: sorting should be done in the frontends
130 # FIXME: sorting should be done in the frontends
125 model['content'].sort(key=sort_key)
131 model['content'].sort(key=sort_key)
126 validate_model(model, expect_content=True)
132 validate_model(model, expect_content=content)
127 self._finish_model(model, location=False)
133 self._finish_model(model, location=False)
128
134
129 @web.authenticated
135 @web.authenticated
@@ -51,12 +51,14 b' class API(object):'
51 def list(self, path='/'):
51 def list(self, path='/'):
52 return self._req('GET', path)
52 return self._req('GET', path)
53
53
54 def read(self, path, type=None, format=None):
54 def read(self, path, type=None, format=None, content=None):
55 params = {}
55 params = {}
56 if type is not None:
56 if type is not None:
57 params['type'] = type
57 params['type'] = type
58 if format is not None:
58 if format is not None:
59 params['format'] = format
59 params['format'] = format
60 if content == False:
61 params['content'] = '0'
60 return self._req('GET', path, params=params)
62 return self._req('GET', path, params=params)
61
63
62 def create_untitled(self, path='/', ext='.ipynb'):
64 def create_untitled(self, path='/', ext='.ipynb'):
@@ -243,6 +245,14 b' class APITest(NotebookTestBase):'
243 dir_names = {normalize('NFC', d['name']) for d in dirs}
245 dir_names = {normalize('NFC', d['name']) for d in dirs}
244 self.assertEqual(dir_names, self.top_level_dirs) # Excluding hidden dirs
246 self.assertEqual(dir_names, self.top_level_dirs) # Excluding hidden dirs
245
247
248 def test_get_dir_no_content(self):
249 for d in self.dirs:
250 model = self.api.read(d, content=False).json()
251 self.assertEqual(model['path'], d)
252 self.assertEqual(model['type'], 'directory')
253 self.assertIn('content', model)
254 self.assertEqual(model['content'], None)
255
246 def test_list_nonexistant_dir(self):
256 def test_list_nonexistant_dir(self):
247 with assert_http_error(404):
257 with assert_http_error(404):
248 self.api.list('nonexistant')
258 self.api.list('nonexistant')
@@ -256,10 +266,19 b' class APITest(NotebookTestBase):'
256 self.assertEqual(nb['type'], 'notebook')
266 self.assertEqual(nb['type'], 'notebook')
257 self.assertIn('content', nb)
267 self.assertIn('content', nb)
258 self.assertEqual(nb['format'], 'json')
268 self.assertEqual(nb['format'], 'json')
259 self.assertIn('content', nb)
260 self.assertIn('metadata', nb['content'])
269 self.assertIn('metadata', nb['content'])
261 self.assertIsInstance(nb['content']['metadata'], dict)
270 self.assertIsInstance(nb['content']['metadata'], dict)
262
271
272 def test_get_nb_no_content(self):
273 for d, name in self.dirs_nbs:
274 path = url_path_join(d, name + '.ipynb')
275 nb = self.api.read(path, content=False).json()
276 self.assertEqual(nb['name'], u'%s.ipynb' % name)
277 self.assertEqual(nb['path'], path)
278 self.assertEqual(nb['type'], 'notebook')
279 self.assertIn('content', nb)
280 self.assertEqual(nb['content'], None)
281
263 def test_get_contents_no_such_file(self):
282 def test_get_contents_no_such_file(self):
264 # Name that doesn't exist - should be a 404
283 # Name that doesn't exist - should be a 404
265 with assert_http_error(404):
284 with assert_http_error(404):
@@ -72,13 +72,12 b' define(function(require) {'
72 /**
72 /**
73 * Get a file.
73 * Get a file.
74 *
74 *
75 * Calls success with file JSON model, or error with error.
76 *
77 * @method get
75 * @method get
78 * @param {String} path
76 * @param {String} path
79 * @param {Object} options
77 * @param {Object} options
80 * type : 'notebook', 'file', or 'directory'
78 * type : 'notebook', 'file', or 'directory'
81 * format: 'text' or 'base64'; only relevant for type: 'file'
79 * format: 'text' or 'base64'; only relevant for type: 'file'
80 * content: true or false; // whether to include the content
82 */
81 */
83 Contents.prototype.get = function (path, options) {
82 Contents.prototype.get = function (path, options) {
84 /**
83 /**
@@ -94,6 +93,7 b' define(function(require) {'
94 var params = {};
93 var params = {};
95 if (options.type) { params.type = options.type; }
94 if (options.type) { params.type = options.type; }
96 if (options.format) { params.format = options.format; }
95 if (options.format) { params.format = options.format; }
96 if (options.content === false) { params.content = '0'; }
97 return utils.promising_ajax(url + '?' + $.param(params), settings);
97 return utils.promising_ajax(url + '?' + $.param(params), settings);
98 };
98 };
99
99
General Comments 0
You need to be logged in to leave comments. Login now