| 1 |
from io import StringIO |
| 2 |
|
| 3 |
from django.core.files.base import ContentFile |
| 4 |
|
| 5 |
from django_downloadview import TextIteratorIO, VirtualDownloadView, VirtualFile |
| 6 |
|
| 7 |
|
| 8 |
class TextDownloadView(VirtualDownloadView): |
| 9 |
def get_file(self): |
| 10 |
"""Return :class:`django.core.files.base.ContentFile` object.""" |
| 11 |
return ContentFile(b"Hello world!\n", name="hello-world.txt") |
| 12 |
|
| 13 |
|
| 14 |
class StringIODownloadView(VirtualDownloadView): |
| 15 |
def get_file(self): |
| 16 |
"""Return wrapper on ``six.StringIO`` object.""" |
| 17 |
file_obj = StringIO("Hello world!\n") |
| 18 |
return VirtualFile(file_obj, name="hello-world.txt") |
| 19 |
|
| 20 |
|
| 21 |
def generate_hello(): |
| 22 |
yield "Hello " |
| 23 |
yield "world!" |
| 24 |
yield "\n" |
| 25 |
|
| 26 |
|
| 27 |
class GeneratedDownloadView(VirtualDownloadView): |
| 28 |
def get_file(self): |
| 29 |
"""Return wrapper on ``StringIteratorIO`` object.""" |
| 30 |
file_obj = TextIteratorIO(generate_hello()) |
| 31 |
return VirtualFile(file_obj, name="hello-world.txt") |