Example #1
0
    def test_functionfield_registry05(self):
        "Sub-registry defined in FunctionField attribute."
        user1 = self.user
        user2 = CremeUser(
            username='******',
            email='*****@*****.**',
            first_name='Chie',
            last_name='Uru',
        )

        class MyFunField1(lv_form.ListViewSearchField):
            pass

        class MyFunField2(lv_form.ListViewSearchField):
            pass

        class MyFunRegistry(lv_search.AbstractListViewSearchFieldRegistry):
            def get_field(self, *, cell, user, **kwarg):
                cls = MyFunField1 if user.username == user1.username else MyFunField2

                return cls(cell=cell, user=user)

        class SearchablePhoneFunctionField(self.PhoneFunctionField):
            search_field_builder = MyFunRegistry

        funfield = SearchablePhoneFunctionField()
        cell = EntityCellFunctionField(model=FakeContact, func_field=funfield)

        registry = lv_search.FunctionFieldSearchRegistry()
        get_field = registry.get_field
        self.assertIsInstance(get_field(cell=cell, user=user1), MyFunField1)
        self.assertIsInstance(get_field(cell=cell, user=user2), MyFunField2)
Example #2
0
    def test_functionfield_registry01(self):
        "Default data"
        registry = lv_search.FunctionFieldSearchRegistry()

        funfield1 = self.PhoneFunctionField()
        cell1 = EntityCellFunctionField(model=FakeContact,
                                        func_field=funfield1)

        field = registry.get_field(cell=cell1, user=self.user)
        self.assertIsInstance(field, lv_form.ListViewSearchField)
        # self.assertFalse(field.widget.type)
        self.assertIsNone(registry.builder(funfield1))

        # ---
        class MyField(lv_form.ListViewSearchField):
            pass

        funfield2 = self.IsAdultFunctionField()
        cell2 = EntityCellFunctionField(model=FakeContact,
                                        func_field=funfield2)

        registry.register(ffield=funfield2, sfield_builder=MyField)
        self.assertIsInstance(registry.get_field(cell=cell2, user=self.user),
                              MyField)

        field1 = registry.get_field(cell=cell1, user=self.user)
        self.assertIsInstance(field1, lv_form.ListViewSearchField)
        self.assertNotIsInstance(field1, MyField)

        self.assertIsNone(registry.builder(funfield1))
        self.assertEqual(MyField, registry.builder(funfield2))
Example #3
0
    def test_functionfield_registry02(self):
        "Register in constructor."

        class MyField(lv_form.ListViewSearchField):
            pass

        registry = lv_search.FunctionFieldSearchRegistry(to_register=[
            (self.PhoneFunctionField, MyField)
        ], )

        cell = EntityCellFunctionField(model=FakeContact,
                                       func_field=self.PhoneFunctionField())
        self.assertIsInstance(registry.get_field(cell=cell, user=self.user),
                              MyField)
Example #4
0
    def test_functionfield_registry03(self):
        "Function field with default search-field."
        registry = lv_search.FunctionFieldSearchRegistry()

        class MySearchField1(lv_form.ListViewSearchField):
            pass

        class SearchablePhoneFunctionField(self.PhoneFunctionField):
            search_field_builder = MySearchField1

        funfield = SearchablePhoneFunctionField()
        cell = EntityCellFunctionField(model=FakeContact, func_field=funfield)
        self.assertIsInstance(registry.get_field(cell=cell, user=self.user),
                              MySearchField1)

        # ---
        class MySearchField2(lv_form.ListViewSearchField):
            pass

        registry.register(ffield=funfield, sfield_builder=MySearchField2)
        self.assertIsInstance(registry.get_field(cell=cell, user=self.user),
                              MySearchField2)