Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature dropbox url #127

Merged
merged 5 commits into from
Mar 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from storages.utils import setting

from dropbox.client import DropboxClient
from dropbox.rest import ErrorResponse

DATE_FORMAT = '%a, %d %b %Y %X +0000'

Expand Down Expand Up @@ -51,8 +52,10 @@ def delete(self, name):
self.client.file_delete(name)

def exists(self, name):
response = self.client.search('/', name, file_limit=1)
return bool(response)
try:
return bool(self.client.metadata(name))
except ErrorResponse:
return False

def listdir(self, path):
directories, files = [], []
Expand All @@ -78,6 +81,10 @@ def accessed_time(self, name):
acc_time = datetime.strptime(metadata['client_mtime'], DATE_FORMAT)
return acc_time

def url(self, name):
media = self.client.media(name)
return media['url']

def _open(self, name, mode='rb'):
remote_file = DropBoxFile(name, self)
return remote_file
Expand Down
14 changes: 12 additions & 2 deletions tests/test_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
'size': '0 bytes',
'thumb_exists': False
}
FILE_MEDIA_FIXTURE = {
'url': 'https://dl.dropboxusercontent.com/1/view/foo',
'expires': 'Fri, 16 Sep 2011 01:01:25 +0000',
}

__all__ = [
'DropBoxTest',
Expand All @@ -67,13 +71,13 @@ def setUp(self, *args):
def test_delete(self, *args):
self.storage.delete('foo')

@mock.patch('dropbox.client.DropboxClient.search',
@mock.patch('dropbox.client.DropboxClient.metadata',
return_value=[FILE_FIXTURE])
def test_exists(self, *args):
exists = self.storage.exists('foo')
self.assertTrue(exists)

@mock.patch('dropbox.client.DropboxClient.search',
@mock.patch('dropbox.client.DropboxClient.metadata',
return_value=[])
def test_not_exists(self, *args):
exists = self.storage.exists('bar')
Expand Down Expand Up @@ -121,6 +125,12 @@ def test_read(self, *args):
content = self.storage._read('foo')
self.assertEqual(content, 'bar')

@mock.patch('dropbox.client.DropboxClient.media',
return_value=FILE_MEDIA_FIXTURE)
def test_url(self, *args):
url = self.storage.url('foo')
self.assertEqual(url, FILE_MEDIA_FIXTURE['url'])


class DropBoxFileTest(TestCase):
@mock.patch('dropbox.client._OAUTH2_ACCESS_TOKEN_PATTERN',
Expand Down