Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Fixed #13605 -- Improved documentation of the django.core.fil…

…es.storage module. Added documentation for DefaultStorage, get_storage_class, FileSystemStorage, and some missing public methods on Storage. New metadata targets included for everything. Thanks to kopernikus for the report and elbarto for contributing to the patch.

Backport of [14831] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14832 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a4d88f007c4df2b4225bedbe79b2865733b8117e 1 parent 54b122c
authored December 05, 2010
135  docs/ref/files/storage.txt
... ...
@@ -1,52 +1,119 @@
1 1
 File storage API
2 2
 ================
3 3
 
4  
-``Storage.exists(name)``
5  
-~~~~~~~~~~~~~~~~~~~~~~~~
  4
+.. module:: django.core.files.storage
6 5
 
7  
-``True`` if a file exists given some ``name``.
  6
+Getting the current storage class
  7
+---------------------------------
8 8
 
9  
-``Storage.path(name)``
10  
-~~~~~~~~~~~~~~~~~~~~~~
  9
+Django provides two convenient ways to access the current storage class:
11 10
 
12  
-The local filesystem path where the file can be opened using Python's standard
13  
-``open()``. For storage systems that aren't accessible from the local
14  
-filesystem, this will raise ``NotImplementedError`` instead.
  11
+.. class:: DefaultStorage
15 12
 
16  
-``Storage.size(name)``
17  
-~~~~~~~~~~~~~~~~~~~~~~
  13
+    :class:`~django.core.files.storage.DefaultStorage` provides
  14
+    lazy access to the current default storage system as defined by
  15
+    :setting:`DEFAULT_FILE_STORAGE`. :class:`DefaultStorage` uses
  16
+    :func:`~django.core.files.storage.get_storage_class` internally.
18 17
 
19  
-Returns the total size, in bytes, of the file referenced by ``name``.
  18
+.. function:: get_storage_class([import_path=None])
20 19
 
21  
-``Storage.url(name)``
22  
-~~~~~~~~~~~~~~~~~~~~~
  20
+    Returns a class or module which implements the storage API.
  21
+    
  22
+    When called without the ``import_path`` parameter ``get_storage_class``
  23
+    will return the current default storage system as defined by
  24
+    :setting:`DEFAULT_FILE_STORAGE`. If ``import_path`` is provided,
  25
+    ``get_storage_class`` will attempt to import the class or module from the
  26
+    given path and will return it if successful. An exception will be
  27
+    raised if the import is unsuccessful.
23 28
 
24  
-Returns the URL where the contents of the file referenced by ``name`` can be
25  
-accessed.
  29
+The FileSystemStorage Class
  30
+---------------------------
26 31
 
27  
-``Storage.open(name, mode='rb')``
28  
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32
+.. class:: FileSystemStorage
29 33
 
30  
-Opens the file given by ``name``. Note that although the returned file is
31  
-guaranteed to be a ``File`` object, it might actually be some subclass. In the
32  
-case of remote file storage this means that reading/writing could be quite slow,
33  
-so be warned.
  34
+    The :class:`~django.core.files.storage.FileSystemStorage` class implements
  35
+    basic file storage on a local filesystem. It inherits from
  36
+    :class:`~django.core.files.storage.Storage` and provides implementations
  37
+    for all the public methods thereof.
  38
+    
  39
+    .. note::
  40
+    
  41
+        The :class:`FileSystemStorage.delete` method will not raise
  42
+        raise an exception if the given file name does not exist.
34 43
 
35  
-``Storage.save(name, content)``
36  
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44
+The Storage Class
  45
+-----------------
37 46
 
38  
-Saves a new file using the storage system, preferably with the name specified.
39  
-If there already exists a file with this name ``name``, the storage system may
40  
-modify the filename as necessary to get a unique name. The actual name of the
41  
-stored file will be returned.
  47
+.. class:: Storage
42 48
 
43  
-The ``content`` argument must be an instance of
44  
-:class:`django.core.files.File` or of a subclass of
45  
-:class:`~django.core.files.File`.
  49
+    The :class:`~django.core.files.storage.Storage` class provides a
  50
+    standardized API for storing files, along with a set of default
  51
+    behaviors that all other storage systems can inherit or override
  52
+    as necessary.
46 53
 
47  
-``Storage.delete(name)``
48  
-~~~~~~~~~~~~~~~~~~~~~~~~
  54
+    .. method:: delete(name)
49 55
 
50  
-Deletes the file referenced by ``name``. This method won't raise an exception if
51  
-the file doesn't exist.
  56
+        Deletes the file referenced by ``name``. If deletion is not supported
  57
+        on the targest storage system this will raise ``NotImplementedError``
  58
+        instead
52 59
 
  60
+    .. method:: exists(name)
  61
+
  62
+        Returns ``True`` if a file referened by the given name already exists
  63
+        in the storage system, or ``False`` if the name is available for a new
  64
+        file.
  65
+
  66
+    .. method:: get_available_name(name)
  67
+
  68
+        Returns a filename based on the ``name`` parameter that's free and
  69
+        available for new content to be written to on the target storage
  70
+        system.
  71
+
  72
+
  73
+    .. method:: get_valid_name(name)
  74
+
  75
+        Returns a filename based on the ``name`` parameter that's suitable
  76
+        for use on the target storage system.
  77
+
  78
+    .. method:: listdir(path)
  79
+
  80
+        Lists the contents of the specified path, returning a 2-tuple of lists;
  81
+        the first item being directories, the second item being files. For
  82
+        storage systems that aren't ale to provide such a listing, this will
  83
+        raise a ``NotImplementedError`` instead.
  84
+
  85
+    .. method:: open(name, mode='rb')
  86
+
  87
+        Opens the file given by ``name``. Note that although the returned file
  88
+        is guaranteed to be a ``File`` object, it might actually be some
  89
+        subclass. In the case of remote file storage this means that
  90
+        reading/writing could be quite slow, so be warned.
  91
+
  92
+    .. method:: path(name)
  93
+
  94
+        The local filesystem path where the file can be opened using Python's
  95
+        standard ``open()``. For storage systems that aren't accessible from
  96
+        the local filesystem, this will raise ``NotImplementedError`` instead.
  97
+
  98
+    .. method:: save(name, content)
  99
+
  100
+        Saves a new file using the storage system, preferably with the name
  101
+        specified. If there already exists a file with this name ``name``, the
  102
+        storage system may modify the filename as necessary to get a unique
  103
+        name. The actual name of the stored file will be returned.
  104
+
  105
+        The ``content`` argument must be an instance of
  106
+        :class:`django.core.files.File` or of a subclass of
  107
+        :class:`~django.core.files.File`.
  108
+
  109
+    .. method:: size(name)
  110
+
  111
+        Returns the total size, in bytes, of the file referenced by ``name``.
  112
+        For storage systems that aren't able to return the file size this will
  113
+        raise ``NotImplementedError`` instead.
  114
+
  115
+    .. method:: url(name)
  116
+
  117
+        Returns the URL where the contents of the file referenced by ``name``
  118
+        can be accessed. For storage systems that don't support access by URL
  119
+        this will raise ``NotImplementedError`` instead.
2  docs/ref/settings.txt
@@ -608,7 +608,7 @@ isn't manually specified. Used with ``DEFAULT_CHARSET`` to construct the
608 608
 DEFAULT_FILE_STORAGE
609 609
 --------------------
610 610
 
611  
-Default: ``'django.core.files.storage.FileSystemStorage'``
  611
+Default: :class:`django.core.files.storage.FileSystemStorage`
612 612
 
613 613
 Default file storage class to be used for any file-related operations that don't
614 614
 specify a particular storage system. See :doc:`/topics/files`.

0 notes on commit a4d88f0

Please sign in to comment.
Something went wrong with that request. Please try again.