Beispiel #1
0
    def test_bulk_to_python(self):
        Contact.objects.bulk_create(
            Contact(pk=i, heading=str(i)) for i in range(10)
        )

        class TestStreamBlock(blocks.StreamBlock):
            contacts = ContactExpandableGroup()

        block = TestStreamBlock()
        value = blocks.StreamValue(
            block,
            [
                {
                    'type': 'contacts',
                    'value': {
                        'group_title': 'First',
                        'contacts': list(range(5)),
                    },
                },
                {
                    'type': 'contacts',
                    'value': {
                        'group_title': 'Second',
                        'contacts': list(range(5, 10))
                    },
                },
            ],
            is_lazy=True
        )

        # This verifies that the block does a single database query to retrieve
        # all 10 Contacts, even though they are split up between two different
        # ContactExpandableGroup blocks.
        with self.assertNumQueries(1):
            block.render(value)
    def generate(self, step, params):
        result = defaultdict(lambda: defaultdict(lambda: defaultdict()))
        subfactory = self.get_factory()

        for key, value in params.items():
            parts = key.split('__', 2)
            index = parts[0]
            if index.isdigit():
                index = int(index)
            else:
                continue

            block_name = parts[1]

            if len(parts) == 2:
                result[index][block_name][block_name] = value
            if len(parts) == 3:
                param = parts[2]
                result[index][block_name][block_name + "__" + param] = value

        retval = []
        for index, block_items in sorted(result.items()):
            for block_name, block_params in block_items.items():
                block_params['_block_name'] = block_name
                step_builder = factory.builder.StepBuilder(
                    subfactory._meta, block_params, "build")
                built = step_builder.build()
                retval.append((block_name, built))

        return blocks.StreamValue(subfactory._meta.model(), retval)
Beispiel #3
0
    def test_doesnt_pull_in_media_for_nonexistent_child_blocks(self):
        page = BrowsePage()
        page.content = blocks.StreamValue(page.content.stream_block, [
            {
                'type': 'full_width_text',
                'value': [],
            },
        ], True)

        # The page media should only include the default BrowsePae media, and
        # shouldn't add any additional files because of the FullWithText.
        self.assertEqual(page.media, ['secondary-navigation.js'])
Beispiel #4
0
    def test_page_pulls_in_child_block_media(self):
        page = CFGOVPage()
        page.sidefoot = blocks.StreamValue(page.sidefoot.stream_block, [
            {
                'type': 'email_signup',
                'value': {
                    'heading': 'Heading'
                }
            },
        ], True)

        self.assertEqual(page.media, ['email-signup.js'])
Beispiel #5
0
    def test_serve_post_valid_calls_feedback_block_handler(self):
        """A valid post should call the feedback block handler.

        This returns a redirect to the calling page and also uses the
        Django messages framework to set a message.
        """
        page = BrowsePage(title='test', slug='test')
        page.content = blocks.StreamValue(page.content.stream_block,
                                          [{
                                              'type': 'feedback',
                                              'value': 'something'
                                          }], True)
        save_new_page(page)

        request = self.factory.post('/', {'form_id': 'form-content-0'})
        SessionMiddleware().process_request(request)
        MessageMiddleware().process_request(request)

        response = page.serve_post(request)

        self.assertEqual((response.status_code, response['Location']),
                         (302, request.path))