Support parameters for import

This commit is contained in:
Julien Duponchelle 2016-07-25 18:58:34 +02:00
parent e50acf811c
commit e50eae19e6
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 28 additions and 9 deletions

View File

@ -268,6 +268,14 @@ class ProjectHandler:
controller = Controller.instance() controller = Controller.instance()
if request.get("path"):
config = Config.instance()
if config.get_section_config("Server").getboolean("local", False) is False:
response.set_status(403)
return
path = request.json.get("path")
name = request.json.get("name")
# We write the content to a temporary location and after we extract it all. # We write the content to a temporary location and after we extract it all.
# It could be more optimal to stream this but it is not implemented in Python. # It could be more optimal to stream this but it is not implemented in Python.
# Spooled means the file is temporary kept in memory until max_size is reached # Spooled means the file is temporary kept in memory until max_size is reached
@ -278,7 +286,7 @@ class ProjectHandler:
if not packet: if not packet:
break break
temp.write(packet) temp.write(packet)
project = yield from import_project(controller, request.match_info["project_id"], temp) project = yield from import_project(controller, request.match_info["project_id"], temp, location=path, name=name)
except OSError as e: except OSError as e:
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e)) raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))

View File

@ -21,6 +21,7 @@ import jsonschema
import asyncio import asyncio
import aiohttp import aiohttp
import logging import logging
import urllib
import traceback import traceback
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -36,8 +37,9 @@ from ..config import Config
@asyncio.coroutine @asyncio.coroutine
def parse_request(request, input_schema): def parse_request(request, input_schema):
"""Parse body of request and raise HTTP errors in case of problems""" """Parse body of request and raise HTTP errors in case of problems"""
content_length = request.content_length content_length = request.content_length
if content_length is not None and content_length > 0: if content_length is not None and content_length > 0 and input_schema:
body = yield from request.read() body = yield from request.read()
try: try:
request.json = json.loads(body.decode('utf-8')) request.json = json.loads(body.decode('utf-8'))
@ -46,6 +48,13 @@ def parse_request(request, input_schema):
raise aiohttp.web.HTTPBadRequest(text="Invalid JSON {}".format(e)) raise aiohttp.web.HTTPBadRequest(text="Invalid JSON {}".format(e))
else: else:
request.json = {} request.json = {}
# Parse the query string
if len(request.query_string) > 0:
for (k, v) in urllib.parse.parse_qs(request.query_string).items():
request.json[k] = v[0]
if input_schema:
try: try:
jsonschema.validate(request.json, input_schema) jsonschema.validate(request.json, input_schema)
except jsonschema.ValidationError as e: except jsonschema.ValidationError as e:
@ -53,6 +62,7 @@ def parse_request(request, input_schema):
raise aiohttp.web.HTTPBadRequest(text="Invalid JSON: {} in schema: {}".format( raise aiohttp.web.HTTPBadRequest(text="Invalid JSON: {} in schema: {}".format(
e.message, e.message,
json.dumps(e.schema))) json.dumps(e.schema)))
return request return request
@ -166,6 +176,7 @@ class Route(object):
if api_version is None or raw is True: if api_version is None or raw is True:
response = Response(request=request, route=route, output_schema=output_schema) response = Response(request=request, route=route, output_schema=output_schema)
request = yield from parse_request(request, None)
yield from func(request, response) yield from func(request, response)
return response return response