def test_regularfield_registry_default01(self): build_cell = partial(EntityCellRegularField.build, model=FakeContact) get_field = partial( lv_search.RegularFieldSearchRegistry().get_field, user=self.user, ) self.assertIsInstance(get_field(cell=build_cell(name='first_name')), lv_form.RegularCharField) self.assertIsInstance(get_field(cell=build_cell(name='is_a_nerd')), lv_form.RegularBooleanField) self.assertIsInstance(get_field(cell=build_cell(name='birthday')), lv_form.RegularDateField) self.assertIsInstance( get_field(cell=build_cell(model=FakeOrganisation, name='capital')), lv_form.RegularPositiveIntegerField) self.assertIsInstance( get_field(cell=build_cell(model=FakeInvoiceLine, name='discount')), lv_form.RegularDecimalField) # Sub field self.assertIsInstance( get_field(cell=build_cell(name='position__title')), lv_form.RegularCharField) # ForeignKey self.assertIsInstance(get_field(cell=build_cell(name='position')), lv_form.RegularRelatedField) # ManyToMany self.assertIsInstance(get_field(cell=build_cell(name='languages')), lv_form.RegularRelatedField)
def test_regularfield_registry06(self): "register_model_field(): sub-registry." user1 = self.user user2 = CremeUser( username='******', email='*****@*****.**', first_name='Chie', last_name='Uru', ) class MyField1(lv_form.ListViewSearchField): pass class MyField2(lv_form.ListViewSearchField): pass class MyRegistry(lv_search.AbstractListViewSearchFieldRegistry): def get_field(self, *, cell, user, **kwarg): cls = MyField1 if user.username == user1.username else MyField2 return cls(cell=cell, user=user) registry = lv_search.RegularFieldSearchRegistry()\ .register_model_field( model=FakeContact, field_name='first_name', sfield_builder=MyRegistry ) cell = EntityCellRegularField.build(model=FakeContact, name='first_name') get_field = registry.get_field self.assertIsInstance(get_field(cell=cell, user=user1), MyField1) self.assertIsInstance(get_field(cell=cell, user=user2), MyField2)
def test_regularfield_registry07(self): "Choices + sub-registry" user1 = self.user user2 = CremeUser( username='******', email='*****@*****.**', first_name='Chie', last_name='Uru', ) class MyChoiceField1(lv_form.ListViewSearchField): pass class MyChoiceField2(lv_form.ListViewSearchField): pass class MyChoiceRegistry(lv_search.AbstractListViewSearchFieldRegistry): def get_field(self, *, cell, user, **kwarg): cls = MyChoiceField1 if user.username == user1.username else MyChoiceField2 return cls(cell=cell, user=user) registry = lv_search.RegularFieldSearchRegistry( ).register_choice_builder(MyChoiceRegistry) cell = EntityCellRegularField.build(model=FakeInvoiceLine, name='discount_unit') get_field = registry.get_field self.assertIsInstance(get_field(cell=cell, user=user1), MyChoiceField1) self.assertIsInstance(get_field(cell=cell, user=user2), MyChoiceField2) self.assertIsInstance(registry.choice_builder, MyChoiceRegistry)
def test_regularfield_registry03(self): "Register by field." class LVField(lv_form.RegularCharField): pass build_cell = partial(EntityCellRegularField.build, model=FakeContact) fname_cell = build_cell(name='first_name') lname_cell = build_cell(name='last_name') registry = lv_search.RegularFieldSearchRegistry(to_register=()) registry.register_model_field( model=FakeContact, field_name='first_name', sfield_builder=LVField, ) fname_field = registry.get_field(cell=fname_cell, user=self.user) self.assertIsInstance(fname_field, LVField) self.assertEqual( LVField, registry.builder_4_model_field(model=FakeContact, field_name='first_name')) lname_field = registry.get_field(cell=lname_cell, user=self.user) self.assertIsInstance(lname_field, lv_form.ListViewSearchField) self.assertNotIsInstance(lname_field, LVField)
def test_regularfield_registry_default04(self): "ManyToManyField to entity." cell = EntityCellRegularField.build(model=FakeEmailCampaign, name='mailing_lists') self.assertIsInstance( lv_search.RegularFieldSearchRegistry().get_field(user=self.user, cell=cell), lv_form.EntityRelatedField)
def test_regularfield_registry_default03(self): "ForeignKey to entity." cell = EntityCellRegularField.build(model=FakeInvoiceLine, name='linked_invoice') self.assertIsInstance( lv_search.RegularFieldSearchRegistry().get_field(user=self.user, cell=cell), lv_form.EntityRelatedField)
def test_regularfield_registry_default02(self): "Choices." build_cell = partial(EntityCellRegularField.build, model=FakeInvoiceLine) get_field = partial(lv_search.RegularFieldSearchRegistry().get_field, user=self.user) self.assertIsInstance(get_field(cell=build_cell(name='item')), lv_form.RegularCharField) self.assertIsInstance(get_field(cell=build_cell(name='discount_unit')), lv_form.RegularChoiceField)
def test_regularfield_registry02(self): "Register by type => inheritance." class MyField(lv_form.RegularCharField): pass # NB: "capital" is a PositiveIntegerField (inheriting IntegerField) int_cell = EntityCellRegularField.build(model=FakeOrganisation, name='capital') registry = lv_search.RegularFieldSearchRegistry(to_register=()) registry.register_model_field_type(type=IntegerField, sfield_builder=MyField) int_field = registry.get_field(cell=int_cell, user=self.user) self.assertIsInstance(int_field, MyField)
def test_regularfield_registry05(self): "Choices field + field registered specifically." class MyChoiceField(lv_form.RegularChoiceField): pass class MyUnitField(lv_form.RegularChoiceField): pass registry = lv_search.RegularFieldSearchRegistry( choice_sfield_builder=MyChoiceField, ) registry.register_model_field( model=FakeInvoiceLine, field_name='discount_unit', sfield_builder=MyUnitField, ) unit_cell = EntityCellRegularField.build(model=FakeInvoiceLine, name='discount_unit') unit_field = registry.get_field(cell=unit_cell, user=self.user) self.assertIsInstance(unit_field, MyUnitField)
def test_regularfield_registry01(self): "Register by type." user = self.user build_cell = partial(EntityCellRegularField.build, model=FakeOrganisation) str_cell = build_cell(name='name') registry = lv_search.RegularFieldSearchRegistry(to_register=()) str_field = registry.get_field(cell=str_cell, user=user) self.assertIsInstance(str_field, lv_form.ListViewSearchField) self.assertEqual(str_cell, str_field.cell) self.assertEqual(user, str_field.user) # self.assertFalse(str_field.widget.type) self.assertIsNone(registry.builder_4_model_field_type(CharField)) self.assertIsNone( registry.builder_4_model_field(model=FakeOrganisation, field_name='name')) # --- class MyField(lv_form.RegularCharField): pass registry.register_model_field_type(type=CharField, sfield_builder=MyField) str_field = registry.get_field(cell=str_cell, user=self.user) self.assertIsInstance(str_field, MyField) self.assertEqual(MyField, registry.builder_4_model_field_type(CharField)) self.assertIsNone( registry.builder_4_model_field(model=FakeOrganisation, field_name='name')) int_cell = build_cell(name='capital') int_field = registry.get_field(cell=int_cell, user=self.user) self.assertIsInstance(int_field, lv_form.ListViewSearchField) self.assertNotIsInstance(int_field, MyField) self.assertIsNone(registry.builder_4_model_field_type(IntegerField))
def test_regularfield_registry04(self): "Choices" class MyCharField(lv_form.RegularCharField): pass class MyChoiceField(lv_form.RegularChoiceField): pass build_cell = partial(EntityCellRegularField.build, model=FakeInvoiceLine) registry = lv_search.RegularFieldSearchRegistry( to_register=[(CharField, MyCharField)], choice_sfield_builder=MyChoiceField, ) item_field = registry.get_field(cell=build_cell(name='item'), user=self.user) self.assertIsInstance(item_field, MyCharField) unit_field = registry.get_field(cell=build_cell(name='discount_unit'), user=self.user) self.assertIsInstance(unit_field, MyChoiceField)