Debian Codegraph @

docs/views/virtual.txt

raw
1 ###################
2 VirtualDownloadView
3 ###################
4
5 .. py:module:: django_downloadview.views.virtual
6
7 :class:`VirtualDownloadView` **serves files that do not live on disk**.
8 Use it when you want to stream a file which content is dynamically generated
9 or which lives in memory.
10
11 It is all about overriding :meth:`VirtualDownloadView.get_file` method so that
12 it returns a suitable file wrapper...
13
14 .. note::
15
16 Current implementation does not support reverse-proxy optimizations,
17 because content is actually generated within Django, not stored in some
18 third-party place.
19
20
21 ************
22 Base options
23 ************
24
25 :class:`VirtualDownloadView` inherits from
26 :class:`~django_downloadview.views.base.DownloadMixin`, which has various
27 options such as :attr:`~django_downloadview.views.base.DownloadMixin.basename`
28 or :attr:`~django_downloadview.views.base.DownloadMixin.attachment`.
29
30
31 ***************************************
32 Serve text (string or unicode) or bytes
33 ***************************************
34
35 Let's consider you build text dynamically, as a bytes or string or unicode
36 object. Serve it with Django's builtin
37 :class:`~django.core.files.base.ContentFile` wrapper:
38
39 .. literalinclude:: /../demo/demoproject/virtual/views.py
40 :language: python
41 :lines: 1, 3, 7-11
42
43
44 **************
45 Serve StringIO
46 **************
47
48 :class:`~StringIO.StringIO` object lives in memory. Let's wrap it in some
49 download view via :class:`~django_downloadview.files.VirtualFile`:
50
51 .. literalinclude:: /../demo/demoproject/virtual/views.py
52 :language: python
53 :lines: 1-4, 5-6, 13-17
54
55
56 ************************
57 Stream generated content
58 ************************
59
60 Let's consider you have a generator function (``yield``) or an iterator object
61 (``__iter__()``):
62
63
64 .. literalinclude:: /../demo/demoproject/virtual/views.py
65 :language: python
66 :lines: 20-23
67
68
69 Stream generated content using :class:`VirtualDownloadView`,
70 :class:`~django_downloadview.files.VirtualFile` and
71 :class:`~django_downloadview.io.BytesIteratorIO`:
72
73 .. literalinclude:: /../demo/demoproject/virtual/views.py
74 :language: python
75 :lines: 3, 26-30
76
77
78 *************
79 API reference
80 *************
81
82 .. autoclass:: VirtualDownloadView
83 :members:
84 :undoc-members:
85 :show-inheritance:
86 :member-order: bysource