Skip to content

Commit

Permalink
closes #12 - set modified time for new files
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneier committed Dec 27, 2014
1 parent 3820fc6 commit bee244d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion storages/backends/s3boto.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import posixpath
import mimetypes
from datetime import datetime
from gzip import GzipFile
from tempfile import SpooledTemporaryFile
import warnings
Expand All @@ -15,7 +16,7 @@
from boto.s3.connection import S3Connection, SubdomainCallingFormat
from boto.exception import S3ResponseError
from boto.s3.key import Key as S3Key
from boto.utils import parse_ts
from boto.utils import parse_ts, ISO8601
except ImportError:
raise ImproperlyConfigured("Could not load Boto's S3 bindings.\n"
"See https://github.com/boto/boto")
Expand Down Expand Up @@ -410,6 +411,7 @@ def _save(self, name, content):
key = self.bucket.new_key(encoded_name)
if self.preload_metadata:
self._entries[encoded_name] = key
key.last_modified = datetime.utcnow().strftime(ISO8601)

key.set_metadata('Content-Type', content_type)
self._save_content(key, content, headers=headers)
Expand Down
20 changes: 13 additions & 7 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.test import TestCase
from django.core.files.base import ContentFile

from boto.utils import parse_ts
from boto.s3.key import Key

from storages.compat import urlparse
Expand All @@ -24,12 +25,6 @@ def test_normal(self):
value = s3boto.parse_ts_extended("Wed, 13 Mar 2013 12:45:49 GMT")
self.assertEquals(value, datetime.datetime(2013, 3, 13, 12, 45, 49))

class S3BotoTestCase(TestCase):
@mock.patch('storages.backends.s3boto.S3Connection')
def setUp(self, S3Connection):
self.storage = s3boto.S3BotoStorage()
self.storage._connection = mock.MagicMock()

class SafeJoinTest(TestCase):
def test_normal(self):
path = s3boto.safe_join("", "path/to/somewhere", "other", "path/to/somewhere")
Expand Down Expand Up @@ -67,7 +62,11 @@ def test_trailing_slash_multi(self):
self.assertEquals(path, "base_url/path/to/somewhere/")


class S3BotoStorageTests(S3BotoTestCase):
class S3BotoStorageTests(TestCase):
@mock.patch('storages.backends.s3boto.S3Connection')
def setUp(self, S3Connection):
self.storage = s3boto.S3BotoStorage()
self.storage._connection = mock.MagicMock()

def test_clean_name(self):
"""
Expand Down Expand Up @@ -281,3 +280,10 @@ def test_generated_url_is_encoded(self):
self.assertEqual(parsed_url.path,
"/whacky%20%26%20filename.mp4")

def test_new_file_last_modified(self):
self.storage.preload_metadata = True
name = 'test_storage_save.txt'
content = ContentFile('new content')
self.storage.save(name, content)
parse_ts(self.storage.modified_time(name)) # no exception

0 comments on commit bee244d

Please sign in to comment.