Pass numeric timestamp to Django's was_modified_since() (was receiving datetime). Thanks @benesch and @zerc.

This commit is contained in:
Benoît Bryon 2015-07-20 18:43:46 +02:00
commit c18e57037b
4 changed files with 39 additions and 3 deletions

View file

@ -13,6 +13,9 @@ Bugfixes.
- Bugfix #103 - ``PathDownloadView.get_file()`` makes a single call to
``PathDownloadView.get_file()`` (was doing it twice).
- Bugfix #104 - Pass numeric timestamp to Django's ``was_modified_since()``
(was passing a datetime).
1.7 (2015-06-13)
----------------

View file

@ -1,7 +1,9 @@
import datetime
import unittest
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from django.http.response import HttpResponseNotModified
import django.test
from django_downloadview import assert_download_response, temporary_media_root
@ -31,6 +33,32 @@ class StaticPathTestCase(django.test.TestCase):
basename='1.txt',
mime_type='text/plain')
@temporary_media_root()
def test_not_modified_download_response(self):
"""'storage:static_path' sends not modified response if unmodified."""
setup_file('1.txt')
url = reverse('storage:static_path', kwargs={'path': '1.txt'})
response = self.client.get(
url,
HTTP_IF_MODIFIED_SINCE='Sat, 29 Oct {year} 19:43:31 GMT'.format(
year=datetime.date.today().year + 4)
)
self.assertTrue(isinstance(response, HttpResponseNotModified))
@temporary_media_root()
def test_modified_since_download_response(self):
"""'storage:static_path' streams file if modified."""
setup_file('1.txt')
url = reverse('storage:static_path', kwargs={'path': '1.txt'})
response = self.client.get(
url,
HTTP_IF_MODIFIED_SINCE='Sat, 29 Oct 1980 19:43:31 GMT')
assert_download_response(self,
response,
content=file_content,
basename='1.txt',
mime_type='text/plain')
class DynamicPathIntegrationTestCase(django.test.TestCase):
"""Integration tests around ``storage:dynamic_path`` URL."""

View file

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""Base material for download views: :class:`DownloadMixin` and
:class:`BaseDownloadView`"""
import calendar
from django.http import HttpResponseNotModified, Http404
from django.views.generic.base import View
from django.views.static import was_modified_since
@ -111,7 +113,8 @@ class DownloadMixin(object):
return file_instance.was_modified_since(since)
except (AttributeError, NotImplementedError):
try:
modification_time = file_instance.modified_time
modification_time = calendar.timegm(
file_instance.modified_time.utctimetuple())
size = file_instance.size
except (AttributeError, NotImplementedError):
return True

View file

@ -1,7 +1,9 @@
# coding=utf-8
"""Tests around :mod:`django_downloadview.views`."""
import calendar
import os
import unittest
from datetime import datetime
try:
from unittest import mock
except ImportError:
@ -92,7 +94,7 @@ class DownloadMixinTestCase(unittest.TestCase):
file_wrapper.was_modified_since = mock.Mock(
side_effect=AttributeError)
file_wrapper.size = mock.sentinel.size
file_wrapper.modified_time = mock.sentinel.modified_time
file_wrapper.modified_time = datetime.now()
was_modified_since_mock = mock.Mock(
return_value=mock.sentinel.was_modified)
mixin = views.DownloadMixin()
@ -103,7 +105,7 @@ class DownloadMixinTestCase(unittest.TestCase):
mock.sentinel.was_modified)
was_modified_since_mock.assert_called_once_with(
mock.sentinel.since,
mock.sentinel.modified_time,
calendar.timegm(file_wrapper.modified_time.utctimetuple()),
mock.sentinel.size)
def test_was_modified_since_fallback(self):