Ticket #10497: storage_times.diff
File storage_times.diff, 6.1 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 2c5f0f4..96bf1dc 100644
a b import tempfile 6 6 import time 7 7 import unittest 8 8 from cStringIO import StringIO 9 from datetime import datetime 9 10 from django.conf import settings 10 11 from django.core.exceptions import SuspiciousOperation 11 from django.core.files.base import ContentFile 12 from django.core.files.base import ContentFile, File 12 13 from django.core.files.images import get_image_dimensions 13 14 from django.core.files.storage import FileSystemStorage 14 15 from django.core.files.uploadedfile import UploadedFile … … class FileStorageTests(unittest.TestCase): 57 58 self.storage.delete('storage_test') 58 59 self.failIf(self.storage.exists('storage_test')) 59 60 61 def test_file_accessed_time(self): 62 """ 63 File storage returns a Datetime object for the last accessed time of 64 a file. 65 """ 66 self.failIf(self.storage.exists('test.file')) 67 68 f = File(open(__file__, 'rb')) 69 f_name = self.storage.save('test.file', f) 70 71 self.assertEqual( 72 self.storage.accessed_time(f_name), 73 datetime.fromtimestamp(os.path.getatime(self.storage.path(f_name))) 74 ) 75 76 self.storage.delete(f_name) 77 78 def test_file_created_time(self): 79 """ 80 File storage returns a Datetime object for the creation time of 81 a file. 82 """ 83 self.failIf(self.storage.exists('test.file')) 84 85 f = File(open(__file__, 'rb')) 86 f_name = self.storage.save('test.file', f) 87 88 self.assertEqual( 89 self.storage.created_time(f_name), 90 datetime.fromtimestamp(os.path.getctime(self.storage.path(f_name))) 91 ) 92 93 self.storage.delete(f_name) 94 95 def test_file_modified_time(self): 96 """ 97 File storage returns a Datetime object for the last modified time of 98 a file. 99 """ 100 self.failIf(self.storage.exists('test.file')) 101 102 f = File(open(__file__, 'rb')) 103 f_name = self.storage.save('test.file', f) 104 105 self.assertEqual( 106 self.storage.modified_time(f_name), 107 datetime.fromtimestamp(os.path.getmtime(self.storage.path(f_name))) 108 ) 109 110 self.storage.delete(f_name) 111 60 112 def test_file_storage_prevents_directory_traversal(self): 61 113 """ 62 114 File storage prevents directory traversal (files can only be accessed if