Commit ea5f54cd authored by Mark Allen's avatar Mark Allen
Browse files

Add Python 2 compatibility for If-Last-Modified

The serve method of the DjangoStreamingServer class support
If-Last-Modified, and for this it requires a timestamp of the last
modified time of the private file. Python 3 datetimes have a
timestamp method, which is used to generate the required timestamp,
but this isn't available in Python 2. Serving files on S3 therefore
fails if Python 2 is used.

This commit adds an alternative means of generating the timestamp
if Python 2 is used.
parent ce8b0b14
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Sending files efficiently for different kind of webservers. Sending files efficiently for different kind of webservers.
""" """
import os import os
import time
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
...@@ -41,7 +42,10 @@ class DjangoStreamingServer(object): ...@@ -41,7 +42,10 @@ class DjangoStreamingServer(object):
@staticmethod @staticmethod
def serve(private_file): def serve(private_file):
# Support If-Last-Modified # Support If-Last-Modified
if sys.version_info >= (3,):
mtime = private_file.modified_time.timestamp() mtime = private_file.modified_time.timestamp()
else:
mtime = time.mktime(private_file.modified_time.timetuple())
size = private_file.size size = private_file.size
if not was_modified_since(private_file.request.META.get('HTTP_IF_MODIFIED_SINCE'), mtime, size): if not was_modified_since(private_file.request.META.get('HTTP_IF_MODIFIED_SINCE'), mtime, size):
return HttpResponseNotModified() return HttpResponseNotModified()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment