Exemplo n.º 1
0
    def test_wildcard02(self):
        "Wildcard in first group + layout."
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name', required=False)
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')
            fax   = forms.CharField(label='Fax')

        fbm = FieldBlockManager(
            {'id': 'names', 'label': 'Names', 'fields': '*', 'layout': LAYOUT_DUAL_SECOND},
            ('details', 'Details', ('phone', 'fax', 'cell')),
        )

        blocks = fbm.build(TestForm())
        name_block = blocks['names']
        self.assertEqual(LAYOUT_DUAL_SECOND, name_block.layout)
        self.assertListEqual(
            ['first_name', 'last_name'],
            [bfield.name for bfield in name_block.bound_fields]
        )
        self.assertListEqual(
            ['phone', 'fax', 'cell'],
            [bfield.name for bfield in blocks['details'].bound_fields]
        )
Exemplo n.º 2
0
    def test_basic_get_item02(self):
        "Constructor with dicts."
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name', required=False)
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')
            fax   = forms.CharField(label='Fax')

        template_details = 'creme_core/generics/blockform/field-block-IMPROVED.html'
        ctxt_details = {'some': 'info'}
        fbm = FieldBlockManager(
            {
                'id': 'names',
                'label': 'Names',
                'fields': ('first_name', 'last_name')
            }, {
                'id': 'details',
                'label': 'Details',
                'fields': ['cell', 'phone', 'fax'],
                'layout': LAYOUT_DUAL_FIRST,
                'template': template_details,
                'context': ctxt_details,
            },
        )
        form = TestForm()

        blocks = fbm.build(form)
        with self.assertNoException():
            names_group = blocks['names']

        self.assertIsInstance(names_group, BoundFieldBlocks.BoundFieldBlock)
        self.assertEqual('Names', names_group.label)
        self.assertEqual(LAYOUT_REGULAR, names_group.layout)
        self.assertEqual(
            'creme_core/generics/blockform/field-block.html',
            names_group.template_name,
        )
        self.assertIsNone(names_group.template_context)

        bfields = names_group.bound_fields
        self.assertEqual(2, len(bfields))

        bound_field1 = bfields[0]
        self.assertIsInstance(bound_field1, BoundField)
        self.assertEqual('first_name', bound_field1.name)

        self.assertEqual('last_name', bfields[1].name)

        # --
        details_group = blocks['details']
        self.assertEqual('Details', details_group.label)
        self.assertEqual(LAYOUT_DUAL_FIRST, details_group.layout)
        self.assertEqual(template_details, details_group.template_name)
        self.assertDictEqual(ctxt_details, details_group.template_context)
Exemplo n.º 3
0
    def test_wildcard03(self):
        "Several wildcards => error."
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name')
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')

        fbm = FieldBlockManager(
            ('names', 'Names', '*'),
            ('details', 'Details', '*'),
        )

        with self.assertRaises(ValueError) as cm:
            fbm.build(TestForm())

        self.assertEqual(
            f'Only one wildcard is allowed: {TestForm}',
            str(cm.exception)
        )
Exemplo n.º 4
0
    def test_basic_get_item01(self):
        "Constructor with tuples."
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name', required=False)
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')
            fax   = forms.CharField(label='Fax')

        fbm = FieldBlockManager(
            ('names',   'Names',   ('first_name', 'last_name')),
            ('details', 'Details', ['cell', 'phone', 'fax']),
        )
        form = TestForm()

        blocks = fbm.build(form)
        with self.assertNoException():
            names_group = blocks['names']

        self.assertIsInstance(names_group, BoundFieldBlocks.BoundFieldBlock)
        self.assertEqual('names', names_group.id)
        self.assertEqual('Names', names_group.label)
        self.assertEqual(LAYOUT_REGULAR, names_group.layout)

        names_bfields = names_group.bound_fields
        self.assertEqual(2, len(names_bfields))

        bound_field1 = names_bfields[0]
        self.assertIsInstance(bound_field1, BoundField)
        self.assertEqual('first_name', bound_field1.name)
        self.assertEqual('id_first_name', bound_field1.auto_id)

        # --
        self.assertEqual('last_name', names_bfields[1].name)

        # --
        with self.assertNoException():
            details_group = blocks['details']

        self.assertEqual('Details', details_group.label)
        self.assertListEqual(
            ['cell', 'phone', 'fax'],  # The order of the block info is used
            [bfield.name for bfield in details_group.bound_fields]
        )

        # ---
        with self.assertRaises(KeyError):
            # Already pop
            blocks['names']  # NOQA
Exemplo n.º 5
0
    def test_invalid_field01(self):
        class TestForm(forms.Form):
            last_name = forms.CharField(label='Last name')

        fbm = FieldBlockManager(
            ('names', 'Names', ('invalid', 'last_name')),
        )
        form = TestForm()

        with self.assertNoException():
            blocks = fbm.build(form)

        with self.assertNoException():
            group = blocks['names']

        self.assertListEqual(
            ['last_name'],
            [bfield.name for bfield in group.bound_fields],
        )
Exemplo n.º 6
0
    def test_wildcard01(self):
        "Wildcard in second group."
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name', required=False)
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')
            fax   = forms.CharField(label='Fax')

        fbm = FieldBlockManager(
            ('names',   'Names',   ('first_name', 'last_name')),
            ('details', 'Details', '*'),
        )

        blocks = fbm.build(TestForm())
        self.assertListEqual(
            ['first_name', 'last_name'],
            [bfield.name for bfield in blocks['names'].bound_fields],
        )
        self.assertListEqual(
            ['phone', 'cell', 'fax'],  # The order of the form-fields is used
            [bfield.name for bfield in blocks['details'].bound_fields],
        )
Exemplo n.º 7
0
    def test_basic_iter(self):
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name', required=False)
            last_name  = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell  = forms.CharField(label='Cell')
            fax   = forms.CharField(label='Fax')

        fbm = FieldBlockManager(
            ('names',   'Names',   ('first_name', 'last_name')),
            ('details', 'Details', ('cell', 'phone', 'fax')),
        )
        form = TestForm()

        with self.assertNoException():
            blocks_list = [*fbm.build(form)]

        self.assertEqual(2, len(blocks_list))

        names_group = blocks_list[0]
        self.assertIsInstance(names_group, BoundFieldBlocks.BoundFieldBlock)
        self.assertEqual('Names', names_group.label)

        self.assertEqual('Details', blocks_list[1].label)
Exemplo n.º 8
0
    def test_form_gather_blocks_for_layout(self):
        class TestForm(forms.Form):
            first_name = forms.CharField(label='First name')
            last_name = forms.CharField(label='Last name')
            phone = forms.CharField(label='Phone')
            cell = forms.CharField(label='Cell')
            fax = forms.CharField(label='Fax')
            address = forms.CharField(label='Address')

        form = TestForm()

        fbm1 = FieldBlockManager(('general', 'Information', '*'))
        self.assertGatheredEqual(
            [('regular', ['general'])],
            form_gather_blocks_for_layout(fbm1.build(form)),
        )

        fbm2 = FieldBlockManager(
            ('general', 'General', '*'),
            ('details', 'Details', ['phone', 'cell', 'fax']),
        )
        self.assertGatheredEqual(
            [('regular', ['general', 'details'])],
            form_gather_blocks_for_layout(fbm2.build(form)),
        )

        fbm3 = FieldBlockManager(
            {
                'id': 'names',
                'label': 'Names',
                'fields': ('last_name', 'first_name'),
                'layout': LAYOUT_DUAL_FIRST,
            },
            {
                'id': 'phones',
                'label': 'Phones',
                'fields': ('phone', 'cell'),
                'layout': LAYOUT_DUAL_SECOND,
            },
            ('other', 'Other', '*'),
        )
        self.assertGatheredEqual(
            [
                ('dual', (['names'], ['phones'])),
                ('regular', ['other']),
            ],
            form_gather_blocks_for_layout(fbm3.build(form)),
        )

        fbm4 = FieldBlockManager(
            {
                'id': 'names',
                'label': 'Names',
                'fields': ('last_name', 'first_name'),
                'layout': LAYOUT_DUAL_FIRST,
            },
            {
                'id': 'phones',
                'label': 'Phones',
                'fields': ('phone', 'cell'),
                'layout': LAYOUT_DUAL_SECOND,
            },
            {
                'id': 'address',
                'label': 'Address',
                'fields': ('address', ),
                'layout': LAYOUT_DUAL_FIRST,
            },
            ('other', 'Other', '*'),
        )
        self.assertGatheredEqual(
            [
                ('dual', (['names', 'address'], ['phones'])),
                ('regular', ['other']),
            ],
            form_gather_blocks_for_layout(fbm4.build(form)),
        )