From 92e4bb88ae5ca1066f32020f8d89955357ffd5d6 Mon Sep 17 00:00:00 2001 From: Sven Grunewaldt Date: Thu, 21 Apr 2016 14:00:27 +0200 Subject: [PATCH] Fix Django 1.7 issue --- django_gcloud_storage/__init__.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/django_gcloud_storage/__init__.py b/django_gcloud_storage/__init__.py index 2278067..57ae8fa 100644 --- a/django_gcloud_storage/__init__.py +++ b/django_gcloud_storage/__init__.py @@ -6,6 +6,7 @@ import re from tempfile import SpooledTemporaryFile +import django from django.conf import settings from django.core.exceptions import SuspiciousFileOperation from django.core.files.base import File @@ -17,6 +18,8 @@ from gcloud.exceptions import NotFound from gcloud.storage.bucket import Bucket +DJANGO_17 = django.get_version().startswith('1.7.') + try: # For Python 3.0 and later from urllib import parse as urlparse @@ -78,7 +81,13 @@ def __init__(self, blob, maxsize=1000): def _update_blob(self): # Specify explicit size to avoid problems with not yet spooled temporary files # Djangos File.size property already knows how to handle cases like this - self._blob.upload_from_file(self._tmpfile, size=self.size, rewind=True) + + if DJANGO_17 and self._tmpfile.name is None: # Django bug #22307 + size = self._tmpfile.tell() + else: + size = self.size + + self._blob.upload_from_file(self._tmpfile, size=size, rewind=True) def write(self, content): self._dirty = True