From 6df5f1c674485e853523d23b41010296c90d061b Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 25 Jan 2017 14:59:43 -0500 Subject: [PATCH] Allow filename to stay. dont hash the name, this will honor the upload_to (#31) * Allow filename to stay. dont hash the name, this will honor the upload_to setting * Version bump and changelog --- README.md | 1 + dbfilestorage/storage.py | 5 ++--- docs/conf.py | 4 ++-- setup.py | 2 +- tests/tests.py | 9 ++++----- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c959c3e..811e4a9 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ I will sign everything with 0x769A1BC78A2DDEE2 ## CHANGELOG +- 2017-01-25 [Tyrel Souza] Keeping Filename on upload. - 2017-01-23 [Tyrel Souza] Add Modified Time to storage support - 2017-01-23 [Tyrel Souza] Everything should return a "filename" even if it's generated. Make the filename be the hash + ext. (fall back to .txt) - 2017-01-20 [Tyrel Souza] Make path return None if no file diff --git a/dbfilestorage/storage.py b/dbfilestorage/storage.py index 5e6e618..2c6febb 100644 --- a/dbfilestorage/storage.py +++ b/dbfilestorage/storage.py @@ -15,7 +15,7 @@ L = logging.getLogger(__name__) def _get_object(param): - return DBFile.objects.filter(Q(name=param)|Q(filehash=param)).first() + return DBFile.objects.filter(Q(name=param) | Q(filehash=param)).first() class DBFileStorage(Storage): @@ -61,7 +61,6 @@ class DBFileStorage(Storage): file_ext = os.path.splitext(name)[1] if not file_ext: file_ext = ".txt" - name = "".join((filehash, file_ext)) # create the file, or just return name if the exact file already exists if not DBFile.objects.filter(pk=filehash).exists(): @@ -83,7 +82,7 @@ class DBFileStorage(Storage): DBFile.objects.filter(pk=name).delete() def exists(self, name): - return DBFile.objects.filter(Q(name=name)|Q(filehash=name)).exists() + return DBFile.objects.filter(Q(name=name) | Q(filehash=name)).exists() def size(self, name): dbf = _get_object(name) diff --git a/docs/conf.py b/docs/conf.py index 871c0f0..fc2a23c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -54,9 +54,9 @@ author = u'Tyrel Souza' # built documents. # # The short X.Y version. -version = u'0.5.0' +version = u'0.5.1' # The full version, including alpha/beta/rc tags. -release = u'0.5.0' +release = u'0.5.1' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/setup.py b/setup.py index bcc7fde..2f5b913 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ class CleanCommand(Command): setup( name="django-dbfilestorage", - version="0.5.0", + version="0.5.1", description="Database backed file storage for testing.", long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.", author="Tyrel Souza", diff --git a/tests/tests.py b/tests/tests.py index d77c79c..72f48e0 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -22,7 +22,6 @@ class DBFileTest(TestCase): self.filename = "kris.jpg" self.filepath = os.path.join(PROJECT_ROOT, "test_files", self.filename) self.md5 = hashlib.md5(open(self.filepath, 'rb').read()).hexdigest() - self.filehash_ext = self.md5 + ".jpg" self._upload() @@ -47,7 +46,7 @@ class DBFileTest(TestCase): """ Test that it won't make a new file if it already exists """ # uploads once in setup already name2 = self._upload() - self.assertEqual(self.filehash_ext, name2) + self.assertEqual(self.filepath, name2) def test_equality(self): """ Test that the DB entry matches what is expected from the file """ @@ -83,12 +82,12 @@ class DBFileTest(TestCase): def test_url(self): """ Test that the url returned is the md5 path not the filename """ - self.assertIn(self.md5, default_storage.url(self.md5)) + self.assertIn(self.filename, default_storage.url(self.md5)) def test_view(self): client = Client() # check it works for both md5 and filename - for param in (self.md5, self.filehash_ext): + for param in (self.md5, self.filepath): url = default_storage.url(param) resp = client.get(url) self.assertEqual(resp.status_code, 200) @@ -112,5 +111,5 @@ class DBFileTest(TestCase): def test_mtime(self): """ Ensure we can get the modified time """ - mtime = default_storage.modified_time(self.filehash_ext) + mtime = default_storage.modified_time(self.filepath) self.assertIsNotNone(mtime)