| 1 |
"""Stream files that you generate or that live in memory.""" |
| 2 |
|
| 3 |
from django_downloadview.views.base import BaseDownloadView |
| 4 |
|
| 5 |
|
| 6 |
class VirtualDownloadView(BaseDownloadView): |
| 7 |
"""Serve not-on-disk or generated-on-the-fly file. |
| 8 |
|
| 9 |
Override the :py:meth:`get_file` method to customize file wrapper. |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
def was_modified_since(self, file_instance, since): |
| 14 |
"""Delegate to file wrapper's was_modified_since, or return True. |
| 15 |
|
| 16 |
This is the implementation of an edge case: when files are generated |
| 17 |
on the fly, we cannot guess whether they have been modified or not. |
| 18 |
If the file wrapper implements ``was_modified_since()`` method, then we |
| 19 |
trust it. Otherwise it is safer to suppose that the file has been |
| 20 |
modified. |
| 21 |
|
| 22 |
This behaviour prevents file size to be computed on the Django side. |
| 23 |
Because computing file size means iterating over all the file contents, |
| 24 |
and we want to avoid that whenever possible. As an example, it could |
| 25 |
reduce all the benefits of working with dynamic file generators... |
| 26 |
which is a major feature of virtual files. |
| 27 |
|
| 28 |
""" |
| 29 |
try: |
| 30 |
return file_instance.was_modified_since(since) |
| 31 |
except (AttributeError, NotImplementedError): |
| 32 |
return True |