Python django.utils.six 模块,add_metaclass() 实例源码

我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用django.utils.six.add_metaclass()

项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def get_meta_node_class_for_model(model, for_concrete_model=True):
    """
    Meta nodes are used to generate a map of the relationships
    in the database. There's only a single MetaNode per model.
    :param model: Django model class.
    :returns: A ``StructuredNode`` class.
    """
    if for_concrete_model:
        model = model._meta.concrete_model

    if model in __meta_cache__:
        return __meta_cache__[model]
    else:
        @six.add_metaclass(MetaNodeMeta)
        class MetaNode(MetaNodeMixin, StructuredNode):
            __metaclass_model__ = model

            class Meta:
                model = None  # Will pick model from parent class __metaclass_model__ attribute

        __meta_cache__[model] = MetaNode
        return MetaNode
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def get_node_class_for_model(model, for_concrete_model=True):
    """
    Model nodes represent a model instance in the database.
    :param model: Django model class.
    :returns: A ``ModelNode`` class.
    """
    if for_concrete_model:
        model = model._meta.concrete_model

    if model in __node_cache__:
        return __node_cache__[model]
    else:
        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            __metaclass_model__ = model

            class Meta:
                model = None  # Will pick model from parent class __metaclass_model__ attribute

        __node_cache__[model] = ModelNode
        return ModelNode
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_node_class_adds_to_cache(self):

        klass1 = get_node_class_for_model(Book)

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            class Meta:
                model = Book

        klass2 = get_node_class_for_model(Book)
        self.assertEqual(klass1, klass2)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_model_node(self):
        book = BookFixture(Book).create_one()

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            class Meta:
                model = Book

        self.assertTrue(issubclass(ModelNode, StructuredNode))
        self.assertIsInstance(ModelNode(instance=book), StructuredNode)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_model_with_content_types(self):
        permission = Permission.objects.latest('pk')

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            class Meta:
                model = Permission

        self.assertTrue(issubclass(ModelNode, StructuredNode))
        self.assertIsInstance(ModelNode(instance=permission), StructuredNode)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_model_node_declaring_model_in_class(self):

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):
            __metaclass_model__ = Book

            class Meta:
                model = None

        self.assertEqual(ModelNode.Meta.model, Book)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_model_node_custom_app_label(self):

        @six.add_metaclass(ModelNodeMeta)
        class ModelNode(ModelNodeMixin, StructuredNode):

            class Meta:
                model = Book
                app_label = 'custom_app_label'

        self.assertEqual(ModelNode.Meta.app_label, 'custom_app_label')
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_model_node_fails_without_meta_class(self):
        try:
            @six.add_metaclass(ModelNodeMeta)
            class ModelNode(ModelNodeMixin, StructuredNode):
                pass

            self.fail('Did not fail when defining a ModelNode without a Meta class.')
        except ImproperlyConfigured as e:
            self.assertEqual(str(e), '%s must implement a Meta class.' % 'ModelNode')
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_meta_node(self):

        @six.add_metaclass(MetaNodeMeta)
        class MetaNode(MetaNodeMixin, StructuredNode):

            class Meta:
                model = Book

        self.assertTrue(issubclass(MetaNode, StructuredNode))
        self.assertIsInstance(MetaNode(), StructuredNode)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_meta_node_declaring_model_in_class(self):

        @six.add_metaclass(MetaNodeMeta)
        class MetaNode(MetaNodeMixin, StructuredNode):
            __metaclass_model__ = Book

            class Meta:
                model = None

        self.assertEqual(MetaNode.Meta.model, Book)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_meta_node_custom_app_label(self):

        @six.add_metaclass(MetaNodeMeta)
        class MetaNode(MetaNodeMixin, StructuredNode):

            class Meta:
                model = Book
                app_label = 'custom_app_label'

        self.assertEqual(MetaNode.Meta.app_label, 'custom_app_label')
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_meta_node_custom_permissions(self):

        @six.add_metaclass(MetaNodeMeta)
        class MetaNode(MetaNodeMixin, StructuredNode):
            class Meta:
                model = Publisher

        self.assertEqual(MetaNode.Meta.model, Publisher)
项目:django-chemtrails    作者:inonit    | 项目源码 | 文件源码
def test_create_meta_node_fails_without_meta_class(self):
        try:
            @six.add_metaclass(MetaNodeMeta)
            class MetaNode(MetaNodeMixin, StructuredNode):
                pass

            self.fail('Did not fail when defining a MetaNode without a Meta class.')
        except ImproperlyConfigured as e:
            self.assertEqual(str(e), '%s must implement a Meta class.' % 'MetaNode')