Listdir v 0.9.1 (#38)

* List Directories

* Adding tests

* bump version and docs
This commit is contained in:
Tyrel Souza 2017-08-11 11:30:15 -04:00 committed by GitHub
parent 682a2ed049
commit 09aef244c9
4 changed files with 54 additions and 6 deletions

View File

@ -93,3 +93,12 @@ class DBFileStorage(Storage):
def modified_time(self, name):
dbf = _get_object(name)
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.
project = u'Django DBFileStorage'
copyright = u'2016, Tyrel Souza'
copyright = u'2017, Tyrel Souza'
author = u'Tyrel Souza'
# The version info for the project you're documenting, acts as replacement for
@ -54,7 +54,7 @@ author = u'Tyrel Souza'
# built documents.
#
# The short X.Y version.
version = u'0.9.0'
version = u'0.9.1'
# The full version, including alpha/beta/rc tags.
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
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
~~~~~~~~~~~~~~~~
Everything else, such as `.path()`, `.delete()`, `.size()`, `.exists()` are
exactly the same, in usage as normal.

View File

@ -21,11 +21,14 @@ class DBFileTest(TestCase):
self.filename = "kris.jpg"
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:
return default_storage.save(self.filepath, f)
return default_storage.save(name, f)
def test_upload(self):
""" Test that the file storage uploads and puts in DB Properly """
@ -135,3 +138,19 @@ class DBFileTest(TestCase):
""" Ensure we can get the modified time """
mtime = default_storage.modified_time(self.filepath)
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)
)