Example #1
0
        def _wrapper(*args, **kwargs):
            if not EasyMode.config(current_app).XHR_API_ENABLE:
                view_result = f(*args, **kwargs)
                if not view_result:
                    abort(403)
                return view_result

            a = allow_http or EasyMode.config(current_app).XHR_API_ALLOW_HTTP

            if not request.is_xhr and not a:
                raise XHRError('XHR endpoints must be called asynchronously.',
                               status_code=500)

            g.xhr = XHR()
            f(*args, **kwargs)
            g.xhr.messages.extend(get_flashed_messages(with_categories=True))

            return jsonify(data=g.xhr.data,
                           messages=g.xhr.messages,
                           html=g.xhr.html)
Example #2
0
        def _wrapper(*args, **kwargs):
            if not EasyMode.config(current_app).XHR_API_ENABLE:
                view_result = f(*args, **kwargs)
                if not view_result:
                    abort(403)
                return view_result

            a = allow_http or EasyMode.config(current_app).XHR_API_ALLOW_HTTP

            if not request.is_xhr and not a:
                raise XHRError(
                    'XHR endpoints must be called asynchronously.',
                    status_code=500)

            g.xhr = XHR()
            f(*args, **kwargs)
            g.xhr.messages.extend(get_flashed_messages(with_categories=True))

            return jsonify(data=g.xhr.data,
                           messages=g.xhr.messages,
                           html=g.xhr.html)
Example #3
0
        def _wrapper(*args, **kwargs):
            if not EasyMode.config(current_app).DI_ENABLE:
                return f(*args, **kwargs)

            # most to least specific to eliminate ambiguity when dealing with
            # nested prefix class names
            classes_by_len = sorted(classes, key=len)
            classes_by_len.reverse()

            injections = {}
            options.setdefault('nomatch', 'skip')
            options.setdefault('lists', 'denote')

            def _param_to_cls_prop_pair(param):
                for cls_name in classes_by_len:
                    r = param.replace(cls_name, '')
                    if r != param:
                        return (cls_name, r[1:])

            def _extract_injections(kvps):
                for k, v in kvps.iteritems():
                    try:
                        cls_name, prop_name = _param_to_cls_prop_pair(k)
                    except TypeError:
                        continue

                    injections[cls_name]['params'].append(k)
                    injections[cls_name]['conditions'].append((prop_name, v))

            for cls_name in classes:
                try:
                    cls = EasyMode._injectables[cls_name]
                except KeyError:
                    raise RuntimeError(
                        'Class "%s" has not been added as injectable. \
                        Use EasyMode.add_injectable(cls_name).' % cls_name)

                injections.setdefault(
                    cls_name, {'class': cls, 'params': [], 'conditions': []})

            sources = {
                'json': request.get_json(silent=True) or {},
                'form': request.form.to_dict(),
                'query_string': request.args,
                'params': kwargs
            }
            scan = EasyMode.config(current_app).DI_SCAN

            [_extract_injections(sources[s]) for s in scan]

            def _load(cls, cnd=[]):
                try:
                    o = cls.load(cnd)
                except AttributeError:
                    raise RuntimeError(
                        'To use %s with dependency injection, the \
                        class must either define a load(cls, \
                        conditions, **kwargs) interface or just \
                        inherit from the provided mixin.'
                        % cls.__name__)
                return o

            for cls_name, i in injections.iteritems():
                if cls_name in classes:
                    o = None
                    nomatch = options.get('nomatch')
                    if not i['conditions'] and nomatch == 'load':
                        o = _load(i['class'])

                    if i['conditions']:
                        o = _load(i['class'], i['conditions'])

                        if not o and nomatch == 'error':
                            return abort(400)

                    if type(o) is list and options.get('lists') == 'denote':
                        cls_name = cls_name + '_list'

                    if options.get('as_args'):
                        kwargs[cls_name] = o
                    else:
                        setattr(g, cls_name, o)

                    [kwargs.pop(p, None) for p in i['params']]

            return f(*args, **kwargs)
Example #4
0
        def _wrapper(*args, **kwargs):
            if not EasyMode.config(current_app).DI_ENABLE:
                return f(*args, **kwargs)

            # most to least specific to eliminate ambiguity when dealing with
            # nested prefix class names
            classes_by_len = sorted(classes, key=len)
            classes_by_len.reverse()

            injections = {}
            options.setdefault('nomatch', 'skip')
            options.setdefault('lists', 'denote')

            def _param_to_cls_prop_pair(param):
                for cls_name in classes_by_len:
                    r = param.replace(cls_name, '')
                    if r != param:
                        return (cls_name, r[1:])

            def _extract_injections(kvps):
                for k, v in kvps.iteritems():
                    try:
                        cls_name, prop_name = _param_to_cls_prop_pair(k)
                    except TypeError:
                        continue

                    injections[cls_name]['params'].append(k)
                    injections[cls_name]['conditions'].append((prop_name, v))

            for cls_name in classes:
                try:
                    cls = EasyMode._injectables[cls_name]
                except KeyError:
                    raise RuntimeError(
                        'Class "%s" has not been added as injectable. \
                        Use EasyMode.add_injectable(cls_name).' % cls_name)

                injections.setdefault(cls_name, {
                    'class': cls,
                    'params': [],
                    'conditions': []
                })

            sources = {
                'json': request.get_json(silent=True) or {},
                'form': request.form.to_dict(),
                'query_string': request.args,
                'params': kwargs
            }
            scan = EasyMode.config(current_app).DI_SCAN

            [_extract_injections(sources[s]) for s in scan]

            def _load(cls, cnd=[]):
                try:
                    o = cls.load(cnd)
                except AttributeError:
                    raise RuntimeError(
                        'To use %s with dependency injection, the \
                        class must either define a load(cls, \
                        conditions, **kwargs) interface or just \
                        inherit from the provided mixin.' % cls.__name__)
                return o

            for cls_name, i in injections.iteritems():
                if cls_name in classes:
                    o = None
                    nomatch = options.get('nomatch')
                    if not i['conditions'] and nomatch == 'load':
                        o = _load(i['class'])

                    if i['conditions']:
                        o = _load(i['class'], i['conditions'])

                        if not o and nomatch == 'error':
                            return abort(400)

                    if type(o) is list and options.get('lists') == 'denote':
                        cls_name = cls_name + '_list'

                    if options.get('as_args'):
                        kwargs[cls_name] = o
                    else:
                        setattr(g, cls_name, o)

                    [kwargs.pop(p, None) for p in i['params']]

            return f(*args, **kwargs)
Example #5
0
def _setup():
    app = create_app()
    em = EasyMode()
    em.init_app(app)
    return (em, app)
Example #6
0
def _setup():
    app = create_app()
    em = EasyMode()
    em.init_app(app)
    return (em, app)