Debian Codegraph @

docs/views/custom.txt

raw
1 ##################
2 Make your own view
3 ##################
4
5 .. currentmodule:: django_downloadview.views.base
6
7
8 *************
9 DownloadMixin
10 *************
11
12 The :py:class:`django_downloadview.views.DownloadMixin` class is not a view. It
13 is a base class which you can inherit of to create custom download views.
14
15 ``DownloadMixin`` is a base of `BaseDownloadView`_, which itself is a base of
16 all other django_downloadview's builtin views.
17
18 .. autoclass:: DownloadMixin
19 :members:
20 :undoc-members:
21 :show-inheritance:
22 :member-order: bysource
23
24
25 ****************
26 BaseDownloadView
27 ****************
28
29 The :py:class:`django_downloadview.views.BaseDownloadView` class is a base
30 class to create download views. It inherits `DownloadMixin`_ and
31 :py:class:`django.views.generic.base.View`.
32
33 The only thing it does is to implement
34 :py:meth:`get <django_downloadview.views.BaseDownloadView.get>`: it triggers
35 :py:meth:`DownloadMixin's render_to_response
36 <django_downloadview.views.DownloadMixin.render_to_response>`.
37
38 .. autoclass:: BaseDownloadView
39 :members:
40 :undoc-members:
41 :show-inheritance:
42 :member-order: bysource
43
44
45 ***********************************************
46 Serving a file inline rather than as attachment
47 ***********************************************
48
49 Use :attr:`~DownloadMixin.attachment` to make a view serve a file inline rather
50 than as attachment, i.e. to display the file as if it was an internal part of a
51 page rather than triggering "Save file as..." prompt.
52
53 See details in :attr:`attachment API documentation
54 <~DownloadMixin.attachment>`.
55
56 .. literalinclude:: /../demo/demoproject/object/views.py
57 :language: python
58 :lines: 1-2, 19
59
60
61 ************************************
62 Handling http not modified responses
63 ************************************
64
65 Sometimes, you know the latest date and time the content was generated at, and
66 you know a new request would generate exactly the same content. In such a case,
67 you should implement :py:meth:`~VirtualDownloadView.was_modified_since` in your
68 view.
69
70 .. note::
71
72 Default :py:meth:`~VirtualDownloadView.was_modified_since` implementation
73 trusts file wrapper's ``was_modified_since`` if any. Else (if calling
74 ``was_modified_since()`` raises ``NotImplementedError`` or
75 ``AttributeError``) it returns ``True``, i.e. it assumes the file was
76 modified.
77
78 As an example, the download views above always generate "Hello world!"... so,
79 if the client already downloaded it, we can safely return some HTTP "304 Not
80 Modified" response:
81
82 .. code:: python
83
84 from django.core.files.base import ContentFile
85 from django_downloadview import VirtualDownloadView
86
87 class TextDownloadView(VirtualDownloadView):
88 def get_file(self):
89 """Return :class:`django.core.files.base.ContentFile` object."""
90 return ContentFile("Hello world!", name='hello-world.txt')
91
92 def was_modified_since(self, file_instance, since):
93 return False # Never modified, always "Hello world!".