Various fixes for the flask adapter

This commit is contained in:
Christophe de Vienne 2013-03-26 14:29:18 +01:00
parent 07adf31cd7
commit bfd1bfb4ff
3 changed files with 25 additions and 11 deletions

View File

@ -1,10 +1,11 @@
import unittest import unittest
from flask import Flask from flask import Flask, json
from wsmeext.flask import signature from wsmeext.flask import signature
from wsme.types import Base, text from wsme.types import Base, text
class Model(Base): class Model(Base):
id = int
name = text name = text
@ -26,11 +27,11 @@ def divide_by_zero():
@test_app.route('/models') @test_app.route('/models')
@signature([Model]) @signature([Model])
def list_models(): def list_models():
return [Model(name=1)] return [Model(name='first')]
@test_app.route('/models/<name>') @test_app.route('/models/<name>')
@signature(Model) @signature(Model, text)
def get_model(name): def get_model(name):
return Model(name=name) return Model(name=name)
@ -63,9 +64,13 @@ class FlaskrTestCase(unittest.TestCase):
assert resp.status_code == 200 assert resp.status_code == 200
def test_post_model(self): def test_post_model(self):
resp = self.app.post('/models', data={"name": "test"}) resp = self.app.post('/models', data={"body.name": "test"})
import ipdb assert resp.status_code == 200
ipdb.set_trace() resp = self.app.post(
'/models',
data=json.dumps({"name": "test"}),
content_type="application/json"
)
assert resp.status_code == 200 assert resp.status_code == 200
def test_serversideerror(self): def test_serversideerror(self):

View File

@ -158,7 +158,7 @@ def args_from_args(funcdef, args, kwargs):
newargs.append(from_param(argdef.datatype, arg)) newargs.append(from_param(argdef.datatype, arg))
newkwargs = {} newkwargs = {}
for argname, value in kwargs.items(): for argname, value in kwargs.items():
newkwargs[argname] = from_param(funcdef.get_arg(argname), value) newkwargs[argname] = from_param(funcdef.get_arg(argname).datatype, value)
return newargs, newkwargs return newargs, newkwargs
@ -229,7 +229,7 @@ def combine_args(funcdef, akw, allow_override=False):
return newargs, newkwargs return newargs, newkwargs
def get_args(funcdef, args, kwargs, params, body, mimetype): def get_args(funcdef, args, kwargs, params, form, body, mimetype):
"""Combine arguments from : """Combine arguments from :
* the host framework args and kwargs * the host framework args and kwargs
* the request params * the request params
@ -248,13 +248,19 @@ def get_args(funcdef, args, kwargs, params, body, mimetype):
# extract args from the request parameters # extract args from the request parameters
from_params = args_from_params(funcdef, params) from_params = args_from_params(funcdef, params)
# extract args from the form parameters
if form:
from_form_params = args_from_params(funcdef, form)
else:
from_form_params = (), {}
# extract args from the request body # extract args from the request body
from_body = args_from_body(funcdef, body, mimetype) from_body = args_from_body(funcdef, body, mimetype)
# combine params and body arguments # combine params and body arguments
from_params_and_body = combine_args( from_params_and_body = combine_args(
funcdef, funcdef,
(from_params, from_body) (from_params, from_form_params, from_body)
) )
return combine_args( return combine_args(

View File

@ -21,12 +21,15 @@ def signature(*args, **kw):
def decorator(f): def decorator(f):
sig(f) sig(f)
funcdef = wsme.api.FunctionDefinition.get(f) funcdef = wsme.api.FunctionDefinition.get(f)
funcdef.resolve_types(wsme.types.registry)
@functools.wraps(f) @functools.wraps(f)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
args, kwargs = wsme.rest.args.get_args( args, kwargs = wsme.rest.args.get_args(
funcdef, args, kwargs, flask.request.args, flask.request.data, funcdef, args, kwargs,
flask.request.content_type flask.request.args, flask.request.form,
flask.request.data,
flask.request.mimetype
) )
dataformat = None dataformat = None