Skip to main content

图像验证(对于 Django 管理员)轻而易举。

项目描述

图片django-vimage

最新 PyPI 版本徽章 Travis CI 构建状态徽章 Codecov 状态徽章 ReadTheDocs 文档状态徽章 支持的 python 版本徽章 支持的 Django 版本徽章 执照徽章

Django Admin的 Django 图像验证轻而易举。验证:尺寸、尺寸、格式和纵横比。

因为,我喜欢寻找单词/band/place/something 的来源,这个包名来自单词validate和(你猜对了) image。因此,django-vimage. 仅此而已,仅此而已:)

这个包是由于缺少类似的 Django 包来进行图像验证而创建的。我搜索了这个,但什么也没找到。因此,我决定创建一个可重用的 Django 包,该包将以简单的方式进行图像验证。只需在一个简单的 Python 字典中声明一些ImageFields 和适用于它们的规则。首先,我将蓝图写在一张纸上,然后我逐渐将其移植到 Django/Python 代码中。

文档

完整的文档位于https://django-vimage.readthedocs.io

快速开始

安装 django-vimage :

pip install django-vimage

将其添加到您的INSTALLED_APPS

INSTALLED_APPS = (
    ...
    'vimage.apps.VimageConfig',
    ...
)

最后,VIMAGE在文件中的某处添加 dict 配置settings

VIMAGE = {
    'my_app.models': {
        'DIMENSIONS': (200, 200),
        'SIZE': {'lt': 100},
    }
}

上述VIMAGE设置为 应用程序ImageField下的所有 Django 字段设置了规则my_app。更具体地说,所有 ImageFields 应该是 200 x 200px并且小于 100KB。任何违反上述任何规则的图像,都会在 Django 管理页面中显示(相应地翻译)一个漂亮的错误消息。

可能的键:值对的完整示例如下所示。请注意,以下代码块不适合复制粘贴到您的settings 文件中,因为它包含重复的 dict 键。这只是为了演示。

VIMAGE = {
    # Possible keys are:
    # 'app.models'  # to all ImageFields inside this app
    # 'app.models.MyModel'  # to all ImageFields inside MyModel
    # 'app.models.MyModel.field'  # only to this ImageField

    # Example of applying validation rules to all images across
    # all models of myapp app
    'myapp.models': {
        # rules
    },

    # Example of applying validation rules to all images across
    # a specific model
    'myapp.models.MyModel': {
        # rules
    },

    # Example of applying validation rules to a
    # specific ImageField field
    'myapp.models.MyModel.img': {
        # rules
    },

    # RULES
    'myapp.models': {

        # By size (measured in KB)

        # Should equal to 100KB
        'SIZE': 100,  # defaults to eq (==)

        # (100KB <= image_size <= 200KB) AND not equal to 150KB
        'SIZE': {
            'gte': 100,
            'lte': 200,
            'ne': 150,
        },

        # Custom error message
        'SIZE': {
            'gte': 100,
            'lte': 200,
            'err': 'Your own error message instead of the default.'
                   'Supports <strong>html</strong> tags too!',
        },


        # By dimensions (measured in px)
        # Should equal to 1200x700px (width x height)
        'DIMENSIONS': (1200, 700),  # defaults to eq (==)

        # Should equal to one of these sizes 1000x300px or 1500x350px
        'DIMENSIONS': [(1000, 300), (1500, 350)],

        # Should be 1000x300 <= image_dimensions <= 2000x500px
        'DIMENSIONS': {
            'gte': (1000, 300),
            'lte': (2000, 500),
        },

        # width must be >= 30px and less than 60px
        # height must be less than 90px and not equal to 40px
        'DIMENSIONS': {
            'w': {
                'gt': 30,
                'lt': 60,
            },
            'h': {
                'lt': 90,
                'ne': 40,
            }
        },


        # By format (jpeg, png, tiff etc)
        # Uploaded image should be JPEG
        'FORMAT': 'jpeg',

        # Uploaded image should be one of the following
        'FORMAT': ['jpeg', 'png', 'gif'],

        # Uploaded image should not be a GIF
        'FORMAT': {
            'ne': 'gif',
        },

        # Uploaded image should be neither a GIF nor a PNG
        'FORMAT': {
            'ne': ['gif', 'png'],
            'err': 'Wrong image <em>format</em>!'
        },
    }
}

特征

  • 可以根据其大小 (KB)、尺寸 (px)、格式(jpeg、png 等)和纵横比(宽/高比)来验证图像。

  • 格式正确的错误消息。它们具有以下形式:

    [IMAGE RULE_NAME]验证错误:image_value不符合验证规则:rule

  • 人性化的错误信息。所有规则和图像值都人性化

    • 'SIZE': {'gte': 100}greater than or equal to 100KB渲染时变为

    • 'DIMENSIONS': {'ne': (100, 100)}not equal to 100 x 100px渲染时变为

  • 可覆盖的错误消息。err可以通过在验证规则中定义一个键来覆盖默认错误消息:

    'SIZE': {'gte': 100, 'err': 'Custom error'}Custom error渲染时变为

  • HTML 安全(自定义)错误消息。所有错误消息(默认或您自己的)都通过函数mark_safe传递。

  • 级联验证规则。可以为应用程序的某些字段定义通用规则ImageField,然后为特定ImageField字段定义另一组规则。通用规则将覆盖通用规则,并且任何新规则都将添加到特定 ImageField字段:

    myapp.models: {
        'SIZE': {
            'lt': 120,
        },
        'FORMAT': 'jpeg',
        'DIMENSIONS': {
            'lt': (500, 600),
        }
     },
     myapp.models.MyModel.img: {
        'DIMENSIONS': (1000, 500),
     },
    

    在上面的例子中(顺序无关紧要),所有ImageField的 s 应该是less than 120KB, JPEGimages less than 500 x 600px. 但是,myapp.models.MyModel.img字段应该是less than 120KB, JPEGimage equal to 1000 x 500px.

运行测试

代码真的有效吗?

source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox

未来的补充

  • 基于图像的模式验证图像模式(上传的图像是否处于索引模式、灰度模式等)。这很容易实现,但却是一种罕见的验证要求。因此,如果用户想要验证图像的模式(这在网络上很少见),它将被实现。
  • 如果您认为可以应用于图像的任何其他验证(除了 svg)并且它不包含在此包中,请随时提交问题或 PR。

学分

用于渲染此包的工具:

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

django-vimage-1.2.0.tar.gz (22.9 kB 查看哈希)

已上传 source

内置分布

django_vimage-1.2.0-py3-none-any.whl (23.5 kB 查看哈希

已上传 py3