django_auxilium.forms.files module

class django_auxilium.forms.files.TypedFileField(*args, **kwargs)[source]

Bases: django.forms.fields.FileField

Typed FileField which allows to make sure the uploaded file has the appropriate type.

File type can be verified either by extension and/or mimetype.

This field accepts all the parameters as FileField, however in addition it accepts some additional parameters as documented below.

Examples

TypedFileField(ext_whitelist=['jpg', 'jpeg'],
               type_whitelist=['image/jpeg'])

Warning

If use_magic is used, please make sure that python-magic is installed. This library does not require it by default.

Parameters:
  • ext_whitelist (list, optional) – List of allowed file extensions. Note that each extension should emit the first period. For example for filename 'example.jpg', the allowed extension should be 'jpg'.
  • type_whitelist (list, optional) – List of allowed file mimetypes.
  • use_magic (bool, optional) – If type_whitelist is specified, this boolean determines whether to use python-magic (based of top of libmagic) to determine the mimetypes of files instead of relying on Django’s UploadedFile.content_type. Django does not take any real care to determine any accurately the mimetype so it is recommended to leave this parameter as True which is the default value.
default_error_messages = {u'extension': u'File extension is not supported.', u'mimetype': u'File mimetype is not supported.'}
validate(value)[source]

Validate that the file is of supported extension and/or type.