| 1 |
"""Base material for download views: :class:`DownloadMixin` and |
| 2 |
:class:`BaseDownloadView`""" |
| 3 |
|
| 4 |
import calendar |
| 5 |
|
| 6 |
from django.http import Http404, HttpResponseNotModified |
| 7 |
from django.views.generic.base import View |
| 8 |
from django.views.static import was_modified_since |
| 9 |
|
| 10 |
from django_downloadview import exceptions |
| 11 |
from django_downloadview.response import DownloadResponse |
| 12 |
|
| 13 |
|
| 14 |
class DownloadMixin(object): |
| 15 |
"""Placeholders and base implementation to create file download views. |
| 16 |
|
| 17 |
.. note:: |
| 18 |
|
| 19 |
This class does not inherit from |
| 20 |
:py:class:`django.views.generic.base.View`. |
| 21 |
|
| 22 |
The :py:meth:`get_file` method is a placeholder subclasses must implement. |
| 23 |
Base implementation raises ``NotImplementedError``. |
| 24 |
|
| 25 |
Other methods provide a base implementation that use the file wrapper |
| 26 |
returned by :py:meth:`get_file`. |
| 27 |
|
| 28 |
""" |
| 29 |
|
| 30 |
#: Response class, to be used in :py:meth:`render_to_response`. |
| 31 |
response_class = DownloadResponse |
| 32 |
|
| 33 |
#: Whether to return the response as attachment or not. |
| 34 |
#: |
| 35 |
#: When ``True`` (the default), the view returns file "as attachment", |
| 36 |
#: which usually triggers a "Save the file as ..." prompt. |
| 37 |
#: |
| 38 |
#: When ``False``, the view returns file "inline", as if it was an element |
| 39 |
#: of the current page. |
| 40 |
#: |
| 41 |
#: .. note:: |
| 42 |
#: |
| 43 |
#: The actual behaviour client-side depends on the browser and its |
| 44 |
#: configuration. |
| 45 |
#: |
| 46 |
#: In fact, affects the "Content-Disposition" header via :attr:`response's |
| 47 |
#: attachment attribute |
| 48 |
#: <django_downloadview.response.DownloadResponse.attachment>`. |
| 49 |
attachment = True |
| 50 |
|
| 51 |
#: Client-side filename, if only file is returned as attachment. |
| 52 |
basename = None |
| 53 |
|
| 54 |
#: File's mime type. |
| 55 |
#: If ``None`` (the default), then the file's mime type will be guessed via |
| 56 |
#: :mod:`mimetypes`. |
| 57 |
mimetype = None |
| 58 |
|
| 59 |
#: File's encoding. |
| 60 |
#: If ``None`` (the default), then the file's encoding will be guessed via |
| 61 |
#: :mod:`mimetypes`. |
| 62 |
encoding = None |
| 63 |
|
| 64 |
def get_file(self): |
| 65 |
"""Return a file wrapper instance. |
| 66 |
|
| 67 |
Raises :class:`~django_downloadview.exceptions.FileNotFound` if file |
| 68 |
does not exist. |
| 69 |
|
| 70 |
""" |
| 71 |
raise NotImplementedError() |
| 72 |
|
| 73 |
def get_basename(self): |
| 74 |
"""Return :attr:`basename`. |
| 75 |
|
| 76 |
Override this method if you need more dynamic basename. |
| 77 |
|
| 78 |
""" |
| 79 |
return self.basename |
| 80 |
|
| 81 |
def get_mimetype(self): |
| 82 |
"""Return :attr:`mimetype`. |
| 83 |
|
| 84 |
Override this method if you need more dynamic mime type. |
| 85 |
|
| 86 |
""" |
| 87 |
return self.mimetype |
| 88 |
|
| 89 |
def get_encoding(self): |
| 90 |
"""Return :attr:`encoding`. |
| 91 |
|
| 92 |
Override this method if you need more dynamic encoding. |
| 93 |
|
| 94 |
""" |
| 95 |
return self.encoding |
| 96 |
|
| 97 |
def was_modified_since(self, file_instance, since): |
| 98 |
"""Return True if ``file_instance`` was modified after ``since``. |
| 99 |
|
| 100 |
Uses file wrapper's ``was_modified_since`` if available, with value of |
| 101 |
``since`` as positional argument. |
| 102 |
|
| 103 |
Else, fallbacks to default implementation, which uses |
| 104 |
:py:func:`django.views.static.was_modified_since`. |
| 105 |
|
| 106 |
Django's ``was_modified_since`` function needs a datetime. |
| 107 |
It is passed the ``modified_time`` attribute from file |
| 108 |
wrapper. If file wrapper does not support this attribute |
| 109 |
(``AttributeError`` or ``NotImplementedError`` is raised), then |
| 110 |
the file is considered as modified and ``True`` is returned. |
| 111 |
|
| 112 |
""" |
| 113 |
try: |
| 114 |
return file_instance.was_modified_since(since) |
| 115 |
except (AttributeError, NotImplementedError): |
| 116 |
try: |
| 117 |
modification_time = calendar.timegm( |
| 118 |
file_instance.modified_time.utctimetuple() |
| 119 |
) |
| 120 |
except (AttributeError, NotImplementedError) as e: |
| 121 |
print("!=======!", e) |
| 122 |
return True |
| 123 |
else: |
| 124 |
return was_modified_since(since, modification_time) |
| 125 |
|
| 126 |
def not_modified_response(self, *response_args, **response_kwargs): |
| 127 |
"""Return :class:`django.http.HttpResponseNotModified` instance.""" |
| 128 |
return HttpResponseNotModified(*response_args, **response_kwargs) |
| 129 |
|
| 130 |
def download_response(self, *response_args, **response_kwargs): |
| 131 |
"""Return :class:`~django_downloadview.response.DownloadResponse`.""" |
| 132 |
response_kwargs.setdefault("file_instance", self.file_instance) |
| 133 |
response_kwargs.setdefault("attachment", self.attachment) |
| 134 |
response_kwargs.setdefault("basename", self.get_basename()) |
| 135 |
response_kwargs.setdefault("file_mimetype", self.get_mimetype()) |
| 136 |
response_kwargs.setdefault("file_encoding", self.get_encoding()) |
| 137 |
response = self.response_class(*response_args, **response_kwargs) |
| 138 |
return response |
| 139 |
|
| 140 |
def file_not_found_response(self): |
| 141 |
"""Raise Http404.""" |
| 142 |
raise Http404() |
| 143 |
|
| 144 |
def render_to_response(self, *response_args, **response_kwargs): |
| 145 |
"""Return "download" response (if everything is ok). |
| 146 |
|
| 147 |
Return :meth:`file_not_found_response` if file does not exist. |
| 148 |
|
| 149 |
Respects the "HTTP_IF_MODIFIED_SINCE" header if any. In that case, uses |
| 150 |
:py:meth:`was_modified_since` and :py:meth:`not_modified_response`. |
| 151 |
|
| 152 |
Else, uses :py:meth:`download_response` to return a download response. |
| 153 |
|
| 154 |
""" |
| 155 |
try: |
| 156 |
self.file_instance = self.get_file() |
| 157 |
except exceptions.FileNotFound: |
| 158 |
return self.file_not_found_response() |
| 159 |
# Respect the If-Modified-Since header. |
| 160 |
since = self.request.headers.get("if-modified-since", None) |
| 161 |
if since is not None: |
| 162 |
if not self.was_modified_since(self.file_instance, since): |
| 163 |
return self.not_modified_response(**response_kwargs) |
| 164 |
# Return download response. |
| 165 |
return self.download_response(*response_args, **response_kwargs) |
| 166 |
|
| 167 |
|
| 168 |
class BaseDownloadView(DownloadMixin, View): |
| 169 |
"""A base :class:`DownloadMixin` that implements :meth:`get`.""" |
| 170 |
|
| 171 |
def get(self, request, *args, **kwargs): |
| 172 |
"""Handle GET requests: stream a file.""" |
| 173 |
return self.render_to_response() |