Ejemplo n.º 1
0
    def get_raw_code(cls, widget_id, params=None):
        """Gets the raw code for a parameterized widget."""
        if params is None:
            params = {}

        widget = cls.get(widget_id)

        # Parameters used to generate the raw code for the widget.
        parameters = {}
        for param in widget.params:
            parameters[param.name] = params.get(param.name, utils.convert_to_js_string(param.value))

        return utils.parse_with_jinja(widget.template, parameters)
Ejemplo n.º 2
0
    def test_convert_to_js_string(self):
        """Test convert_to_js_string method."""
        expected_values = [
            ('a', '\\"a\\"'),
            (2, '2'),
            (5.5, '5.5'),
            ("'", '\\"\\\'\\"'),
            (u'¡Hola!', '\\"\\\\u00a1Hola!\\"'),
            (['a', '¡Hola!', 2], '[\\"a\\", \\"\\\\u00a1Hola!\\", 2]'),
            ({'a': 4, '¡Hola!': 2}, '{\\"a\\": 4, \\"\\\\u00a1Hola!\\": 2}'),
            ('', '\\"\\"'),
            (None, 'null'),
            (['a', {'b': 'c', 'd': ['e', None]}],
                '[\\"a\\", {\\"b\\": \\"c\\", \\"d\\": [\\"e\\", null]}]')
        ]

        for tup in expected_values:
            self.assertEqual(utils.convert_to_js_string(tup[0]), tup[1])
Ejemplo n.º 3
0
    def get_raw_code(cls, widget_id, params=None):
        """Gets the raw code for a parameterized widget.

        This method should be called on a subclass of Widget.
        """
        if cls.__name__ == 'Widget':
            raise NotImplementedError
        if params is None:
            params = {}

        widget = cls.get(widget_id)

        # Parameters used to generate the raw code for the widget.
        # TODO(sll): Why do we convert only the default value to a JS string?
        parameters = dict(
            (param.name, params.get(
                param.name, utils.convert_to_js_string(param.value))
             ) for param in widget.params)
        return utils.parse_with_jinja(widget.template, parameters)