waldur / django-upload-validator

A simple Django file type validator using python-magic

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django Upload Validator

image

Django Upload Validator is a simple utility for validating file types and extensions using python-magic library.

Installation

Install the current PyPI release:

pip install django-upload-validator

Usage

General usage :

from upload_validator import FileTypeValidator

validator = FileTypeValidator(
    allowed_types=['application/msword'],
    allowed_extensions=['.doc', '.docx']
)

file_resource = open('sample.doc')

# ValidationError will be raised in case of invalid type or extension
validator(file_resource)

Usage as a FileField validator in Django forms :

from upload_validator import FileTypeValidator

 profile_image = forms.FileField(
    label='', help_text="Formats accepted: JPEG nd PNG", required=False,
    validators=[FileTypeValidator(
        allowed_types=[ 'image/jpeg','image/png']
    )]
)

Wildcard character specification is also supported. e.g; for accepting only images: :

profile_image = forms.FileField(
    label='', help_text="Only image formats are accepted.", required=False,
    validators=[FileTypeValidator(
        allowed_types=[ 'image/*']
    )]
)

Running Tests

  1. Install testing requirements pip install -r tests/requirements.txt
  2. Run python runtests.py inside the root directory of package

About

A simple Django file type validator using python-magic


Languages

Language:Python 100.0%