Skip to content

Commit

Permalink
Merge 9089892 into 4df350a
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Sep 11, 2017
2 parents 4df350a + 9089892 commit 9f39f19
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
7 changes: 4 additions & 3 deletions s3file/apps.py
Expand Up @@ -11,7 +11,8 @@ def ready(self):

if isinstance(default_storage, S3Boto3Storage):
from django import forms
from .forms import S3FileInput
from .forms import S3FileInputMixin

forms.ClearableFileInput.__new__ = \
lambda cls, *args, **kwargs: object.__new__(S3FileInput)
if S3FileInputMixin not in forms.ClearableFileInput.__bases__:
forms.ClearableFileInput.__bases__ = \
(S3FileInputMixin,) + forms.ClearableFileInput.__bases__
5 changes: 2 additions & 3 deletions s3file/forms.py
Expand Up @@ -10,7 +10,7 @@
logger = logging.getLogger('s3file')


class S3FileInput(ClearableFileInput):
class S3FileInputMixin:
"""FileInput that uses JavaScript to directly upload to Amazon S3."""

needs_multipart_form = False
Expand All @@ -26,7 +26,7 @@ def client(self):
return default_storage.connection.meta.client

def build_attrs(self, *args, **kwargs):
attrs = super(S3FileInput, self).build_attrs(*args, **kwargs)
attrs = super().build_attrs(*args, **kwargs)

mime_type = attrs.get('accept', None)
response = self.client.generate_presigned_post(
Expand Down Expand Up @@ -75,5 +75,4 @@ def upload_folder(self):
class Media:
js = (
's3file/js/s3file.js',

)
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@

setup(
name='django-s3file',
version='3.0.0',
version='3.0.1',
description='A lightweight file uploader input for Django and Amazon S3',
author='codingjoe',
url='https://github.com/codingjoe/django-s3file',
Expand Down
6 changes: 3 additions & 3 deletions tests/test_apps.py
Expand Up @@ -3,14 +3,14 @@
from django import forms

from s3file.apps import S3FileConfig
from s3file.forms import S3FileInput
from s3file.forms import S3FileInputMixin


class TestS3FileConfig:
def test_ready(self, settings):
app = S3FileConfig('s3file', importlib.import_module('tests.testapp'))
app.ready()
assert not isinstance(forms.ClearableFileInput(), S3FileInput)
assert not isinstance(forms.ClearableFileInput(), S3FileInputMixin)
settings.DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
app.ready()
assert isinstance(forms.ClearableFileInput(), S3FileInput)
assert isinstance(forms.ClearableFileInput(), S3FileInputMixin)
24 changes: 13 additions & 11 deletions tests/test_forms.py
Expand Up @@ -7,7 +7,7 @@
from selenium.webdriver.support.expected_conditions import staleness_of
from selenium.webdriver.support.wait import WebDriverWait

from s3file.forms import S3FileInput
from s3file.forms import S3FileInputMixin
from tests.testapp.forms import UploadForm

try:
Expand All @@ -33,13 +33,15 @@ def url(self):

@pytest.fixture(autouse=True)
def patch(self):
ClearableFileInput.__new__ = \
lambda cls, *args, **kwargs: object.__new__(S3FileInput)
if S3FileInputMixin not in ClearableFileInput.__bases__:
ClearableFileInput.__bases__ = \
(S3FileInputMixin,) + ClearableFileInput.__bases__
pass

@pytest.fixture
def freeze(self, monkeypatch):
"""Freeze datetime and UUID."""
monkeypatch.setattr('s3file.forms.S3FileInput.upload_folder', 'tmp')
monkeypatch.setattr('s3file.forms.S3FileInputMixin.upload_folder', 'tmp')

def test_value_from_datadict(self, client, upload_file):
with open(upload_file) as f:
Expand Down Expand Up @@ -78,7 +80,7 @@ def test_clear(self, filemodel):
assert not form.cleaned_data['file']

def test_build_attr(self):
assert set(S3FileInput().build_attrs({}).keys()) == {
assert set(ClearableFileInput().build_attrs({}).keys()) == {
'class',
'data-url',
'data-fields-x-amz-algorithm',
Expand All @@ -88,11 +90,11 @@ def test_build_attr(self):
'data-fields-policy',
'data-fields-key',
}
assert S3FileInput().build_attrs({})['class'] == 's3file'
assert S3FileInput().build_attrs({'class': 'my-class'})['class'] == 'my-class s3file'
assert ClearableFileInput().build_attrs({})['class'] == 's3file'
assert ClearableFileInput().build_attrs({'class': 'my-class'})['class'] == 'my-class s3file'

def test_get_conditions(self, freeze):
conditions = S3FileInput().get_conditions(None)
conditions = ClearableFileInput().get_conditions(None)
assert all(condition in conditions for condition in [
{"bucket": 'test-bucket'},
{"success_action_status": "201"},
Expand All @@ -101,15 +103,15 @@ def test_get_conditions(self, freeze):
]), conditions

def test_accept(self):
widget = S3FileInput()
widget = ClearableFileInput()
assert 'accept' not in widget.render(name='file', value='test.jpg')
assert ["starts-with", "$Content-Type", ""] in widget.get_conditions(None)

widget = S3FileInput(attrs={'accept': 'image/*'})
widget = ClearableFileInput(attrs={'accept': 'image/*'})
assert 'accept="image/*"' in widget.render(name='file', value='test.jpg')
assert ["starts-with", "$Content-Type", "image/"] in widget.get_conditions('image/*')

widget = S3FileInput(attrs={'accept': 'image/jpeg'})
widget = ClearableFileInput(attrs={'accept': 'image/jpeg'})
assert 'accept="image/jpeg"' in widget.render(name='file', value='test.jpg')
assert {"Content-Type": 'image/jpeg'} in widget.get_conditions('image/jpeg')

Expand Down

0 comments on commit 9f39f19

Please sign in to comment.