Skip to main content

A simple Django file type validator using python-magic

Project description

https://circleci.com/gh/naeem91/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.

Source Distribution

django-upload-validator-1.1.6.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

django_upload_validator-1.1.6-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file django-upload-validator-1.1.6.tar.gz.

File metadata

  • Download URL: django-upload-validator-1.1.6.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.1

File hashes

Hashes for django-upload-validator-1.1.6.tar.gz
Algorithm Hash digest
SHA256 dbf93740fb42f8c9310b936838bbf99f873339c2f860de1c7c0d7a27464f7c8a
MD5 80af737ece530ebf8c382cbca66f778c
BLAKE2b-256 ed99f4476cb7350ea6fef1f037a6346be635754ff4e870b323a562a5151fe2a9

See more details on using hashes here.

File details

Details for the file django_upload_validator-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: django_upload_validator-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.1

File hashes

Hashes for django_upload_validator-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b4c1a2d2138b319288a20069c5dbdbbdb1257a447ec2f0b4e6cf9e8d6fd3d1bc
MD5 4941f24767bec35234233fbab91ca223
BLAKE2b-256 1120d6f75e016f06a5c73bd2fe2ea7a3714093538a57131ea15f55c0fb65b407

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page