Skip to content

Commit

Permalink
Merge pull request #38 from druids/FileFieldCanVerifiContentType
Browse files Browse the repository at this point in the history
Added content type verification to file field
  • Loading branch information
matllubos committed Feb 1, 2017
2 parents 3b63795 + 52c746d commit 5167618
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
20 changes: 16 additions & 4 deletions chamber/locale/cs/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-07-28 21:15+0200\n"
"POT-Creation-Date: 2017-02-01 11:55+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand All @@ -18,16 +18,28 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=3; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2;\n"

#: models/__init__.py:134
#: models/__init__.py:148
msgid "created at"
msgstr "vytvořeno"

#: models/__init__.py:136
#: models/__init__.py:150
msgid "changed at"
msgstr "upraveno"

#: models/fields.py:63
#: models/fields.py:69
#, python-brace-format
msgid "Please keep filesize under {max}. Current filesize {current}"
msgstr ""
"Udržujte velikost souboru pod {max}. Současná velikost souboru je {current}"

#: models/fields.py:89
msgid "Unsupported file type"
msgstr "Nepodporovaný typ souboru"

#: models/fields.py:171
msgid "Value must be empty"
msgstr "Hodnota musí být prázdná"

#: models/fields.py:176 models/fields.py:206
msgid "Allowed choices are {}."
msgstr "Povolené možnosti jsou {}."
23 changes: 21 additions & 2 deletions chamber/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import magic # pylint: disable=E0401
import os
from uuid import uuid4 as uuid

Expand Down Expand Up @@ -74,17 +75,35 @@ def __call__(self, data):
return data


class AllowedContentTypesFileValidator(object):

def __init__(self, content_types):
self.content_types = content_types

def __call__(self, data):
with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m:
mime_type = m.id_buffer(data.file.read(1024))
data.file.seek(0)
if mime_type not in self.content_types:
raise ValidationError(
ugettext('Unsupported file type')
)
return data


class RestrictedFileFieldMixin(SouthMixin):
"""
Same as FileField, but you can specify:
* content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* allowed_content_types - list of allowed content types. Example: ['application/json', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload in MB.
"""
def __init__(self, *args, **kwargs):
max_upload_size = kwargs.pop('max_upload_size', config.CHAMBER_MAX_FILE_UPLOAD_SIZE) * 1024 * 1024

allowed_content_types = kwargs.pop('allowed_content_types', None)
super(RestrictedFileFieldMixin, self).__init__(*args, **kwargs)
self.validators.append(RestrictedFileValidator(max_upload_size))
if allowed_content_types:
self.validators.append(AllowedContentTypesFileValidator(allowed_content_types))

def get_filename(self, filename):
"""
Expand Down
1 change: 1 addition & 0 deletions example/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ six==1.10.0
coverage==4.0.2
pyprind==2.9.9
coveralls==1.1
filemagic==1.6
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
'Unidecode>=0.04.17',
'pyprind==2.9.9',
'six>=1.10.0',
'filemagic>=1.6',
],
)

0 comments on commit 5167618

Please sign in to comment.