Skip to content

Commit

Permalink
Merge pull request #114 from uploadcare/autostore
Browse files Browse the repository at this point in the history
Add autostore support
  • Loading branch information
dmitry-mukhin committed Sep 29, 2016
2 parents 1c821a2 + 640d171 commit f23fde4
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 131 deletions.
46 changes: 22 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
# https://www.djangoproject.com/download/#supported-versions

sudo: false
language: python
python:
- 2.6
- 2.7
- 3.2
- 3.3
- 3.4
- 3.5
env:
global:
- SUITE="tests/functional"
matrix:
- DJANGO=1.4
- DJANGO=1.5
- DJANGO=1.6
- DJANGO=1.7
- DJANGO=1.8
- DJANGO=1.9
- DJANGO=1.10
matrix:
exclude:
- python: 2.6
env: DJANGO=1.7
- python: 2.6
env: DJANGO=1.8
include:
- python: 2.6
env: DJANGO=1.9
- python: 3.2
env: DJANGO=1.4
- python: 3.2
env: DJANGO=1.9
- python: 3.3
env: DJANGO=1.6

- python: 2.7
env: DJANGO=1.4
- python: 2.7
env: DJANGO=1.5
- python: 2.7
env: DJANGO=1.6
- python: 2.7
env: DJANGO=1.7

- python: 3.3
env: DJANGO=1.9
- python: 3.4
env: DJANGO=1.4
include:
env: DJANGO=1.8

# integration tests
- python: 2.7
env: DJANGO=1.8 SUITE="tests/integration"
env: DJANGO=1.10 SUITE="tests/integration"
- python: 3.5
env: DJANGO=1.10 SUITE="tests/integration"

cache:
directories:
- $HOME/.cache/pip
Expand All @@ -51,6 +50,5 @@ notifications:
recipients:
- ak@uploadcare.com
- dm@uploadcare.com
- vs@uploadcare.com
on_success: change
on_failure: change
9 changes: 8 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
History
-------

2.1.0
~~~~~

- Support auto storing in upload requests
- Updated widget to version 2.10.0 (see `widget changelog`_).
- Drop support for Python 3.2

2.0.1
~~~
~~~~~

- Fixed issue with missing ``ucare_cli`` package.

Expand Down
2 changes: 1 addition & 1 deletion pyuploadcare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from __future__ import unicode_literals

__version__ = '2.0.1'
__version__ = '2.1.0'

from .api_resources import File, FileList, FileGroup
from .exceptions import (
Expand Down
94 changes: 87 additions & 7 deletions pyuploadcare/api_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,73 @@ def construct_from(cls, file_info):
return file_

@classmethod
def upload(cls, file_obj):
"""Uploads a file and returns ``File`` instance."""
files = uploading_request('POST', 'base/', files={'file': file_obj})
def upload(cls, file_obj, store=None):
"""Uploads a file and returns ``File`` instance.
Args:
file_obj: file object to upload to
store (Optional[bool]): Should the file be automatically stored
upon upload. Defaults to None.
- False - do not store file
- True - store file (can result in error if autostore
is disabled for project)
- None - use project settings
Returns:
``File`` instance
"""
if store is None:
store = 'auto'
elif store:
store = '1'
else:
store = '0'

data = {
'UPLOADCARE_STORE': store,
}

files = uploading_request('POST', 'base/', data=data,
files={'file': file_obj})
file_ = cls(files['file'])
return file_

@classmethod
def upload_from_url(cls, url):
def upload_from_url(cls, url, store=None, filename=None):
"""Uploads file from given url and returns ``FileFromUrl`` instance.
Args:
url (str): URL of file to upload to
store (Optional[bool]): Should the file be automatically stored
upon upload. Defaults to None.
- False - do not store file
- True - store file (can result in error if autostore
is disabled for project)
- None - use project settings
filename (Optional[str]): Name of the uploaded file. If this not
specified the filename will be obtained from response headers
or source URL. Defaults to None.
Returns:
``FileFromUrl`` instance
"""

if store is None:
store = 'auto'
elif store:
store = '1'
else:
store = '0'

data = {
'source_url': url,
'store': store,
}
if filename:
data['filename'] = filename

result = uploading_request('POST', 'from_url/',
data={'source_url': url})
data=data)
if 'token' not in result:
raise APIError(
'could not find token in result: {0}'.format(result)
Expand All @@ -298,10 +353,35 @@ def upload_from_url(cls, url):

@classmethod
def upload_from_url_sync(cls, url, timeout=30, interval=0.3,
until_ready=False):
until_ready=False, store=None, filename=None):
"""Uploads file from given url and returns ``File`` instance.
Args:
url (str): URL of file to upload to
store (Optional[bool]): Should the file be automatically stored
upon upload. Defaults to None.
- False - do not store file
- True - store file (can result in error if autostore
is disabled for project)
- None - use project settings
filename (Optional[str]): Name of the uploaded file. If this not
specified the filename will be obtained from response headers
or source URL. Defaults to None.
timeout (Optional[int]): seconds to wait for successful upload.
Defaults to 30.
interval (Optional[float]): interval between upload status checks.
Defaults to 0.3.
until_ready (Optional[bool]): should we wait until file is
available via CDN. Defaults to False.
Returns:
``File`` instance
Raises:
``TimeoutError`` if file wasn't uploaded in time
"""
ffu = cls.upload_from_url(url)
ffu = cls.upload_from_url(url, store, filename)
return ffu.wait(timeout=timeout, interval=interval,
until_ready=until_ready)

Expand Down
2 changes: 1 addition & 1 deletion pyuploadcare/dj/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
conf.cdn_base = settings.UPLOADCARE['cdn_base']


widget_version = settings.UPLOADCARE.get('widget_version', '2.8.1')
widget_version = settings.UPLOADCARE.get('widget_version', '2.10.0')
widget_build = settings.UPLOADCARE.get(
'widget_build',
settings.UPLOADCARE.get('widget_variant', 'full.min'))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

setup(
name='pyuploadcare',
version='2.0.1',
version='2.1.0',
description='Python library for Uploadcare.com',
long_description=(long_description),
author='Uploadcare LLC',
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Meta:
emp = Employee(cv='3addab78-6368-4c55-ac08-22412b6a2a4c')

with self.assertRaises(ValidationError):
emp._meta.get_field_by_name('cv')[0].clean(emp.cv, emp)
emp._meta.get_field('cv').clean(emp.cv, emp)


class FileGroupFieldTest(unittest.TestCase):
Expand Down

0 comments on commit f23fde4

Please sign in to comment.