Ticket #10497: storage_times.3.diff

File storage_times.3.diff, 7.3 KB (added by steph, 14 years ago)

Renamed Datetime to match the class name.

  • 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  
    22import errno
    33import urlparse
    44import itertools
     5import datetime
    56
    67from django.conf import settings
    78from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
    class Storage(object):  
    120121        """
    121122        raise NotImplementedError()
    122123
     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
    123148class FileSystemStorage(Storage):
    124149    """
    125150    Standard filesystem storage
    class FileSystemStorage(Storage):  
    220245            raise ValueError("This file is not accessible via a URL.")
    221246        return urlparse.urljoin(self.base_url, name).replace('\\', '/')
    222247
     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
    223260def get_storage_class(import_path=None):
    224261    if import_path is None:
    225262        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..1046374 100644
    a b The local filesystem path where the file can be opened using Python's standard  
    1313``open()``. For storage systems that aren't accessible from the local
    1414filesystem, this will raise ``NotImplementedError`` instead.
    1515
     16``Storage.accessed_time(name)``
     17~~~~~~~~~~~~~~~~~~~~~~
     18
     19Returns a ``datetime`` object containing the last accessed time of the file.
     20For storage systems that aren't able to return the last accessed time, this
     21will raise ``NotImplementedError`` instead.
     22
     23``Storage.created_time(name)``
     24~~~~~~~~~~~~~~~~~~~~~~
     25
     26Returns a ``datetime`` object containing the creation time of the file.
     27For storage systems that aren't able to return the creation time, this
     28will raise ``NotImplementedError`` instead.
     29
     30``Storage.modified_time(name)``
     31~~~~~~~~~~~~~~~~~~~~~~
     32
     33Returns a ``datetime`` object containing the last modified time. For storage
     34systems that aren't able to return the last modified time, this will raise
     35``NotImplementedError`` instead.
     36
    1637``Storage.size(name)``
    1738~~~~~~~~~~~~~~~~~~~~~~
    1839
  • 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  
    66import time
    77import unittest
    88from cStringIO import StringIO
     9from datetime import datetime, timedelta
    910from django.conf import settings
    1011from django.core.exceptions import SuspiciousOperation
    1112from django.core.files.base import ContentFile, File
    class GetStorageClassTests(unittest.TestCase):  
    8182
    8283class FileStorageTests(unittest.TestCase):
    8384    storage_class = FileSystemStorage
    84    
     85
    8586    def setUp(self):
    8687        self.temp_dir = tempfile.mktemp()
    8788        os.makedirs(self.temp_dir)
    8889        self.storage = self.storage_class(location=self.temp_dir,
    8990            base_url='/test_media_url/')
    90    
     91
    9192    def tearDown(self):
    9293        shutil.rmtree(self.temp_dir)
    93        
     94
    9495    def test_file_access_options(self):
    9596        """
    9697        Standard file access options are available, and work as expected.
    class FileStorageTests(unittest.TestCase):  
    104105        f = self.storage.open('storage_test', 'r')
    105106        self.assertEqual(f.read(), 'storage contents')
    106107        f.close()
    107        
     108
    108109        self.storage.delete('storage_test')
    109110        self.failIf(self.storage.exists('storage_test'))
    110111
     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
    111175    def test_file_save_without_name(self):
    112176        """
    113177        File storage extracts the filename from the content object if no
    class CustomStorage(FileSystemStorage):  
    215279
    216280class CustomStorageTests(FileStorageTests):
    217281    storage_class = CustomStorage
    218    
     282
    219283    def test_custom_get_available_name(self):
    220284        first = self.storage.save('custom_storage', ContentFile('custom contents'))
    221285        self.assertEqual(first, 'custom_storage')
Back to Top