def test_regularrelatedfield_registry06(self): "Default is a sub-registry." user1 = self.user user2 = CremeUser( username='******', email='*****@*****.**', first_name='Chie', last_name='Uru', ) class MyFKField1(lv_form.ListViewSearchField): pass class MyFKField2(lv_form.ListViewSearchField): pass class MyRegistry(lv_search.AbstractListViewSearchFieldRegistry): def get_field(self, *, cell, user, **kwarg): cls = MyFKField1 if user.username == user1.username else MyFKField2 return cls(cell=cell, user=user) registry = lv_search.RegularRelatedFieldSearchRegistry( default=MyRegistry) cell = EntityCellRegularField.build(model=FakeContact, name='user') get_field = registry.get_field self.assertIsInstance(get_field(cell=cell, user=user1), MyFKField1) self.assertIsInstance(get_field(cell=cell, user=user2), MyFKField2)
def test_regularrelatedfield_registry03(self): "ForeignKey to entity." registry = lv_search.RegularRelatedFieldSearchRegistry() cell = EntityCellRegularField.build(model=FakeInvoiceLine, name='linked_invoice') self.assertIsInstance(registry.get_field(cell=cell, user=self.user), lv_form.EntityRelatedField)
def test_regularrelatedfield_registry02(self): "Register default." class MyFKField(lv_form.ListViewSearchField): pass registry = lv_search.RegularRelatedFieldSearchRegistry() \ .register_default(MyFKField) get_field = partial(registry.get_field, user=self.user) build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) self.assertIsInstance(get_field(cell=build_cell(name='sector')), MyFKField) self.assertEqual(MyFKField, registry.default_builder)
def test_regularrelatedfield_registry01(self): "Register by related model." class MyUserFKField(lv_form.ListViewSearchField): pass registry = lv_search.RegularRelatedFieldSearchRegistry() \ .register_related_model(model=CremeUser, sfield_builder=MyUserFKField, ) get_field = partial(registry.get_field, user=self.user) build_cell = partial(EntityCellRegularField.build, model=FakeContact) self.assertIsInstance(get_field(cell=build_cell(name='user')), MyUserFKField) self.assertIsInstance(get_field(cell=build_cell(name='is_user')), MyUserFKField) sector_field = get_field(cell=build_cell(name='sector')) self.assertEqual(lv_form.RegularRelatedField, type(sector_field)) builder = registry.builder_4_related_model self.assertIsNone(builder(FakeSector)) self.assertEqual(MyUserFKField, builder(CremeUser))