diff --git a/dbfilestorage/storage.py b/dbfilestorage/storage.py index d770385..94ff433 100644 --- a/dbfilestorage/storage.py +++ b/dbfilestorage/storage.py @@ -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) diff --git a/docs/conf.py b/docs/conf.py index 5c065c7..947bd1a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -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 diff --git a/docs/dbfilestorage/usage.rst b/docs/dbfilestorage/usage.rst index 4b69f95..1f8d425 100644 --- a/docs/dbfilestorage/usage.rst +++ b/docs/dbfilestorage/usage.rst @@ -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. + + + diff --git a/tests/tests.py b/tests/tests.py index 0a456fa..1f043a2 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -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 """ @@ -113,7 +116,7 @@ class DBFileTest(TestCase): url = default_storage.url(self.filepath) resp = client.get(url) self.assertEqual(resp.status_code, 200) - + def test_view_fails(self): client = Client() url = default_storage.url("failure") @@ -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) + ) +