def test_relationtype_registry06(self): "Register a default sub-registry." user1 = self.user user2 = CremeUser( username='******', email='*****@*****.**', first_name='Chie', last_name='Uru', ) class MyRelationField1(lv_form.ListViewSearchField): pass class MyRelationField2(lv_form.ListViewSearchField): pass class MyRelRegistry(lv_search.AbstractListViewSearchFieldRegistry): def get_field(self, *, cell, user, **kwarg): cls = MyRelationField1 if user.username == user1.username else MyRelationField2 return cls(cell=cell, user=user) registry = lv_search.RelationSearchRegistry().register_default( MyRelRegistry) cell = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) get_field = registry.get_field self.assertIsInstance(get_field(cell=cell, user=user1), MyRelationField1) self.assertIsInstance(get_field(cell=cell, user=user2), MyRelationField2)
def test_relationtype_registry01(self): "Default data + register() method." registry = lv_search.RelationSearchRegistry() cell1 = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) field = registry.get_field(cell=cell1, user=self.user) self.assertIsInstance(field, lv_form.RelationField) self.assertIsNone(registry.builder(REL_SUB_HAS)) self.assertEqual(lv_form.RelationField, registry.default_builder) # --- class MyRelationField(lv_form.ListViewSearchField): pass rtype2 = RelationType.create( ('test-subject_loves', 'loves'), ('test-object_loved', 'is loved by'), )[0] cell2 = EntityCellRelation(model=FakeContact, rtype=rtype2) registry.register(rtype_id=rtype2.id, sfield_builder=MyRelationField) self.assertIsInstance(registry.get_field(cell=cell2, user=self.user), MyRelationField) self.assertIsInstance(registry.get_field(cell=cell1, user=self.user), lv_form.RelationField) self.assertIsNone(registry.builder(REL_SUB_HAS)) self.assertEqual(MyRelationField, registry.builder(rtype2.id))
def test_cell_registry_relation02(self): "Default data." registry = lv_search.ListViewSearchFieldRegistry() cell = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) field = registry.get_field(cell=cell, user=self.user) self.assertIsInstance(field, lv_form.RelationField)
def test_relationtype_registry03(self): "Set default." class MyRelationField(lv_form.ListViewSearchField): pass registry = lv_search.RelationSearchRegistry(default=MyRelationField) self.assertEqual(MyRelationField, registry.default_builder) cell = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) self.assertIsInstance(registry.get_field(cell=cell, user=self.user), MyRelationField)
def test_relationtype_registry02(self): "Register in constructor." class MyRelationField(lv_form.ListViewSearchField): pass registry = lv_search.RelationSearchRegistry(to_register=[ (REL_SUB_HAS, MyRelationField), ]) cell = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) self.assertIsInstance(registry.get_field(cell=cell, user=self.user), MyRelationField)
def test_cell_registry_relation01(self): cell = EntityCellRelation.build(model=FakeContact, rtype_id=REL_SUB_HAS) registry = lv_search.ListViewSearchFieldRegistry(to_register=()) field = registry.get_field(cell=cell, user=self.user) self.assertIsInstance(field, lv_form.ListViewSearchField) class MyField(lv_form.RelationField): pass class Registry: def get_field(this, *, cell, user): return MyField(cell=cell, user=user) registry.register(cell_id=EntityCellRelation.type_id, registry_class=Registry) field = registry.get_field(cell=cell, user=self.user) self.assertIsInstance(field, MyField)
def test_relation(self): "EntityCellRelation are not sortable." sorter = QuerySorter() field_name = 'name' cells = [ EntityCellRegularField.build(model=FakeOrganisation, name=field_name), EntityCellRelation.build(model=FakeOrganisation, rtype_id=REL_SUB_HAS), ] sortinfo = sorter.get( model=FakeInvoice, cells=cells, cell_key=cells[1].key, order=Order(), ) self.assertEqual((field_name, 'cremeentity_ptr_id'), sortinfo.field_names) self.assertEqual(cells[0].key, sortinfo.main_cell_key) self.assertTrue(sortinfo.main_order.asc)