| 1 |
"""Stream files that live in models.""" |
| 2 |
|
| 3 |
from django.views.generic.detail import SingleObjectMixin |
| 4 |
|
| 5 |
from django_downloadview.exceptions import FileNotFound |
| 6 |
from django_downloadview.views.base import BaseDownloadView |
| 7 |
|
| 8 |
|
| 9 |
class ObjectDownloadView(SingleObjectMixin, BaseDownloadView): |
| 10 |
"""Serve file fields from models. |
| 11 |
|
| 12 |
This class extends :class:`~django.views.generic.detail.SingleObjectMixin`, |
| 13 |
so you can use its arguments to target the instance to operate on: |
| 14 |
``slug``, ``slug_kwarg``, ``model``, ``queryset``... |
| 15 |
|
| 16 |
In addition to :class:`~django.views.generic.detail.SingleObjectMixin` |
| 17 |
arguments, you can set arguments related to the file to be downloaded: |
| 18 |
|
| 19 |
* :attr:`file_field`; |
| 20 |
* :attr:`basename_field`; |
| 21 |
* :attr:`encoding_field`; |
| 22 |
* :attr:`mime_type_field`; |
| 23 |
* :attr:`charset_field`; |
| 24 |
* :attr:`modification_time_field`; |
| 25 |
* :attr:`size_field`. |
| 26 |
|
| 27 |
:attr:`file_field` is the main one. Other arguments are provided for |
| 28 |
convenience, in case your model holds some (deserialized) metadata about |
| 29 |
the file, such as its basename, its modification time, its MIME type... |
| 30 |
These fields may be particularly handy if your file storage is not the |
| 31 |
local filesystem. |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
#: Name of the model's attribute which contains the file to be streamed. |
| 36 |
#: Typically the name of a FileField. |
| 37 |
file_field = "file" |
| 38 |
|
| 39 |
#: Optional name of the model's attribute which contains the basename. |
| 40 |
basename_field = None |
| 41 |
|
| 42 |
#: Optional name of the model's attribute which contains the encoding. |
| 43 |
encoding_field = None |
| 44 |
|
| 45 |
#: Optional name of the model's attribute which contains the MIME type. |
| 46 |
mime_type_field = None |
| 47 |
|
| 48 |
#: Optional name of the model's attribute which contains the charset. |
| 49 |
charset_field = None |
| 50 |
|
| 51 |
#: Optional name of the model's attribute which contains the modification |
| 52 |
# time. |
| 53 |
modification_time_field = None |
| 54 |
|
| 55 |
#: Optional name of the model's attribute which contains the size. |
| 56 |
size_field = None |
| 57 |
|
| 58 |
def get_file(self): |
| 59 |
"""Return :class:`~django.db.models.fields.files.FieldFile` instance. |
| 60 |
|
| 61 |
The file wrapper is model's field specified as :attr:`file_field`. It |
| 62 |
is typically a :class:`~django.db.models.fields.files.FieldFile` or |
| 63 |
subclass. |
| 64 |
|
| 65 |
Raises :class:`~django_downloadview.exceptions.FileNotFound` if |
| 66 |
instance's field is empty. |
| 67 |
|
| 68 |
Additional attributes are set on the file wrapper if :attr:`encoding`, |
| 69 |
:attr:`mime_type`, :attr:`charset`, :attr:`modification_time` or |
| 70 |
:attr:`size` are configured. |
| 71 |
|
| 72 |
""" |
| 73 |
file_instance = getattr(self.object, self.file_field) |
| 74 |
if not file_instance: |
| 75 |
raise FileNotFound( |
| 76 |
f'Field="{self.file_field}" on object="{self.object}" is empty' |
| 77 |
) |
| 78 |
for field in ("encoding", "mime_type", "charset", "modification_time", "size"): |
| 79 |
model_field = getattr(self, "%s_field" % field, False) |
| 80 |
if model_field: |
| 81 |
value = getattr(self.object, model_field) |
| 82 |
setattr(file_instance, field, value) |
| 83 |
return file_instance |
| 84 |
|
| 85 |
def get_basename(self): |
| 86 |
"""Return client-side filename.""" |
| 87 |
basename = super().get_basename() |
| 88 |
if basename is None: |
| 89 |
field = "basename" |
| 90 |
model_field = getattr(self, "%s_field" % field, False) |
| 91 |
if model_field: |
| 92 |
basename = getattr(self.object, model_field) |
| 93 |
return basename |
| 94 |
|
| 95 |
def get(self, request, *args, **kwargs): |
| 96 |
self.object = self.get_object() |
| 97 |
return super().get(request, *args, **kwargs) |