Skip to content

Commit dd0dde0

Browse files
committed
Merge pull request #84 from django-mongodb-engine/gridfs-url
Added url() support to GridFSStorage.
2 parents b3cfe5e + 1cab7b8 commit dd0dde0

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

django_mongodb_engine/storage.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
2+
import urlparse
23

34
from gridfs import GridFS, NoFile
45

56
from django.core.exceptions import ImproperlyConfigured
67
from django.core.files.storage import Storage
8+
from django.utils.encoding import filepath_to_uri
9+
710

811
def _get_subcollections(collection):
912
"""
@@ -15,6 +18,7 @@ def _get_subcollections(collection):
1518
if cleaned != collection.name and cleaned.startswith(collection.name):
1619
yield cleaned
1720

21+
1822
class GridFSStorage(Storage):
1923
"""
2024
GridFS Storage backend for Django.
@@ -39,15 +43,24 @@ class GridFSStorage(Storage):
3943
:param database:
4044
Alias of the Django database to use. Defaults to 'default' (the default
4145
Django database).
46+
:param base_url:
47+
URL that serves the files in GridFS (for instance, through nginx-gridfs).
48+
Defaults to None (file not accessible through a URL).
4249
"""
4350

44-
def __init__(self, location='', collection='storage', database='default'):
51+
def __init__(self, location='', collection='storage', database='default',
52+
base_url=None):
4553
self.location = location.strip(os.sep)
4654
self.collection = collection
4755
self.database = database
56+
self.base_url = base_url
57+
4858
if not self.collection:
4959
raise ImproperlyConfigured("'collection' may not be empty")
5060

61+
if self.base_url and not self.base_url.endswith('/'):
62+
raise ImproperlyConfigured("If set, 'base_url' must end with a slash")
63+
5164
def _open(self, path, mode='rb'):
5265
"""
5366
Returns a :class:`~gridfs.GridOut` file opened in `mode`, or raises
@@ -104,6 +117,11 @@ def size(self, path):
104117
gridfs, filename = self._get_gridfs(path)
105118
return gridfs.get_last_version(filename=filename).length
106119

120+
def url(self, name):
121+
if self.base_url is None:
122+
raise ValueError("This file is not accessible via a URL.")
123+
return urlparse.urljoin(self.base_url, filepath_to_uri(name))
124+
107125
def created_time(self, path):
108126
"""
109127
Returns the datetime the file at `path` was created.

tests/storage/tests.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ def setUp(self):
1515
self.storage = self.get_storage(self.temp_dir)
1616

1717
def tearDown(self):
18-
for collection in self.storage._db.collection_names():
19-
if not collection.startswith('system.'):
20-
self.storage._db.drop_collection(collection)
18+
if hasattr(self.storage, '_db'):
19+
for collection in self.storage._db.collection_names():
20+
if not collection.startswith('system.'):
21+
self.storage._db.drop_collection(collection)
2122

22-
def get_storage(self, location):
23-
return self.storage_class(location=location)
23+
def get_storage(self, location, **kwargs):
24+
return self.storage_class(location=location, **kwargs)
2425

2526
def test_file_access_options(self):
2627
"""
@@ -113,15 +114,15 @@ def test_file_save_without_name(self):
113114
#
114115
# self.storage.delete(f_name)
115116

116-
# def test_file_url(self):
117-
# """
118-
# File storage returns a url to access a given file from the Web.
119-
# """
120-
# self.assertEqual(self.storage.url('test.file'),
121-
# '%s%s' % (self.storage.base_url, 'test.file'))
122-
#
123-
# self.storage.base_url = None
124-
# self.assertRaises(ValueError, self.storage.url, 'test.file')
117+
def test_file_url(self):
118+
"""
119+
File storage returns a url to access a given file from the Web.
120+
"""
121+
self.assertRaises(ValueError, self.storage.url, 'test.file')
122+
123+
self.storage = self.get_storage(self.storage.location, base_url='foo/')
124+
self.assertEqual(self.storage.url('test.file'),
125+
'%s%s' % (self.storage.base_url, 'test.file'))
125126

126127
def test_file_with_mixin(self):
127128
"""

0 commit comments

Comments
 (0)