Python flask_restx.fields.Integer() Examples

The following are 2 code examples of flask_restx.fields.Integer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module flask_restx.fields , or try the search function .
Example #1
Source File: decorators.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _model_from_parser(model_name: str, parser: reqparse.RequestParser) -> Model:
    from flask_restx import fields

    base_type_map = {
        "integer": fields.Integer,
        "string": fields.String,
        "number": fields.Float,
    }
    type_factory = {
        "integer": lambda arg: base_type_map["integer"],
        "string": lambda arg: base_type_map["string"],
        "number": lambda arg: base_type_map["number"],
        "array": lambda arg: fields.List(base_type_map[arg["items"]["type"]]),
    }
    return Model(
        model_name,
        {arg["name"]: type_factory[arg["type"]](arg) for arg in parser.__schema__},
    ) 
Example #2
Source File: api.py    From indra with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def make_preassembly_model(func):
    """Create new Flask model with function arguments."""
    args = inspect.signature(func).parameters
    # We can reuse Staetments model if only stmts_in or stmts and **kwargs are
    # arguments of the function
    if ((len(args) == 1 and ('stmts_in' in args or 'stmts' in args)) or
            (len(args) == 2 and 'kwargs' in args and
                ('stmts_in' in args or 'stmts' in args))):
        return stmts_model
    # Inherit a model if there are other arguments
    model_fields = {}
    for arg in args:
        if arg != 'stmts_in' and arg != 'stmts' and arg != 'kwargs':
            default = None
            if args[arg].default is not inspect.Parameter.empty:
                default = args[arg].default
            # Need to use default for boolean and example for other types
            if arg in boolean_args:
                model_fields[arg] = fields.Boolean(default=default)
            elif arg in int_args:
                model_fields[arg] = fields.Integer(example=default)
            elif arg in float_args:
                model_fields[arg] = fields.Float(example=0.7)
            elif arg in list_args:
                if arg == 'curations':
                    model_fields[arg] = fields.List(
                        fields.Nested(dict_model),
                        example=[{'pa_hash': '1234', 'source_hash': '2345',
                                  'tag': 'wrong_relation'}])
                else:
                    model_fields[arg] = fields.List(
                        fields.String, example=default)
            elif arg in dict_args:
                model_fields[arg] = fields.Nested(dict_model)
            else:
                model_fields[arg] = fields.String(example=default)
    new_model = api.inherit(
        ('%s_input' % func.__name__), stmts_model, model_fields)
    return new_model