コード例 #1
0
    def route_child(self, environ, start_response):
        req = webob.Request(environ)
        ctx = contexts.Ctx(req)

        variable_name, error = conv.pipe(conv.cleanup_line, conv.not_none)(req.urlvars.get("name"), state=ctx)
        child = self.child_from_node(ctx, unique_name=variable_name or u"")
        if error is not None:
            return wsgihelpers.not_found(ctx, body=child.render_not_found(ctx))(environ, start_response)

        return child.route(environ, start_response)
コード例 #2
0
    def route_child(self, environ, start_response):
        req = webob.Request(environ)
        ctx = contexts.Ctx(req)

        variable_name, error = conv.pipe(
            conv.cleanup_line,
            conv.not_none,
        )(req.urlvars.get('name'), state=ctx)
        child = self.child_from_node(ctx, unique_name=variable_name or u'')
        if error is not None:
            return wsgihelpers.not_found(ctx, body=child.render_not_found())(
                environ, start_response)

        return child.route(environ, start_response)
コード例 #3
0
    def trace(self, req):
        ctx = contexts.Ctx(req)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx)

        content_type = req.content_type
        if content_type is not None:
            content_type = content_type.split(';', 1)[0].strip()
        if req.method == 'POST' and content_type == 'application/json':
            simulation, error = conv.pipe(
                conv.make_input_to_json(object_pairs_hook = collections.OrderedDict),
                conv.test_isinstance(dict),
                conv.not_none,
                )(req.body, state = ctx)
            if error is not None:
                return wsgihelpers.respond_json(ctx,
                    collections.OrderedDict(sorted(dict(
                        apiVersion = '1.0',
                        error = collections.OrderedDict(sorted(dict(
                            code = 400,  # Bad Request
                            errors = [error],
                            message = ctx._(u'Invalid JSON in request POST body'),
                            ).iteritems())),
                        method = req.script_name,
                        params = req.body,
                        url = req.url.decode('utf-8'),
                        ).iteritems())),
                    headers = headers,
                    )
            api_url = conf['urls.api']
        else:
            # URL-encoded GET or POST.
            params = req.params
            inputs = dict(
                api_url = params.get('api_url'),
                simulation = params.get('simulation'),
                )
            data, errors = conv.struct(
                dict(
                    api_url = conv.pipe(
                        conv.make_input_to_url(error_if_fragment = True, error_if_path = True, error_if_query = True,
                            full = True),
                        conv.default(conf['urls.api']),
                        ),
                    simulation = conv.pipe(
                        conv.make_input_to_json(object_pairs_hook = collections.OrderedDict),
                        conv.test_isinstance(dict),
                        # When None, use default value defined in atom.mako.
                        ),
                    ),
                )(inputs, state = ctx)
            if errors is not None:
                return wsgihelpers.respond_json(ctx,
                    collections.OrderedDict(sorted(dict(
                        apiVersion = '1.0',
                        context = inputs.get('context'),
                        error = collections.OrderedDict(sorted(dict(
                            code = 400,  # Bad Request
                            errors = [errors],
                            message = ctx._(u'Bad parameters in request'),
                            ).iteritems())),
                        method = req.script_name,
                        params = inputs,
                        url = req.url.decode('utf-8'),
                        ).iteritems())),
                    headers = headers,
                    )
            api_url = data['api_url']
            simulation = data['simulation']
        simulation_text = unicode(json.dumps(simulation, encoding = 'utf-8', ensure_ascii = False, indent = 2)) \
            if simulation is not None else None

        response = req.response
        response.headers.update(headers)
        return self.render(api_url = api_url, simulation_text = simulation_text)
コード例 #4
0
    def trace(self, req):
        ctx = contexts.Ctx(req)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx)

        content_type = req.content_type
        if content_type is not None:
            content_type = content_type.split(';', 1)[0].strip()
        if req.method == 'POST' and content_type == 'application/json':
            simulation, error = conv.pipe(
                conv.make_input_to_json(
                    object_pairs_hook=collections.OrderedDict),
                conv.test_isinstance(dict),
                conv.not_none,
            )(req.body, state=ctx)
            if error is not None:
                return wsgihelpers.respond_json(
                    ctx,
                    collections.OrderedDict(
                        sorted(
                            dict(
                                apiVersion='1.0',
                                error=collections.OrderedDict(
                                    sorted(
                                        dict(
                                            code=400,  # Bad Request
                                            errors=[error],
                                            message=ctx.
                                            _(u'Invalid JSON in request POST body'
                                              ),
                                        ).iteritems())),
                                method=req.script_name,
                                params=req.body,
                                url=req.url.decode('utf-8'),
                            ).iteritems())),
                    headers=headers,
                )
            api_url = conf['urls.api']
        else:
            # URL-encoded GET or POST.
            params = req.params
            inputs = dict(
                api_url=params.get('api_url'),
                simulation=params.get('simulation'),
            )
            data, errors = conv.struct(
                dict(
                    api_url=conv.pipe(
                        conv.make_input_to_url(error_if_fragment=True,
                                               error_if_path=True,
                                               error_if_query=True,
                                               full=True),
                        conv.default(conf['urls.api']),
                    ),
                    simulation=conv.pipe(
                        conv.make_input_to_json(
                            object_pairs_hook=collections.OrderedDict),
                        conv.test_isinstance(dict),
                        # When None, use default value defined in atom.mako.
                    ),
                ), )(inputs, state=ctx)
            if errors is not None:
                return wsgihelpers.respond_json(
                    ctx,
                    collections.OrderedDict(
                        sorted(
                            dict(
                                apiVersion='1.0',
                                context=inputs.get('context'),
                                error=collections.OrderedDict(
                                    sorted(
                                        dict(
                                            code=400,  # Bad Request
                                            errors=[errors],
                                            message=ctx._(
                                                u'Bad parameters in request'),
                                        ).iteritems())),
                                method=req.script_name,
                                params=inputs,
                                url=req.url.decode('utf-8'),
                            ).iteritems())),
                    headers=headers,
                )
            api_url = data['api_url']
            simulation = data['simulation']
        simulation_text = unicode(json.dumps(simulation, encoding = 'utf-8', ensure_ascii = False, indent = 2)) \
            if simulation is not None else None

        response = req.response
        response.headers.update(headers)
        return self.render(api_url=api_url, simulation_text=simulation_text)
コード例 #5
0
    def trace(self, req):
        ctx = contexts.Ctx(req)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx)

        content_type = req.content_type
        if content_type is not None:
            content_type = content_type.split(';', 1)[0].strip()
        if req.method == 'POST' and content_type == 'application/json':
            simulation, error = conv.pipe(
                conv.make_input_to_json(
                    object_pairs_hook=collections.OrderedDict),
                conv.test_isinstance(dict),
                conv.not_none,
            )(req.body, state=ctx)
            if error is not None:
                return wsgihelpers.respond_json(
                    ctx,
                    collections.OrderedDict(
                        sorted(
                            dict(
                                apiVersion='1.0',
                                error=collections.OrderedDict(
                                    sorted(
                                        dict(
                                            code=400,  # Bad Request
                                            errors=[error],
                                            message=ctx.
                                            _(u'Invalid JSON in request POST body'
                                              ),
                                        ).iteritems())),
                                method=req.script_name,
                                params=req.body,
                                url=req.url.decode('utf-8'),
                            ).iteritems())),
                    headers=headers,
                )
            api_url = conf['urls.api']
        else:
            # URL-encoded GET or POST.
            params = req.params
            inputs = dict(
                api_url=params.get('api_url'),
                simulation=params.get('simulation'),
                simulation_url=params.get('simulation_url'),
            )
            data, errors = conv.struct(
                dict(
                    api_url=conv.pipe(
                        conv.make_input_to_url(error_if_fragment=True,
                                               error_if_path=True,
                                               error_if_query=True,
                                               full=True),
                        conv.default(conf['urls.api']),
                    ),
                    simulation=conv.pipe(
                        conv.make_input_to_json(
                            object_pairs_hook=collections.OrderedDict),
                        conv.test_isinstance(dict),
                        # When None, use default value defined in atom.mako.
                    ),
                    simulation_url=conv.make_input_to_url(full=True),
                ), )(inputs, state=ctx)
            if errors is not None:
                return wsgihelpers.respond_json(
                    ctx,
                    collections.OrderedDict(
                        sorted(
                            dict(
                                apiVersion='1.0',
                                context=inputs.get('context'),
                                error=collections.OrderedDict(
                                    sorted(
                                        dict(
                                            code=400,  # Bad Request
                                            errors=[errors],
                                            message=ctx._(
                                                u'Bad parameters in request'),
                                        ).iteritems())),
                                method=req.script_name,
                                params=inputs,
                                url=req.url.decode('utf-8'),
                            ).iteritems())),
                    headers=headers,
                )
            api_url = data['api_url']
            if data['simulation_url'] is not None:
                # TODO Implement this call client-side to allow URLs with localhost
                request = urllib2.Request(
                    data['simulation_url'],
                    headers={'User-Agent': 'OpenFisca-Trace-Tool'})
                try:
                    response = urllib2.urlopen(request)
                except (urllib2.HTTPError, urllib2.URLError) as response:
                    return wsgihelpers.bad_request(
                        ctx,
                        message=ctx._(u'Unable to open simulation_url'),
                        headers=headers,
                    )
                response_text = response.read()
                try:
                    simulation = json.loads(
                        response_text,
                        object_pairs_hook=collections.OrderedDict)
                except ValueError as error:
                    return wsgihelpers.bad_request(
                        ctx,
                        message=unicode(error),
                        headers=headers,
                    )
            else:
                simulation = data['simulation']
        simulation_text = json.dumps(simulation, encoding = 'utf-8', ensure_ascii = False, indent = 2) \
            if simulation is not None else None

        response = req.response
        response.headers.update(headers)
        return self.render(api_url=api_url, simulation_text=simulation_text)
コード例 #6
0
ファイル: trace.py プロジェクト: dattaz/openfisca-web-site
    def trace(self, req):
        ctx = contexts.Ctx(req)
        headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx)

        content_type = req.content_type
        if content_type is not None:
            content_type = content_type.split(';', 1)[0].strip()
        if req.method == 'POST' and content_type == 'application/json':
            simulation, error = conv.pipe(
                conv.make_input_to_json(object_pairs_hook = collections.OrderedDict),
                conv.test_isinstance(dict),
                conv.not_none,
                )(req.body, state = ctx)
            if error is not None:
                return wsgihelpers.respond_json(ctx,
                    collections.OrderedDict(sorted(dict(
                        apiVersion = '1.0',
                        error = collections.OrderedDict(sorted(dict(
                            code = 400,  # Bad Request
                            errors = [error],
                            message = ctx._(u'Invalid JSON in request POST body'),
                            ).iteritems())),
                        method = req.script_name,
                        params = req.body,
                        url = req.url.decode('utf-8'),
                        ).iteritems())),
                    headers = headers,
                    )
            api_url = conf['urls.api']
        else:
            # URL-encoded GET or POST.
            params = req.params
            inputs = dict(
                api_url = params.get('api_url'),
                simulation = params.get('simulation'),
                simulation_url = params.get('simulation_url'),
                )
            data, errors = conv.struct(
                dict(
                    api_url = conv.pipe(
                        conv.make_input_to_url(error_if_fragment = True, error_if_path = True, error_if_query = True,
                            full = True),
                        conv.default(conf['urls.api']),
                        ),
                    simulation = conv.pipe(
                        conv.make_input_to_json(object_pairs_hook = collections.OrderedDict),
                        conv.test_isinstance(dict),
                        # When None, use default value defined in atom.mako.
                        ),
                    simulation_url = conv.make_input_to_url(full = True),
                    ),
                )(inputs, state = ctx)
            if errors is not None:
                return wsgihelpers.respond_json(ctx,
                    collections.OrderedDict(sorted(dict(
                        apiVersion = '1.0',
                        context = inputs.get('context'),
                        error = collections.OrderedDict(sorted(dict(
                            code = 400,  # Bad Request
                            errors = [errors],
                            message = ctx._(u'Bad parameters in request'),
                            ).iteritems())),
                        method = req.script_name,
                        params = inputs,
                        url = req.url.decode('utf-8'),
                        ).iteritems())),
                    headers = headers,
                    )
            api_url = data['api_url']
            if data['simulation_url'] is not None:
                # TODO Implement this call client-side to allow URLs with localhost
                request = urllib2.Request(data['simulation_url'], headers = {'User-Agent': 'OpenFisca-Trace-Tool'})
                try:
                    response = urllib2.urlopen(request)
                except (urllib2.HTTPError, urllib2.URLError) as response:
                    return wsgihelpers.bad_request(ctx,
                        message = ctx._(u'Unable to open simulation_url'),
                        headers = headers,
                        )
                response_text = response.read()
                try:
                    simulation = json.loads(response_text, object_pairs_hook = collections.OrderedDict)
                except ValueError as error:
                    return wsgihelpers.bad_request(ctx,
                        message = unicode(error),
                        headers = headers,
                        )
            else:
                simulation = data['simulation']
        simulation_text = json.dumps(simulation, encoding = 'utf-8', ensure_ascii = False, indent = 2) \
            if simulation is not None else None

        response = req.response
        response.headers.update(headers)
        return self.render(api_url = api_url, simulation_text = simulation_text)