def test_types(self):
     with pytest.raises(fields.MarshallingError):
         class WrongType:
             pass
         x = WrongType()
         field1 = fields.Wildcard(WrongType)  # noqa
         field2 = fields.Wildcard(x)  # noqa
 def test_marshal_wildcard_with_skip_none(self):
     wild = fields.Wildcard(fields.String)
     model = OrderedDict([('foo', fields.Raw), ('*', wild)])
     marshal_dict = OrderedDict([('foo', None), ('bat', None),
                                 ('baz', 'biz'), ('bar', None)])
     output = marshal(marshal_dict, model, skip_none=True)
     assert output == {'baz': 'biz'}
Exemple #3
0
 def test_marshal_wildcard_nested(self):
     nest = fields.Nested(
         OrderedDict([('thumbnail', fields.String),
                      ('video', fields.String)]))
     wild = fields.Wildcard(nest)
     wildcard_fields = OrderedDict([('*', wild)])
     model = OrderedDict([('preview', fields.Nested(wildcard_fields))])
     sub_dict = OrderedDict([('9:16', {
         'thumbnail': 24,
         'video': 12
     }), ('16:9', {
         'thumbnail': 25,
         'video': 11
     }), ('1:1', {
         'thumbnail': 26,
         'video': 10
     })])
     marshal_dict = OrderedDict([('preview', sub_dict)])
     output = marshal(marshal_dict, model)
     assert output == {
         'preview': {
             '1:1': {
                 'thumbnail': '26',
                 'video': '10'
             },
             '16:9': {
                 'thumbnail': '25',
                 'video': '11'
             },
             '9:16': {
                 'thumbnail': '24',
                 'video': '12'
             }
         }
     }
Exemple #4
0
class GetSubmissionTextsDto:
    api = Namespace('get submission texts',
                    description='submission-specific get operations')

    status = api.model(
        'Status', {
            'status': fields.String(description="Request status"),
            'message':
            fields.String(description="Message describing the status")
        })

    tags = {'*': fields.Wildcard(fields.String)}

    token = {
        "word": fields.String(description='original word'),
        "lemma": fields.String(description='lemma of original word'),
        "tags": fields.Nested(tags)
    }

    tokens = {
        "tokens":
        fields.List(cls_or_instance=fields.Nested(token),
                    description='list of tokens')
    }

    sentences = {
        "sentences":
        fields.List(cls_or_instance=fields.Nested(tokens),
                    description='list of sentences')
    }
    text_fields = {
        'text_id':
        fields.Integer(description='text identifier'),
        'processing_complete':
        fields.Boolean(
            description='Boolean indicating if text has been processed'),
        'raw_text':
        fields.String(description='Original text input'),
        'processed_text':
        fields.Nested(sentences)
    }

    submission_fields = {
        'submission_id':
        fields.Integer(description='submission identifier'),
        'text_count':
        fields.Integer(
            description='number indicating the amount of texts in submission'),
        'submitted_date':
        fields.DateTime(description='date of submission'),
        'tags':
        fields.List(cls_or_instance=fields.String(),
                    description='submission tags')
    }

    get_submission_texts = api.inherit(
        'get submission texts', status, {
            'submission': fields.Nested(submission_fields, default=dict()),
            'texts': fields.List(cls_or_instance=fields.Nested(text_fields))
        })
Exemple #5
0
 def test_defaults(self):
     field = fields.Wildcard(fields.String)
     assert not field.required
     assert field.__schema__ == {
         'type': 'object',
         'additionalProperties': {
             'type': 'string'
         }
     }
    def test_list_of_raw(self):
        field = fields.Wildcard(fields.Raw)

        data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]
        expected = [OrderedDict([('a', 1), ('b', 1)]),
                    OrderedDict([('a', 2), ('b', 1)]),
                    OrderedDict([('a', 3), ('b', 1)])]
        self.assert_field(field, data, expected)

        data = [1, 2, 'a']
        self.assert_field(field, data, data)
Exemple #7
0
 def test_marshal_wildcard_list(self):
     wild = fields.Wildcard(fields.List(fields.String))
     wildcard_fields = OrderedDict([('*', wild)])
     model = OrderedDict([('preview', fields.Nested(wildcard_fields))])
     sub_dict = OrderedDict([('1:1', [1, 2, 3]), ('16:9', [4, 5, 6]),
                             ('9:16', [7, 8, 9])])
     marshal_dict = OrderedDict([('preview', sub_dict)])
     output = marshal(marshal_dict, model)
     assert output == {
         'preview': {
             '9:16': ['7', '8', '9'],
             '16:9': ['4', '5', '6'],
             '1:1': ['1', '2', '3']
         }
     }
Exemple #8
0
    def test_clone(self, api):
        wild1 = fields.Wildcard(fields.String)
        wild2 = wild1.clone()

        wild_fields1 = api.model('cloneWildcard1', {'*': wild1})
        wild_fields2 = api.model('cloneWildcard2', {'*': wild2})

        data = {'John': 12, 'bob': 42, 'Jane': '68'}
        expected1 = {'John': '12', 'bob': '42', 'Jane': '68'}

        result1 = api.marshal(data, wild_fields1)
        result2 = api.marshal(data, wild_fields2)

        assert expected1 == result1
        assert result2 == result1
    def test_with_scoped_attribute_on_dict_or_obj(self):
        class Test(object):
            def __init__(self, data):
                self.data = data

        class Nested(object):
            def __init__(self, value):
                self.value = value

        nesteds = [Nested(i) for i in ['a', 'b', 'c']]
        test_obj = Test(nesteds)
        test_dict = {'data': [{'value': 'a'}, {'value': 'b'}, {'value': 'c'}]}

        field = fields.Wildcard(fields.String(attribute='value'), attribute='data')
        assert ['a' == 'b', 'c'], field.output('whatever', test_obj)
        assert ['a' == 'b', 'c'], field.output('whatever', test_dict)
 def test_marshal_wildcard_with_envelope(self):
     wild = fields.Wildcard(fields.String)
     model = OrderedDict([('foo', fields.Raw), ('*', wild)])
     marshal_dict = OrderedDict([('foo', {
         'bat': 'baz'
     }), ('a', 'toto'), ('b', 'tata')])
     output = marshal(marshal_dict, model, envelope='hey')
     assert output == {
         'hey': {
             'a': 'toto',
             'b': 'tata',
             'foo': {
                 'bat': 'baz'
             }
         }
     }
Exemple #11
0
class StatePredict:
    api = Namespace('state_predict', description='Model for detecting the current state of the mission.')

    prediction_dto = api.model('Prediction', {
        'cur_state': fields.String(required=True, description='The current state'),
        'message': fields.String(required=True, description='The message generating the prediction'),
        'message_index': fields.Integer(default=-1, description='The message index within the chat stream'),
        'predictions': fields.Wildcard(fields.Float)
    })

    payload_model = api.model('Payload', {
        'domain_name': fields.String(required=True, description=''),
        'message': fields.String(required=True, description='')
    })

    state_payload = reqparse.RequestParser(bundle_errors=True)
    state_payload.add_argument('domain_name', help='The name of the domain to predict against')
    state_payload.add_argument('message', help='The message to predict on', location="json")
Exemple #12
0
class TextDto:
    api = Namespace('text', description='Text-related operations')

    status = api.model(
        'status', {
            'status': fields.String(description="request status"),
            'message': fields.String(description="status message")
        })

    tags = {'*': fields.Wildcard(fields.String)}

    token = {
        "word": fields.String(description='original word'),
        "lemma": fields.String(description='lemma of original word'),
        "tags": fields.Nested(tags)
    }

    tokens = {
        "tokens":
        fields.List(cls_or_instance=fields.Nested(token),
                    description='list of tokens')
    }

    sentences = {
        "sentences":
        fields.List(cls_or_instance=fields.Nested(tokens),
                    description='list of sentences')
    }

    text = api.inherit(
        'text_details', status, {
            'id':
            fields.Integer(description='public id of text'),
            'submitted_date':
            fields.DateTime(description='Time text was submitted'),
            'processing_complete':
            fields.Boolean(
                description='indicates if text is processed or not'),
            'raw_text':
            fields.String(description="raw submitted text"),
            'processed_text':
            fields.Nested(sentences, default=dict())
        })
Exemple #13
0
    def test_wildcard(self, api):
        wild1 = fields.Wildcard(fields.String)
        wild2 = fields.Wildcard(fields.Integer)
        wild3 = fields.Wildcard(fields.String)
        wild4 = fields.Wildcard(fields.String, default='x')
        wild5 = fields.Wildcard(fields.String)
        wild6 = fields.Wildcard(fields.Integer)
        wild7 = fields.Wildcard(fields.String)
        wild8 = fields.Wildcard(fields.String)

        mod5 = OrderedDict()
        mod5['toto'] = fields.Integer
        mod5['bob'] = fields.Integer
        mod5['*'] = wild5

        wild_fields1 = api.model('WildcardModel1', {'*': wild1})
        wild_fields2 = api.model('WildcardModel2', {'j*': wild2})
        wild_fields3 = api.model('WildcardModel3', {'*': wild3})
        wild_fields4 = api.model('WildcardModel4', {'*': wild4})
        wild_fields5 = api.model('WildcardModel5', mod5)
        wild_fields6 = api.model(
            'WildcardModel6', {
                'nested': {
                    'f1': fields.String(default='12'),
                    'f2': fields.Integer(default=13)
                },
                'a*': wild6
            })
        wild_fields7 = api.model('WildcardModel7', {'*': wild7})
        wild_fields8 = api.model('WildcardModel8', {'*': wild8})

        class Dummy(object):
            john = 12
            bob = '42'
            alice = None

        class Dummy2(object):
            pass

        class Dummy3(object):
            a = None
            b = None

        data = {'John': 12, 'bob': 42, 'Jane': '68'}
        data3 = Dummy()
        data4 = Dummy2()
        data5 = {'John': 12, 'bob': 42, 'Jane': '68', 'toto': '72'}
        data6 = {'nested': {'f1': 12, 'f2': 13}, 'alice': '14'}
        data7 = Dummy3()
        data8 = None
        expected1 = {'John': '12', 'bob': '42', 'Jane': '68'}
        expected2 = {'John': 12, 'Jane': 68}
        expected3 = {'john': '12', 'bob': '42'}
        expected4 = {'*': 'x'}
        expected5 = {'John': '12', 'bob': 42, 'Jane': '68', 'toto': 72}
        expected6 = {'nested': {'f1': '12', 'f2': 13}, 'alice': 14}
        expected7 = {}
        expected8 = {}

        result1 = api.marshal(data, wild_fields1)
        result2 = api.marshal(data, wild_fields2)
        result3 = api.marshal(data3, wild_fields3, skip_none=True)
        result4 = api.marshal(data4, wild_fields4)
        result5 = api.marshal(data5, wild_fields5)
        result6 = api.marshal(data6, wild_fields6)
        result7 = api.marshal(data7, wild_fields7, skip_none=True)
        result8 = api.marshal(data8, wild_fields8, skip_none=True)

        assert expected1 == result1
        assert expected2 == result2
        assert expected3 == result3
        assert expected4 == result4
        assert expected5 == result5
        assert expected6 == result6
        assert expected7 == result7
        assert expected8 == result8
Exemple #14
0
        "Identifica se a requisição foi processada com sucesso ou se houveram erros",
        example="success",
        default="success"),
    "error":
    fields.String(
        required=False,
        description=
        "Se o status for diferente de \"success\" esse campo irá conter uma mensagem amigavel "
        + "sobre o erro ocorrido",
        example=
        "Ops, Houve um erro desconhecido no servidor, se o erro persistir, contate o suporte"
    ),
    "messages":
    fields.Wildcard(
        fields.String,
        required=False,
        description=
        "Se o status for \"validation_error\" esse campo irá conter as mensagens de erro de validação"
    )
}


def make_response_model(model: dict) -> dict:
    updated_model = model.copy()
    updated_model.update(default_response_model)
    return updated_model


##### TASKS MODELS #####
get_tasks_response_model = {
    "total":
    fields.Integer(
Exemple #15
0
from flask_restplus import fields
from weakref import WeakSet


class AnyNotNullField(fields.Raw):
    __schema_type__ = 'any'

    def format(self, value):
        if not isinstance(value, WeakSet):
            return value


class ForbiddenField(fields.Raw):
    __schema_type__ = 'any'

    def format(self, value):
        return


# ==================== Wildcard fields =======================

wild_any_fields = fields.Wildcard(AnyNotNullField,
                                  description="other fields",
                                  skip_none=False,
                                  allow_null=False)
wild_forbid_fields = fields.Wildcard(ForbiddenField,
                                     description="forbidden fields for output")
Exemple #16
0
    'Lname': 'Zimmerman'
}, {
    'Fname': 'Jeremy',
    'Lname': 'Leininger'
}]
MAP_KEY_EXAMPLE = [{"Fname": "FirstName"}, {"Lname": "LastName"}]

api = Namespace(
    'basic-map',
    description=
    'Maps one JSON Object to another JSON Object based off of a map key')

basic_input = api.model(
    'Basic Input', {
        'input_data':
        fields.List(fields.Wildcard(fields.String),
                    example=INPUT_DATA_EXAMPLE),
        'map_key':
        fields.List(fields.Wildcard(fields.String), example=MAP_KEY_EXAMPLE)
    })


@api.route('/')
class BasicMap(Resource):
    @api.expect(basic_input)
    @api.doc('basic_map')
    def post(self):
        #print(request.json)
        input_list = request.json['input_data']
        map_key = request.json['map_key']
Exemple #17
0
- DETACHED_PROCESS
- CREATE_DEFAULT_ERROR_MODE
- CREATE_BREAKAWAY_FROM_JOB"""),
        'cwd':
        fields.String(
            required=False,
            default=None,
            example=".",
            description=
            """If cwd is not None, the function changes the working directory to cwd before executing the child. cwd can be a string, bytes or path-like object. In particular, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path."""
        ),
        'env':
        fields.Wildcard(
            fields.String,
            required=False,
            default={},
            example={},
            description=
            """If env is not None, it must be a mapping that defines the environment variables for the new process; these are used instead of the default behavior of inheriting the current process' environment."""
        ),
        'restore_signals':
        fields.Boolean(
            required=False,
            default=True,
            example=True,
            description=
            """If restore_signals is true (the default) all signals that Python has set to SIG_IGN are restored to SIG_DFL in the child process before the exec. Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals. (POSIX only)"""
        ),
        'start_new_session':
        fields.Boolean(
            required=False,
            default=False,
Exemple #18
0
                ast.literal_eval(args[self.PAGE_KEY]) if args[self.PAGE_KEY] is not None else None
                , args[self.PAGE_SIZE])
        except Exception as e:
            print(f'Exception occured during getPagedArchive {e}')
            return 'Internal Server Error', 500
        return marshal(result, multiArchiveMetadata), 200


singleArchiveMetadata = archive.model('singleArchiveMetadata', {
    'id': fields.String,
    'thumbnailUrl': fields.String,
    'title': fields.String
})

multiArchiveMetadata = archive.model('multiArchiveMetadata', {
    'nextPageKey': fields.Wildcard(fields.String),
    'archives': fields.List(fields.Nested(singleArchiveMetadata))
})


@archive.route('/<string:id>',
               doc={
                   "deprecated": True
               })
class getArchive(Resource):

    def get(self, id):
        print(f'archive item - id {id}')

        try:
            # TODO : Need DI for Archive, not creating dynamically
Exemple #19
0
                      description="Name of the model",
                      help="Name cannot be blank."),
        'version':
        fields.String(required=True,
                      description="Version of the model",
                      help="Name cannot be blank."),
        'format':
        fields.String(required=True,
                      description="Format of the model",
                      help="Name cannot be blank."),
        'author':
        fields.String(required=True,
                      description="Author of the model",
                      help="Name cannot be blank."),
        'metrics':
        fields.Wildcard(fields.String),
        'customProperties':
        fields.Wildcard(fields.String)
    })

model_signature_parameter = api.model(
    'ModelSignatureParameter', {
        'name':
        fields.String(required=True,
                      description="Name of the model",
                      help="Name cannot be blank."),
        'order':
        fields.String(required=True,
                      description="Version of the model",
                      help="Name cannot be blank."),
        'type':
Exemple #20
0
            +
            'while a lower scores could mean missing data or generalization. When a match score is '
            +
            'below the match_threshold of the chatbot, a no_answer is given by the chatbot.'
        ),
    })

chatbot_statistics_dict = api.model(
    'Chatbot Statistics per day', {
        '*':
        fields.Wildcard(
            fields.Nested(chatbot_statistics),
            example={
                'user_count': 5,
                'session_count': 7,
                'total_msg_count': 63,
                'bot_msg_count': 30,
                'user_msg_count': 21,
                'accuracy': 84.75,
                'match_score': 0.77239636
            },
            description='The chatbot statistics for a specific day')
    })

suggestion = api.model(
    'Suggestion', {
        'text':
        fields.String(readOnly=True,
                      example='How much does TV Plus cost?',
                      description='The text'),
        'score':
        fields.Float(readOnly=True,
Exemple #21
0
# urlmap

m_secprofilemap = api.model(
    "Security Profile Map", {
        "name": fields.String(required=True),
        "match": fields.String(required=True),
        "acl_profile": fields.String(required=True),
        "acl_active": fields.Boolean(required=True),
        "waf_profile": fields.String(required=True),
        "waf_active": fields.Boolean(required=True),
        "limit_ids": fields.List(fields.Raw()),
    })

m_map = api.model("Security Profile Map",
                  {"*": fields.Wildcard(fields.Nested(m_secprofilemap))})

m_urlmap = api.model(
    "URL Map", {
        "id": fields.String(required=True),
        "name": fields.String(required=True),
        "match": fields.String(required=True),
        "map": fields.List(fields.Nested(m_secprofilemap)),
    })

# wafrule

m_wafrule = api.model(
    "WAF Rule", {
        "id": fields.String(required=True),
        "name": fields.String(required=True),
Exemple #22
0

# urlmap

m_secprofilemap = api.model("Security Profile Map", {
    "name": fields.String(required=True),
    "match": fields.String(required=True),
    "acl_profile": fields.String(required=True),
    "acl_active": fields.Boolean(required=True),
    "waf_profile": fields.String(required=True),
    "waf_active": fields.Boolean(required=True),
    "limit_ids": fields.List(fields.Raw()),
})

m_map = api.model("Security Profile Map", {
    "*": fields.Wildcard(fields.Nested(m_secprofilemap))
})

m_urlmap = api.model("URL Map", {
    "id": fields.String(required=True),
    "name": fields.String(required=True),
    "match": fields.String(required=True),
    "map": fields.List(fields.Nested(m_secprofilemap)),
})

# wafsig

m_wafsig = api.model("WAF Signature", {
    "id": fields.String(required=True),
    "name": fields.String(required=True),
    "msg": fields.String(required=True),
from apps.api.profile.controllers import member_api

entity = member_api.model('Entity', {'id': fields.Integer})

basic_profile = member_api.model(
    'BasicProfile',
    {
        'real_name': fields.String,
        'gender': fields.String,
        # 'birthday': fields.String
    })

extra_profile = member_api.model('ExtraProfile', {
    'profile_category': fields.String,
    '*': fields.Wildcard(fields.String)
})

user_profile = member_api.clone(
    'UserProfile', entity, {
        'user_type': fields.String,
        'user_name': fields.String,
        'user_status': fields.String,
    })

member_profile = member_api.clone(
    'MemberProfile', user_profile, {
        'basic_profile': fields.Nested(basic_profile),
        'extra_profile': fields.List(fields.Nested(extra_profile))
    })