Exemple #1
0
    def test_recursive_arg_packing(self):
        dangerously_in_love = Album("Dangerously in Love", [
            Artist("Beyoncé"),
        ])
        ctx = JSContext()
        result = ctx.pack(dangerously_in_love)

        self.assertEqual(
            result, {
                '_type':
                'music.Album',
                '_args': [
                    "Dangerously in Love",
                    [
                        {
                            '_type': 'music.Artist',
                            '_args': ["Beyoncé"]
                        },
                    ]
                ]
            })

        self.assertIn('music_player.js', str(ctx.media))
Exemple #2
0
class BlockWidget(forms.Widget):
    """Wraps a block object as a widget so that it can be incorporated into a Django form"""

    def __init__(self, block_def, attrs=None):
        super().__init__(attrs=attrs)
        self.block_def = block_def
        self._js_context = None

    def _build_block_json(self):
        self._js_context = JSContext()
        self._block_json = json.dumps(self._js_context.pack(self.block_def))

    @property
    def js_context(self):
        if self._js_context is None:
            self._build_block_json()

        return self._js_context

    @property
    def block_json(self):
        if self._js_context is None:
            self._build_block_json()

        return self._block_json

    def render_with_errors(self, name, value, attrs=None, errors=None, renderer=None):
        value_json = json.dumps(self.block_def.get_form_state(value))

        if errors:
            errors_json = json.dumps(self.js_context.pack(errors.as_data()))
        else:
            errors_json = '[]'

        return format_html(
            """
                <div id="{id}" data-block="{block_json}" data-value="{value_json}" data-errors="{errors_json}"></div>
                <script>
                    initBlockWidget('{id}');
                </script>
            """,
            id=name, block_json=self.block_json, value_json=value_json, errors_json=errors_json
        )

    def render(self, name, value, attrs=None, renderer=None):
        return self.render_with_errors(name, value, attrs=attrs, errors=None, renderer=renderer)

    @cached_property
    def media(self):
        return self.js_context.media + forms.Media(
            js=[
                # needed for initBlockWidget, although these will almost certainly be
                # pulled in by the block adapters too
                versioned_static('wagtailadmin/js/telepath/telepath.js'),
                versioned_static('wagtailadmin/js/telepath/blocks.js'),
            ],
            css={'all': [
                versioned_static('wagtailadmin/css/panels/streamfield.css'),
            ]}
        )

    def value_from_datadict(self, data, files, name):
        return self.block_def.value_from_datadict(data, files, name)

    def value_omitted_from_data(self, data, files, name):
        return self.block_def.value_omitted_from_data(data, files, name)
Exemple #3
0
    def test_grouper_object_collisions(self):
        """
        Certain functions such as itertools.groupby will cause new objects (namely, tuples and
        custom itertools._grouper iterables) to be created in the course of iterating over the
        object tree. If we're not careful, these will be released and the memory reallocated to
        new objects while we're still iterating, leading to ID collisions.
        """
        # create 100 Ark objects all with distinct animals (no object references are re-used)
        arks = [
            Ark([
                {
                    'type': 'lion',
                    'name': 'Simba %i' % i
                },
                {
                    'type': 'lion',
                    'name': 'Nala %i' % i
                },
                {
                    'type': 'dog',
                    'name': 'Lady %i' % i
                },
                {
                    'type': 'dog',
                    'name': 'Tramp %i' % i
                },
            ]) for i in range(0, 100)
        ]

        ctx = JSContext()
        result = ctx.pack(arks)

        self.assertEqual(len(result), 100)
        for i, ark in enumerate(result):
            # each object should be represented in full, with no _id or _ref keys
            self.assertEqual(
                ark, {
                    '_type':
                    'boats.Ark',
                    '_args': [[
                        [
                            'lion',
                            [{
                                'type': 'lion',
                                'name': 'Simba %i' % i
                            }, {
                                'type': 'lion',
                                'name': 'Nala %i' % i
                            }]
                        ],
                        [
                            'dog',
                            [{
                                'type': 'dog',
                                'name': 'Lady %i' % i
                            }, {
                                'type': 'dog',
                                'name': 'Tramp %i' % i
                            }]
                        ],
                    ]]
                })