decorators.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Mocha 作者: mardix 项目源码 文件源码
def cors(*args, **kwargs):
    """
    A wrapper around flask-cors cross_origin, to also act on classes

    **An extra note about cors, a response must be available before the
    cors is applied. Dynamic return is applied after the fact, so use the
    decorators, json, xml, or return self.render() for txt/html
    ie:
    @cors()
    class Index(Mocha):
        def index(self):
            return self.render()

        @json
        def json(self):
            return {}

    class Index2(Mocha):
        def index(self):
            return self.render()

        @cors()
        @json
        def json(self):
            return {}


    :return:
    """
    def decorator(fn):
        cors_fn = flask_cors.cross_origin(automatic_options=False, *args, **kwargs)
        if inspect.isclass(fn):
            apply_function_to_members(fn, cors_fn)
        else:
            return cors_fn(fn)
        return fn
    return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号