Ticket #10497: storage_times.2.diff
File storage_times.2.diff, 7.3 KB (added by , 14 years ago) |
---|
-
django/core/files/storage.py
diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 4f27502..0746331 100644
a b import os 2 2 import errno 3 3 import urlparse 4 4 import itertools 5 import datetime 5 6 6 7 from django.conf import settings 7 8 from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation … … class Storage(object): 120 121 """ 121 122 raise NotImplementedError() 122 123 124 def accessed_time(self, name): 125 """ 126 Returns the last accessed time (as Datetime object) for a given name. 127 If the subclass don't support accessed times, a NotImplementedError 128 should be raised. (just dont overwrite this method in the subclass) 129 """ 130 raise NotImplementedError("This backend doesn't support accessed time.") 131 132 def created_time(self, name): 133 """ 134 Returns the creation time (as Datetime object) for a given name. 135 If the subclass don't support creation times, a NotImplementedError 136 should be raised. (just dont overwrite this method in the subclass) 137 """ 138 raise NotImplementedError("This backend doesn't support created time.") 139 140 def modified_time(self, name): 141 """ 142 Returns the last modified time (as Datetime object) for a given name. 143 If the subclass don't support modified times, a NotImplementedError 144 should be raised. (just dont overwrite this method in the subclass) 145 """ 146 raise NotImplementedError("This backend doesn't support modified time.") 147 123 148 class FileSystemStorage(Storage): 124 149 """ 125 150 Standard filesystem storage … … class FileSystemStorage(Storage): 220 245 raise ValueError("This file is not accessible via a URL.") 221 246 return urlparse.urljoin(self.base_url, name).replace('\\', '/') 222 247 248 def accessed_time(self, name): 249 return datetime.datetime.fromtimestamp( 250 os.path.getatime(self.path(name))) 251 252 def created_time(self, name): 253 return datetime.datetime.fromtimestamp( 254 os.path.getctime(self.path(name))) 255 256 def modified_time(self, name): 257 return datetime.datetime.fromtimestamp( 258 os.path.getmtime(self.path(name))) 259 223 260 def get_storage_class(import_path=None): 224 261 if import_path is None: 225 262 import_path = settings.DEFAULT_FILE_STORAGE -
docs/ref/files/storage.txt
diff --git a/docs/ref/files/storage.txt b/docs/ref/files/storage.txt index 2b055bb..59cb3aa 100644
a b The local filesystem path where the file can be opened using Python's standard 13 13 ``open()``. For storage systems that aren't accessible from the local 14 14 filesystem, this will raise ``NotImplementedError`` instead. 15 15 16 ``Storage.accessed_time(name)`` 17 ~~~~~~~~~~~~~~~~~~~~~~ 18 19 Returns a ``Datetime`` object containing the last accessed time of the file. 20 For storage systems that aren't able to return the last accessed time, this 21 will raise ``NotImplementedError`` instead. 22 23 ``Storage.created_time(name)`` 24 ~~~~~~~~~~~~~~~~~~~~~~ 25 26 Returns a ``Datetime`` object containing the creation time of the file. 27 For storage systems that aren't able to return the creation time, this 28 will raise ``NotImplementedError`` instead. 29 30 ``Storage.modified_time(name)`` 31 ~~~~~~~~~~~~~~~~~~~~~~ 32 33 Returns a ``Datetime`` object containing the last modified time. For storage 34 systems that aren't able to return the last modified time, this will raise 35 ``NotImplementedError`` instead. 36 16 37 ``Storage.size(name)`` 17 38 ~~~~~~~~~~~~~~~~~~~~~~ 18 39 -
tests/regressiontests/file_storage/tests.py
diff --git a/tests/regressiontests/file_storage/tests.py b/tests/regressiontests/file_storage/tests.py index d59fe9c..7297bdb 100644
a b import tempfile 6 6 import time 7 7 import unittest 8 8 from cStringIO import StringIO 9 from datetime import datetime, timedelta 9 10 from django.conf import settings 10 11 from django.core.exceptions import SuspiciousOperation 11 12 from django.core.files.base import ContentFile, File … … class GetStorageClassTests(unittest.TestCase): 81 82 82 83 class FileStorageTests(unittest.TestCase): 83 84 storage_class = FileSystemStorage 84 85 85 86 def setUp(self): 86 87 self.temp_dir = tempfile.mktemp() 87 88 os.makedirs(self.temp_dir) 88 89 self.storage = self.storage_class(location=self.temp_dir, 89 90 base_url='/test_media_url/') 90 91 91 92 def tearDown(self): 92 93 shutil.rmtree(self.temp_dir) 93 94 94 95 def test_file_access_options(self): 95 96 """ 96 97 Standard file access options are available, and work as expected. … … class FileStorageTests(unittest.TestCase): 104 105 f = self.storage.open('storage_test', 'r') 105 106 self.assertEqual(f.read(), 'storage contents') 106 107 f.close() 107 108 108 109 self.storage.delete('storage_test') 109 110 self.failIf(self.storage.exists('storage_test')) 110 111 112 def test_file_accessed_time(self): 113 """ 114 File storage returns a Datetime object for the last accessed time of 115 a file. 116 """ 117 self.failIf(self.storage.exists('test.file')) 118 119 f = File(open(__file__, 'rb')) 120 f_name = self.storage.save('test.file', f) 121 122 self.assertEqual( 123 self.storage.accessed_time(f_name), 124 datetime.fromtimestamp(os.path.getatime(self.storage.path(f_name))) 125 ) 126 127 self.assertTrue( 128 datetime.now() - self.storage.accessed_time(f_name) < timedelta(seconds=2) 129 ) 130 131 self.storage.delete(f_name) 132 133 def test_file_created_time(self): 134 """ 135 File storage returns a Datetime object for the creation time of 136 a file. 137 """ 138 self.failIf(self.storage.exists('test.file')) 139 140 f = File(open(__file__, 'rb')) 141 f_name = self.storage.save('test.file', f) 142 143 self.assertEqual( 144 self.storage.created_time(f_name), 145 datetime.fromtimestamp(os.path.getctime(self.storage.path(f_name))) 146 ) 147 148 self.assertTrue( 149 datetime.now() - self.storage.created_time(f_name) < timedelta(seconds=2) 150 ) 151 152 self.storage.delete(f_name) 153 154 def test_file_modified_time(self): 155 """ 156 File storage returns a Datetime object for the last modified time of 157 a file. 158 """ 159 self.failIf(self.storage.exists('test.file')) 160 161 f = File(open(__file__, 'rb')) 162 f_name = self.storage.save('test.file', f) 163 164 self.assertEqual( 165 self.storage.modified_time(f_name), 166 datetime.fromtimestamp(os.path.getmtime(self.storage.path(f_name))) 167 ) 168 169 self.assertTrue( 170 datetime.now() - self.storage.modified_time(f_name) < timedelta(seconds=2) 171 ) 172 173 self.storage.delete(f_name) 174 111 175 def test_file_save_without_name(self): 112 176 """ 113 177 File storage extracts the filename from the content object if no … … class CustomStorage(FileSystemStorage): 215 279 216 280 class CustomStorageTests(FileStorageTests): 217 281 storage_class = CustomStorage 218 282 219 283 def test_custom_get_available_name(self): 220 284 first = self.storage.save('custom_storage', ContentFile('custom contents')) 221 285 self.assertEqual(first, 'custom_storage')