Skip to content

Commit

Permalink
Merge pull request #127 from zuck/feature-dropbox-url
Browse files Browse the repository at this point in the history
Feature dropbox url
  • Loading branch information
jschneier committed Mar 20, 2016
2 parents dc9dea4 + edb4979 commit 575d520
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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

0 comments on commit 575d520

Please sign in to comment.