| 1 |
############# |
| 2 |
File wrappers |
| 3 |
############# |
| 4 |
|
| 5 |
.. module:: django_downloadview.files |
| 6 |
|
| 7 |
A view return :class:`~django_downloadview.response.DownloadResponse` which |
| 8 |
itself carries a file wrapper. Here are file wrappers distributed by Django |
| 9 |
and django-downloadview. |
| 10 |
|
| 11 |
|
| 12 |
***************** |
| 13 |
Django's builtins |
| 14 |
***************** |
| 15 |
|
| 16 |
`Django itself provides some file wrappers`_ you can use within |
| 17 |
``django-downloadview``: |
| 18 |
|
| 19 |
* :class:`django.core.files.File` wraps a file that live on local |
| 20 |
filesystem, initialized with a path. ``django-downloadview`` uses this |
| 21 |
wrapper in :doc:`/views/path`. |
| 22 |
|
| 23 |
* :class:`django.db.models.fields.files.FieldFile` wraps a file that is |
| 24 |
managed in a model. ``django-downloadview`` uses this wrapper in |
| 25 |
:doc:`/views/object`. |
| 26 |
|
| 27 |
* :class:`django.core.files.base.ContentFile` wraps a bytes, string or |
| 28 |
unicode object. You may use it with :doc:`VirtualDownloadView |
| 29 |
</views/virtual>`. |
| 30 |
|
| 31 |
|
| 32 |
**************************** |
| 33 |
django-downloadview builtins |
| 34 |
**************************** |
| 35 |
|
| 36 |
``django-downloadview`` implements additional file wrappers: |
| 37 |
|
| 38 |
* :class:`StorageFile` wraps a file that is |
| 39 |
managed via a storage (but not necessarily via a model). |
| 40 |
:doc:`/views/storage` uses this wrapper. |
| 41 |
|
| 42 |
* :class:`HTTPFile` wraps a file that lives at |
| 43 |
some (remote) location, initialized with an URL. |
| 44 |
:doc:`/views/http` uses this wrapper. |
| 45 |
|
| 46 |
* :class:`VirtualFile` wraps a file that lives in |
| 47 |
memory, i.e. built as a string. |
| 48 |
This is a convenient wrapper to use in :doc:`/views/virtual` subclasses. |
| 49 |
|
| 50 |
|
| 51 |
********************** |
| 52 |
Low-level IO utilities |
| 53 |
********************** |
| 54 |
|
| 55 |
`django-downloadview` provides two classes to implement file-like objects |
| 56 |
whose content is dynamically generated: |
| 57 |
|
| 58 |
* :class:`~django_downloadview.io.TextIteratorIO` for generated text; |
| 59 |
* :class:`~django_downloadview.io.BytesIteratorIO` for generated bytes. |
| 60 |
|
| 61 |
These classes may be handy to serve dynamically generated files. See |
| 62 |
:doc:`/views/virtual` for details. |
| 63 |
|
| 64 |
.. tip:: |
| 65 |
|
| 66 |
**Text or bytes?** (formerly "unicode or str?") As `django-downloadview` |
| 67 |
is meant to serve files, as opposed to read or parse files, what matters |
| 68 |
is file contents is preserved. `django-downloadview` tends to handle files |
| 69 |
in binary mode and as bytes. |
| 70 |
|
| 71 |
|
| 72 |
************* |
| 73 |
API reference |
| 74 |
************* |
| 75 |
|
| 76 |
StorageFile |
| 77 |
=========== |
| 78 |
|
| 79 |
.. autoclass:: StorageFile |
| 80 |
:members: |
| 81 |
:undoc-members: |
| 82 |
:show-inheritance: |
| 83 |
:member-order: bysource |
| 84 |
|
| 85 |
HTTPFile |
| 86 |
======== |
| 87 |
|
| 88 |
.. autoclass:: HTTPFile |
| 89 |
:members: |
| 90 |
:undoc-members: |
| 91 |
:show-inheritance: |
| 92 |
:member-order: bysource |
| 93 |
|
| 94 |
|
| 95 |
VirtualFile |
| 96 |
=========== |
| 97 |
|
| 98 |
.. autoclass:: VirtualFile |
| 99 |
:members: |
| 100 |
:undoc-members: |
| 101 |
:show-inheritance: |
| 102 |
:member-order: bysource |
| 103 |
|
| 104 |
|
| 105 |
BytesIteratorIO |
| 106 |
=============== |
| 107 |
|
| 108 |
.. autoclass:: django_downloadview.io.BytesIteratorIO |
| 109 |
:members: |
| 110 |
:undoc-members: |
| 111 |
:show-inheritance: |
| 112 |
:member-order: bysource |
| 113 |
|
| 114 |
|
| 115 |
TextIteratorIO |
| 116 |
============== |
| 117 |
|
| 118 |
.. autoclass:: django_downloadview.io.TextIteratorIO |
| 119 |
:members: |
| 120 |
:undoc-members: |
| 121 |
:show-inheritance: |
| 122 |
:member-order: bysource |
| 123 |
|
| 124 |
|
| 125 |
.. rubric:: Notes & references |
| 126 |
|
| 127 |
.. target-notes:: |
| 128 |
|
| 129 |
.. _`Django itself provides some file wrappers`: |
| 130 |
https://docs.djangoproject.com/en/3.0/ref/files/file/ |