1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #23832 -- Added timezone aware Storage API.

New Storage.get_{accessed,created,modified}_time() methods convert the
naive time from now-deprecated {accessed,created_modified}_time()
methods into aware objects in UTC if USE_TZ=True.
This commit is contained in:
James Aylett
2016-02-09 15:00:14 +00:00
committed by Tim Graham
parent eda306f1ce
commit 1ff6e37de4
7 changed files with 390 additions and 36 deletions

View File

@@ -135,6 +135,9 @@ details on these changes.
* Support for the template ``Context.has_key()`` method will be removed.
* Support for the ``django.core.files.storage.Storage.accessed_time()``,
``created_time()``, and ``modified_time()`` methods will be removed.
.. _deprecation-removed-in-1.10:
1.10

View File

@@ -73,10 +73,9 @@ The ``Storage`` class
as necessary.
.. note::
For methods returning naive ``datetime`` objects, the
effective timezone used will be the current value of
``os.environ['TZ']``; note that this is usually set from
Django's :setting:`TIME_ZONE`.
When methods return naive ``datetime`` objects, the effective timezone
used will be the current value of ``os.environ['TZ']``; note that this
is usually set from Django's :setting:`TIME_ZONE`.
.. method:: accessed_time(name)
@@ -85,6 +84,10 @@ The ``Storage`` class
able to return the last accessed time this will raise
``NotImplementedError`` instead.
.. deprecated:: 1.10
Use :meth:`get_accessed_time` instead.
.. method:: created_time(name)
Returns a naive ``datetime`` object containing the creation
@@ -92,6 +95,10 @@ The ``Storage`` class
return the creation time this will raise
``NotImplementedError`` instead.
.. deprecated:: 1.10
Use :meth:`get_created_time` instead.
.. method:: delete(name)
Deletes the file referenced by ``name``. If deletion is not supported
@@ -104,6 +111,17 @@ The ``Storage`` class
in the storage system, or ``False`` if the name is available for a new
file.
.. method:: get_accessed_time(name)
.. versionadded:: 1.10
Returns a :class:`~datetime.datetime` of the last accessed time of the
file. For storage systems unable to return the last accessed time this
will raise :exc:`NotImplementedError`.
If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
otherwise returns a naive ``datetime`` in the local timezone.
.. method:: get_available_name(name, max_length=None)
Returns a filename based on the ``name`` parameter that's free and
@@ -119,6 +137,28 @@ The ``Storage`` class
7 character alphanumeric string is appended to the filename before
the extension.
.. method:: get_created_time(name)
.. versionadded:: 1.10
Returns a :class:`~datetime.datetime` of the creation time of the file.
For storage systems unable to return the creation time this will raise
:exc:`NotImplementedError`.
If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
otherwise returns a naive ``datetime`` in the local timezone.
.. method:: get_modified_time(name)
.. versionadded:: 1.10
Returns a :class:`~datetime.datetime` of the last modified time of the
file. For storage systems unable to return the last modified time this
will raise :exc:`NotImplementedError`.
If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
otherwise returns a naive ``datetime`` in the local timezone.
.. method:: get_valid_name(name)
Returns a filename based on the ``name`` parameter that's suitable
@@ -138,6 +178,10 @@ The ``Storage`` class
the last modified time, this will raise
``NotImplementedError`` instead.
.. deprecated:: 1.10
Use :meth:`get_modified_time` instead.
.. method:: open(name, mode='rb')
Opens the file given by ``name``. Note that although the returned file

View File

@@ -203,7 +203,13 @@ Email
File Storage
~~~~~~~~~~~~
* ...
* Storage backends now present a timezone-aware API with new methods
:meth:`~django.core.files.storage.Storage.get_accessed_time`,
:meth:`~django.core.files.storage.Storage.get_created_time`, and
:meth:`~django.core.files.storage.Storage.get_modified_time`. They return a
timezone-aware ``datetime`` if :setting:`USE_TZ` is ``True`` and a naive
``datetime`` in the local timezone otherwise.
File Uploads
~~~~~~~~~~~~
@@ -623,6 +629,21 @@ added in Django 1.9::
This prevents confusion about an assignment resulting in an implicit save.
Non-timezone-aware :class:`~django.core.files.storage.Storage` API
------------------------------------------------------------------
The old, non-timezone-aware methods ``accessed_time()``, ``created_time()``,
and ``modified_time()`` are deprecated in favor of the new ``get_*_time()``
methods.
Third-party storage backends should implement the new methods and mark the old
ones as deprecated. Until then, the new ``get_*_time()`` methods on the base
:class:`~django.core.files.storage.Storage` class convert ``datetime``\s from
the old methods as required and emit a deprecation warning as they do so.
Third-party storage backends may retain the old methods as long as they
wish to support earlier versions of Django.
:mod:`django.contrib.gis`
-------------------------