Skip to main content

A simple Django file type validator using python-magic

Project description

https://circleci.com/gh/mckinseyacademy/django-upload-validator/tree/master.svg?style=svg

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-upload-validator, version 1.1.5
Filename, size File type Python version Upload date Hashes
Filename, size django_upload_validator-1.1.5-py3-none-any.whl (5.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-upload-validator-1.1.5.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page