Skip to main content

This Django app adds a new field type, ConstrainedFileField, that has the capability of checking the document size and type.

Project description

Python Django License PyPI Build Status Coverage Status

ConstrainedFileField for Django

This Django app adds a new field type, ConstrainedFileField, that has the capability of checking the file size and type. Also provides a javascript checker for the form field.

Features

  • File size limitation

  • File type limitation

  • Javascript file size checker

Requirements

  • Python>=2.7

  • Django>=1.8.17

  • python-magic >= 0.4.2 iff you want to check the file type

Installation

Using PyPI

  1. Run pip install django-constrainedfilefield

Using the source code

  1. Run ./pypi_packager.sh

  2. Run pip install dist/django_constrainedfilefield-x.y.z-none-any.wheel, where x.y.z must be replaced by the actual version number

Usage

Validate single file

The field can be used in forms or model forms like a normal FileField. If a user tries to upload a file which is too large or without a valid type, a form validation error will occur.

Creating form from model

Create a model and add a field of type ConstrainedFileField. You can add a maximum size in bytes and a list of valid mime types that will be allowed. The list of all mime types is available here: http://www.iana.org/assignments/media-types/index.html. Setting none of the above, it behaves like a regular FileField.

from django.db import models
from constrainedfilefield.fields import ConstrainedFileField

class TestModel(models.Model):
    the_file = ConstrainedFileField(
                            null=True,
                            blank=True,
                            upload_to='testfile',
                            content_types=['image/png'],
                            max_upload_size=10240
                                    )
from django import forms
from myproject.models import TestModel

class TestModelForm(forms.ModelForm):
    class Meta:
        model = TestModel
        fields = ['the_file']

Building a form

from django import forms
from constrainedfilefield.fields import ConstrainedFileField

class TestNoModelForm(forms.Form):
    the_file = ConstrainedFileField(
                            null=True,
                            blank=True,
                            upload_to='testfile',
                            content_types=['image/png'],
                            max_upload_size=10240
                                    ).formfield()

Javascript file size validation

Additionally, to prevent user uploading too large files, a javascript checker can be set to the form field. In order to achieve that, you need to

  1. Add constrainedfilefield to the INSTALLED_APPS. This will load the javascripts from the static files.

  • Activate this feature by setting js_checker=True when instantiating the ConstrainedFileField.

  • Include the javascript in the template where the form field is used

    {% load static %}
    <script src="{% static 'constrainedfilefield/js/file_checker.js' %}"></script>

Note on DOS attacks

Important note: the check of the file size is made by Django once the whole file has been uploaded to the server and stored in a temp directory (or in memory if the file is small). Thus, this is useful to guarantee the quota of the users, for example, but will not stop an attacking user that wants to block the server by sending huge files (e. g. of several Gb).

To avoid this, you need to configure your front end to limit the size of uploaded files. How to do it depends on the software you are using. For example, if you use apache, you should use **LimitRequestBody** directive.

This is a complementary measure, because you’ll usually want normal users that exceed the size by a reasonable amount to get a friendly form validation message, while attacking users will see how their connection is abruptly cut before the file finishes uploading. So the recommended setting is to give max_upload_size a small value (e.g. 5Mb) and LimitRequestBody a higher one (e.g. 100Mb).

Credits

This is a fork of django-validated-file from Kaleidos.

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-constrainedfilefield-3.0.7.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_constrainedfilefield-3.0.7-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file django-constrainedfilefield-3.0.7.tar.gz.

File metadata

File hashes

Hashes for django-constrainedfilefield-3.0.7.tar.gz
Algorithm Hash digest
SHA256 00cc51febabdb2bc470972f458391152e2da3483a6277825aa11eaac68a7b3bb
MD5 67b8160a3a28b76440e3898e470f7312
BLAKE2b-256 64e03a07e01c6465c8e40e88714423817c3cc459a62fbed6948546ae4c17aacc

See more details on using hashes here.

File details

Details for the file django_constrainedfilefield-3.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for django_constrainedfilefield-3.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d78ac0fe770cc5494648a3aa3b30ed7092ae3dd2ef57a41533734d3595bbaab5
MD5 b2729a2c9bc3da9bf05fc4174800166d
BLAKE2b-256 ebd78bcc9ce8890acbede046281253c2955e05b7607cd94236157d111d68da14

See more details on using hashes here.

Supported by

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