Have save overwrite existing files (#36)

* Have save overwrite existing files

* Change unicode string to non-addgene branded

* Bump version numbers
This commit is contained in:
Pamela McA'Nulty 2017-05-10 10:46:12 -04:00 committed by Tyrel Souza
parent 8f811db7c8
commit 095da166ee
4 changed files with 38 additions and 5 deletions

View File

@ -55,11 +55,17 @@ class DBFileStorage(Storage):
ct = mimetypes.guess_type(name)[0] ct = mimetypes.guess_type(name)[0]
# create the file, or just return name if the exact file already exists # create the file, or just return name if the exact file already exists
if not DBFile.objects.filter(name=name).exists(): the_file = DBFile.objects.filter(name=name).first()
the_file = DBFile.objects.create( if not the_file:
DBFile.objects.create(
name=name, name=name,
content_type=ct, content_type=ct,
b64=b64) b64=b64)
else:
the_file.content_type=ct
the_file.b64 = b64
the_file.save()
return name return name
def get_available_name(self, name, max_length=None): def get_available_name(self, name, max_length=None):

View File

@ -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.8.0' version = u'0.9.0'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = version release = version

View File

@ -24,7 +24,7 @@ class CleanCommand(Command):
setup( setup(
name="django-dbfilestorage", name="django-dbfilestorage",
version="0.8.0", version="0.9.0",
description="Database backed file storage for testing.", description="Database backed file storage for testing.",
long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.", long_description="Database backed file storage for testing. Stores files as base64 encoded textfields.",
author="Tyrel Souza", author="Tyrel Souza",

View File

@ -33,7 +33,7 @@ class DBFileTest(TestCase):
def test_content_file(self): def test_content_file(self):
""" Test that this code works with ContentFile as well """ """ Test that this code works with ContentFile as well """
content_file = ContentFile(u"ΑΔΔGΕΝΕ") content_file = ContentFile(u"ƊBStørage")
default_storage.save("unicode", content_file) default_storage.save("unicode", content_file)
unicode_file = DBFile.objects.get(name="unicode") unicode_file = DBFile.objects.get(name="unicode")
self.assertEqual(unicode(unicode_file), self.assertEqual(unicode(unicode_file),
@ -77,6 +77,33 @@ class DBFileTest(TestCase):
size = default_storage.size(self.filepath) size = default_storage.size(self.filepath)
self.assertGreater(size, 0) self.assertGreater(size, 0)
def test_raw_save(self):
CONTENT_DATA_1 = u"Here's some stuff! ƊBStørage - ONE"
CONTENT_DATA_2 = u"Here's some stuff! ƊBStørage - TWO"
FILE_NAME = "saveable.txt"
self.assertFalse(DBFile.objects.filter(name=FILE_NAME).exists())
# -- Save to a _new_ file
content_file = ContentFile(CONTENT_DATA_1.encode("utf-8"))
default_storage.save(FILE_NAME, content_file)
self.assertTrue(DBFile.objects.filter(name=FILE_NAME).exists())
with default_storage.open(FILE_NAME, "rb") as f:
read_back = f.read().decode("utf-8")
self.assertEqual(read_back, CONTENT_DATA_1)
# -- Save to an _existing_ file
content_file = ContentFile(CONTENT_DATA_2.encode("utf-8"))
default_storage.save(FILE_NAME, content_file)
self.assertTrue(DBFile.objects.filter(name=FILE_NAME).exists())
with default_storage.open(FILE_NAME, "rb") as f:
read_back = f.read().decode("utf-8")
self.assertEqual(read_back, CONTENT_DATA_2)
# -- Clean up after ourselves
default_storage.delete(FILE_NAME)
self.assertFalse(DBFile.objects.filter(name=FILE_NAME).exists())
def test_url(self): def test_url(self):
""" Test that the url returned is the filename """ """ Test that the url returned is the filename """
self.assertIn(self.filename, default_storage.url(self.filename)) self.assertIn(self.filename, default_storage.url(self.filename))