Ticket #10497: storage_times.diff

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

Storage backends with time support.

  • 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..59cb3aa 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 2c5f0f4..96bf1dc 100644
    a b import tempfile  
    66import time
    77import unittest
    88from cStringIO import StringIO
     9from datetime import datetime
    910from django.conf import settings
    1011from django.core.exceptions import SuspiciousOperation
    11 from django.core.files.base import ContentFile
     12from django.core.files.base import ContentFile, File
    1213from django.core.files.images import get_image_dimensions
    1314from django.core.files.storage import FileSystemStorage
    1415from django.core.files.uploadedfile import UploadedFile
    class FileStorageTests(unittest.TestCase):  
    5758        self.storage.delete('storage_test')
    5859        self.failIf(self.storage.exists('storage_test'))
    5960
     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
    60112    def test_file_storage_prevents_directory_traversal(self):
    61113        """
    62114        File storage prevents directory traversal (files can only be accessed if
Back to Top