python - 使用 Flask-Restful 时使用 fields.Url 生成 url 会生成 BuildError

标签 python flask url-routing flask-restful

我想改编精彩的Tutorial from Miguel Grinberg创建一个 unittest 测试执行器。我主要只是根据我的需要调整了 Miguel 的代码,但是在字段映射中生成 uri 时遇到问题。只要我删除

'uri': fields.Url('test')

一切正常,但否则我得到一个构建错误:
BuildError: ('test', {'Test_environment_id': 123, 'Test_duration': '0.5 sec', 'Success': 1, 'Failure_count': 0, 'Tested_files': 'Some files', 'Request_id': 1, 'Runs_count': 3, 'Created_on': '01.01.1970', 'Error_count': 0, 'Requester': 'John', 'Skipped_count': 2}, None)

我在 stackoverflow here 上发现了非常相似的问题但这并没有帮助我理解我的代码有什么问题。我可以使用该问题中描述的解决方法,但我真的很想知道我做错了什么。

这是我的代码:

#!/usr/bin/python
__author__ = 'karlitos'

from flask import Flask, jsonify, abort, make_response, request
from flask.ext.restful import Api, Resource, reqparse, fields, marshal,url_for
from time import strftime
from glob import glob
import os
import sqlite3

CURRENT_DIRECTORY = os.getcwd()
PROJECT_DIRECTORIES = glob('{}/projects/*'.format(CURRENT_DIRECTORY))
# create a sqlite database connection object
db_con = sqlite3.connect('{}unittest.db'.format(CURRENT_DIRECTORY))

app = Flask(__name__, static_url_path="")
api = Api(app)

tests = [
    {
        'Request_id': 1,
        'Requester': 'John',
        'Created_on': '01.01.1970',
        'Test_environment_id': 123,
        'Tested_files': 'Some files',
        'Test_duration': '0.5 sec',
        'Runs_count': 3,
        'Error_count': 0,
        'Failure_count': 0,
        'Skipped_count': 2,
        'Success': 1
    }
]

"""Structure storing the `Request_id`'s of all test currently running indexed by their `Test_environment_id`'s."""
env_id_of_running_tests = {}

"""Structure serving as a template for the `marshal` function which takes raw data and a dict of fields to output and
 filters the data based on those fields."""
test_fields = {
    'Request_id': fields.Integer,
    'Requester': fields.String,
    'Created_on': fields.String,
    'Test_environment_id': fields.Integer,
    'Tested_files': fields.String,
    'Test_duration': fields.String,
    'Runs_count': fields.Integer,
    'Error_count': fields.Integer,
    'Failure_count': fields.Integer,
    'Skipped_count': fields.Integer,
    'Success': fields.Boolean,
    'uri': fields.Url('test')
}

"""Validation function for the environment-id type which has to be in range [1,100]"""


def env_id_type(value, name):
    if value <= 1 or value >= 100:
        raise ValueError("The parameter '{}' is not between 1 and 100. The value: {} was provided".format(name, value))
    return value


class TestsAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('Requester', type=str, required=True,
                                   help='No requester name provided', location='json')
        self.reqparse.add_argument('Test_environment_id', type=env_id_type, required=True,
                                   help='Bad environment-id provided, between 1 and 100.', location='json')
        super(TestsAPI, self).__init__()

    def get(self):
        return {'tests': [marshal(test, test_fields) for test in tests]}

    def post(self):
        args = self.reqparse.parse_args()

        request_id = tests[-1]['Request_id'] + 1

        # check if the current Test_environment_id is not under the currently running test
        if args['Test_environment_id'] in env_id_of_running_tests:
            return {'message': 'Another test with the same Environment-ID is still running.'}, 409
        else:
            env_id_of_running_tests[args['Test_environment_id']] = request_id
        test = {
            'Request_id': request_id,
            'Requester': args['Requester'],
            'Created_on': strftime('%a, %d %b %Y %H:%M:%S'),
            'Test_environment_id': args['Test_environment_id'],
            'Tested_files': 'Some files',
            'Test_duration': '',
            'Runs_count': None,
            'Error_count': None,
            'Failure_count': None,
            'Skipped_count': None,
            'Success': None
        }

        tests.append(test)

        return {'test started': marshal(test, test_fields)}, 201


class TestAPI(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('Request_id', type=int, required=True,
                                   help='No Request-ID provided', location='json')
        super(TestAPI, self).__init__()

    def get(self, request_id):
        test = [test for test in tests if test['Request_id'] == request_id]
        print 'Request_ID', request_id
        if len(test) == 0:
            abort(404)
        return {'test   ': marshal(test[0], test_fields)}


api.add_resource(TestsAPI, '/test-executor/api/tests', endpoint='tests')
api.add_resource(TestAPI, '/test-executor/api/tests/<int:request_id>', endpoint='test')

if __name__ == '__main__':
    app.run(debug=True)

最佳答案

您的“测试”端点的路由有一个参数 request_id (带有小写首字母 r),但您的测试数据字典有一个带有键 Request_id 的条目(大写初始 R)。编码数据时,flask-restful 会查找带有小写 request_id 的条目在测试数据字典中以构造URL,但由于大小写不匹配而无法找到它。

如果将路由中的参数改为大写Request_id它会处理你的 BuildError 对“测试”端点的请求。但是通过对测试端点的请求,由于 TestAPI.get() 中类似的大小写不匹配,您将遇到新的易于修复的错误。

关于python - 使用 Flask-Restful 时使用 fields.Url 生成 url 会生成 BuildError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386470/

相关文章:

python - 在 python 中从 BackgroundSubtractorMOG2 获取背景模型

Python多处理安全地写入文件

python - 上下文中的 flask 上下文,对于 jsonfy,全部在一个页面中

具有无限/#/in url的Angular 4路由

python - 按 pandas 数据框中的数据分组

python - 在 Pytorch 中,(x<0) 和 x.lt(0) 之间有区别吗?

python - Flask、Jinja2、 "$"字符上的 Babel 错误

javascript - 通过对 Flask 的函数调用更新值

php - 从子目录飞行 PHP 路由

javascript - Angular 2 中的 URL 路由