Skip to content

Commit

Permalink
Merge pull request #14 from jschneier/fix_new_file_last_modified
Browse files Browse the repository at this point in the history
closes #12 - set modified time for new files
  • Loading branch information
jschneier committed Dec 29, 2014
2 parents 039c82c + 8bd2728 commit 8e5e90c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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
12 changes: 12 additions & 0 deletions tests/test_s3boto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.files.base import ContentFile

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

from storages.compat import urlparse
from storages.backends import s3boto
Expand Down Expand Up @@ -281,3 +282,14 @@ def test_generated_url_is_encoded(self):
self.assertEqual(parsed_url.path,
"/whacky%20%26%20filename.mp4")

def test_new_file_modified_time(self):
self.storage.preload_metadata = True
name = 'test_storage_save.txt'
content = ContentFile('new content')
utcnow = datetime.datetime.utcnow()
with mock.patch('storages.backends.s3boto.datetime') as mock_datetime:
mock_datetime.utcnow.return_value = utcnow
self.storage.save(name, content)
self.assertEqual(self.storage.modified_time(name),
parse_ts(utcnow.strftime(ISO8601)))

0 comments on commit 8e5e90c

Please sign in to comment.