示例#1
0
    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)
示例#2
0
    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))
示例#3
0
    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)
示例#4
0
    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)