Skip to content

Commit

Permalink
Added test of file fields, improved its documentation and removed south
Browse files Browse the repository at this point in the history
  • Loading branch information
matllubos committed Feb 3, 2017
1 parent beb7e43 commit f81fc97
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Mixin for automatic South migration of custom model fields.
#### `chamber.models.fields.RestrictedFileFieldMixin`

Same as FileField, but you can specify:
* `content_types` - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* `allowed_content_types` - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* `max_upload_size` - a number indicating the maximum file size allowed for upload in MB.
Maximum upload size can be specified in project settings under `MAX_FILE_UPLOAD_SIZE` constant

Expand Down
Binary file modified chamber/locale/cs/LC_MESSAGES/django.mo
Binary file not shown.
2 changes: 1 addition & 1 deletion 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: 2017-02-01 11:55+0100\n"
"POT-Creation-Date: 2017-02-02 20:19+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
37 changes: 0 additions & 37 deletions chamber/migrations/fixtures_south.py

This file was deleted.

15 changes: 3 additions & 12 deletions chamber/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.forms import forms
from django.template.defaultfilters import filesizeformat
from django.utils.encoding import force_text
from django.utils.translation import ugettext
from django.utils.translation import ugettext, ugettext_lazy

from chamber import config
from chamber.forms.fields import DecimalField as DecimalFormField
Expand All @@ -25,16 +25,7 @@
from django.db.models import ImageField as OriginImageField


class SouthMixin(object):

def south_field_triple(self):
from south.modelsinspector import introspector
cls_name = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
args, kwargs = introspector(self)
return (cls_name, args, kwargs)


class DecimalField(SouthMixin, OriginDecimalField):
class DecimalField(OriginDecimalField):

def __init__(self, *args, **kwargs):
self.step = kwargs.pop('step', 'any')
Expand Down Expand Up @@ -91,7 +82,7 @@ def __call__(self, data):
return data


class RestrictedFileFieldMixin(SouthMixin):
class RestrictedFileFieldMixin(object):
"""
Same as FileField, but you can specify:
* allowed_content_types - list of allowed content types. Example: ['application/json', 'image/jpeg']
Expand Down
File renamed without changes.
File renamed without changes.
Binary file added example/data/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/data/test.pdf
Binary file not shown.
Binary file added example/data/test2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions example/dj/apps/test_chamber/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class TestFieldsModel(chamber_models.SmartModel):
default=STATE.NOT_OK)
state_graph = chamber_models.EnumSequencePositiveIntegerField(verbose_name=_('graph'), null=True, blank=True,
enum=GRAPH)
file = chamber_models.FileField(verbose_name=_('file'), null=True, blank=True,
allowed_content_types=('application/pdf', 'text/plain'))
image = chamber_models.FileField(verbose_name=_('image'), null=True, blank=True, max_upload_size=1)


class TestDispatchersModel(chamber_models.SmartModel):
Expand Down
30 changes: 29 additions & 1 deletion example/dj/apps/test_chamber/tests/models/fields.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.core.exceptions import ValidationError
from django.core.files import File
from django.test import TransactionTestCase

from chamber.exceptions import PersistenceException
from chamber.models.fields import generate_random_upload_path
from chamber.shortcuts import change_and_save

from germanium.tools import assert_equal, assert_is_none, assert_raises, assert_true # pylint: disable=E0401
from germanium.tools import assert_equal, assert_is_none, assert_is_not_none, assert_raises, assert_true # pylint: disable=E0401

from test_chamber.models import CSVRecord, TestFieldsModel # pylint: disable=E0401

Expand Down Expand Up @@ -86,3 +87,30 @@ def test_sequence_choices_num_enum(self):
assert_raises(PersistenceException, change_and_save, self.inst, state_graph=TestFieldsModel.GRAPH.FIRST)
change_and_save(self.inst, state_graph=TestFieldsModel.GRAPH.THIRD)
assert_equal(self.inst.state_graph, TestFieldsModel.GRAPH.THIRD)

def test_file_field_content_type(self):
# These files can be saved because it has supported type
for filename in ('all_fields_filled.csv', 'test.pdf'):
with open('data/{}'.format(filename), 'rb') as f:
assert_is_none(self.inst.file)
self.inst.file.save(filename, File(f))
assert_is_not_none(self.inst.file)
change_and_save(self.inst, file=None)

# Image file is not supported
with open('data/test2.jpg', 'rb') as f:
assert_is_none(self.inst.file)
assert_raises(PersistenceException, self.inst.file.save, 'image.jpeg', File(f))

def test_image_field_max_upload_size(self):
# File is can be stored
with open('data/test2.jpg', 'rb') as f:
assert_is_none(self.inst.image)
self.inst.image.save('test2.jpg', File(f))
assert_is_not_none(self.inst.image)
change_and_save(self.inst, image=None)

# File is too large to store
with open('data/test.jpg', 'rb') as f:
assert_is_none(self.inst.image)
assert_raises(PersistenceException, self.inst.image.save, 'image.jpeg', File(f))
1 change: 1 addition & 0 deletions example/requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage==4.0.2
pyprind==2.9.9
coveralls==1.1
filemagic==1.6
unidecode==0.4.20

0 comments on commit f81fc97

Please sign in to comment.