Merge pull request #375 from betatim/form-file-upload

Handle file upload in spawner form
This commit is contained in:
Min RK
2016-01-09 23:25:52 +01:00
4 changed files with 34 additions and 2 deletions

View File

@@ -87,6 +87,8 @@ class SpawnHandler(BaseHandler):
form_options = {}
for key, byte_list in self.request.body_arguments.items():
form_options[key] = [ bs.decode('utf8') for bs in byte_list ]
for key, byte_list in self.request.files.items():
form_options["%s_file"%key] = byte_list
options = user.spawner.options_from_form(form_options)
yield self.spawn_single_user(user, options=options)
self.set_login_cookie(user)

View File

@@ -83,6 +83,8 @@ class FormSpawner(MockSpawner):
options['bounds'] = [int(i) for i in form_data['bounds']]
if 'energy' in form_data:
options['energy'] = form_data['energy'][0]
if 'hello_file' in form_data:
options['hello'] = form_data['hello_file'][0]
return options

View File

@@ -119,3 +119,31 @@ def test_spawn_form(app, io_loop):
'notspecified': 5,
}
def test_spawn_form_with_file(app, io_loop):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}):
base_url = ujoin(app.proxy.public_server.host, app.hub.server.base_url)
cookies = app.login_user('jones')
orm_u = orm.User.find(app.db, 'jones')
u = app.users[orm_u]
io_loop.run_sync(u.stop)
r = requests.post(ujoin(base_url, 'spawn'),
cookies=cookies,
data={
'bounds': ['-1', '1'],
'energy': '511keV',
},
files={'hello': ('hello.txt', b'hello world\n')}
)
r.raise_for_status()
print(u.spawner)
print(u.spawner.user_options)
assert u.spawner.user_options == {
'energy': '511keV',
'bounds': [-1, 1],
'notspecified': 5,
'hello': {'filename': 'hello.txt',
'body': b'hello world\n',
'content_type': 'application/unknown'},
}

View File

@@ -7,7 +7,7 @@
<h1>Spawner options</h1>
</div>
<div class="row col-sm-offset-2 col-sm-8">
<form id="spawn_form" action="{{base_url}}spawn" method="post" role="form">
<form enctype="multipart/form-data" id="spawn_form" action="{{base_url}}spawn" method="post" role="form">
{{spawner_options_form}}
<br>
<input type="submit" value="Spawn" class="btn btn-jupyter">
@@ -24,4 +24,4 @@ require(["jquery"], function ($) {
$("#spawn_form").find("input, select, textarea, button").addClass("form-control");
});
</script>
{% endblock %}
{% endblock %}