Merge branch 'master' of github.com:tyrelsouza/django-dbfilestorage

This commit is contained in:
Tyrel Souza 2017-08-11 11:34:26 -04:00
commit 60c3f77a58
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
4 changed files with 54 additions and 6 deletions

View File

@ -93,3 +93,12 @@ class DBFileStorage(Storage):
def modified_time(self, name): def modified_time(self, name):
dbf = _get_object(name) dbf = _get_object(name)
return dbf.mtime return dbf.mtime
def listdir(self, path):
dirs = [] # this doesn't support dirs, so just empty list
files = sorted(DBFile.objects.filter(
name__startswith=path
).values_list(
"name", flat=True
))
return (dirs, files)

View File

@ -46,7 +46,7 @@ master_doc = 'index'
# General information about the project. # General information about the project.
project = u'Django DBFileStorage' project = u'Django DBFileStorage'
copyright = u'2016, Tyrel Souza' copyright = u'2017, Tyrel Souza'
author = u'Tyrel Souza' author = u'Tyrel Souza'
# The version info for the project you're documenting, acts as replacement for # The version info for the project you're documenting, acts as replacement for
@ -54,7 +54,7 @@ author = u'Tyrel Souza'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = u'0.9.0' version = u'0.9.1'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = version release = version

View File

@ -58,8 +58,28 @@ It gets the file object from the database referenced by the md5 (the
filefield.url() will provide this) automatically. It then returns a filefield.url() will provide this) automatically. It then returns a
HttpResponse of the decoded file and content type. Very straightforward. HttpResponse of the decoded file and content type. Very straightforward.
Listing Directories
~~~~~~~~~~~~~~~~~~~
Because there is no such thing as a "directory" in DBFileStorage, a query is made
for all names that start with the passed in string.
Improvements could be made to pass in anything else that has a slash into the first
tuple and everything that doesn't in the second, but for this version I have chosen
just to place everything into the first tuple.
.. code:: python
DBFileStorage.listdir("path/here")
# Returns
([], ["path/here/files.txt", "path/here/that/match.txt",])
Other operations Other operations
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
Everything else, such as `.path()`, `.delete()`, `.size()`, `.exists()` are Everything else, such as `.path()`, `.delete()`, `.size()`, `.exists()` are
exactly the same, in usage as normal. exactly the same, in usage as normal.

View File

@ -21,11 +21,14 @@ class DBFileTest(TestCase):
self.filename = "kris.jpg" self.filename = "kris.jpg"
self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename) self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename)
self._upload() self._upload(self.filepath)
def _upload(self, name=None):
if name is None:
name = self.filepath
def _upload(self):
with open(self.filepath, 'rb') as f: with open(self.filepath, 'rb') as f:
return default_storage.save(self.filepath, f) return default_storage.save(name, f)
def test_upload(self): def test_upload(self):
""" Test that the file storage uploads and puts in DB Properly """ """ Test that the file storage uploads and puts in DB Properly """
@ -113,7 +116,7 @@ class DBFileTest(TestCase):
url = default_storage.url(self.filepath) url = default_storage.url(self.filepath)
resp = client.get(url) resp = client.get(url)
self.assertEqual(resp.status_code, 200) self.assertEqual(resp.status_code, 200)
def test_view_fails(self): def test_view_fails(self):
client = Client() client = Client()
url = default_storage.url("failure") url = default_storage.url("failure")
@ -135,3 +138,19 @@ class DBFileTest(TestCase):
""" Ensure we can get the modified time """ """ Ensure we can get the modified time """
mtime = default_storage.modified_time(self.filepath) mtime = default_storage.modified_time(self.filepath)
self.assertIsNotNone(mtime) self.assertIsNotNone(mtime)
def test_listdir(self):
""" Make sure listdir works, and only returns things under 'dirname' """
names = [
u'dirname/kris.jpg',
u'dirname/kris2.jpg',
u'dirname/kris3.jpg']
for name in names:
self._upload(name=name)
self.assertEqual(
default_storage.listdir("dirname"),
([], names)
)