validators.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:a4-opin 作者: liqd 项目源码 文件源码
def validate_file_type_and_size(upload):

    file_max_mb = 5
    max_size = file_max_mb*10**6
    fileformats = settings.FILE_ALIASES['*']['fileformats']
    mimetypes = [mimetype for name, mimetype in fileformats]
    names = [name for name, mimetype in fileformats]

    errors = []

    filetype = magic.from_buffer(upload.read(), mime=True)
    if filetype.lower() not in mimetypes:
        msg = _(
            'Unsupported file format. Supported formats are {}.'.format(
                ', '.join(names)
            )
        )
        errors.append(ValidationError(msg))
    if upload.size > max_size:
        msg = _('File should be at most {} MB'.format(file_max_mb))
        errors.append(ValidationError(msg))
    if errors:
        raise ValidationError(errors)
    return upload
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号