Skip to content

Commit

Permalink
Fix bug in gentity file downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
aptiko committed Jun 20, 2016
1 parent 95eefc2 commit 0a8404b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/general/release-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Changes in 1.0 microversions
resulting time series could start on a slightly different start date
because of some confusion with the time zone. The bug was fixed in
1.0.1.
- Gentity files could not be downloading because of a bug in the downloading
code. Fixed in 1.0.2.

Version 0.8
===========
Expand Down
10 changes: 6 additions & 4 deletions enhydris/hcore/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,19 @@ def test_gentity_file(self):
mommy.make(User, username='admin', password=make_password('topsecret'),
is_active=True, is_superuser=True, is_staff=True)
mommy.make(Station, name='Komboti')
mommy.make(FileType, mime_type='image/jpeg')
mommy.make(FileType, mime_type='image/gif')

# Upload a gentity file
gentity_id = Station.objects.get(name='Komboti').id
r = self.client.login(username='admin', password='topsecret')
self.assertTrue(r)
self.assertEqual(GentityFile.objects.filter(gentity__id=gentity_id
).count(), 0)
filetype_id = FileType.objects.get(mime_type='image/jpeg').id
filetype_id = FileType.objects.get(mime_type='image/gif').id
small_gif = b'GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00' \
b'\x01\x00\x01\x00\x00\x02\x00;'
with open(os.path.join(self.tempdir, 'aaa.jpg'), 'w+b') as f:
f.write(b'Irrelevant data\n')
f.write(small_gif)
f.seek(0)
response = self.client.post(reverse('gentityfile_add'),
{'gentity': gentity_id,
Expand All @@ -373,7 +375,7 @@ def test_gentity_file(self):
response = self.client.get(reverse('gentityfile_dl',
kwargs={'gf_id': gentity_file_id}))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'Irrelevant data\n')
self.assertEqual(response.content, small_gif)


class TsTestCase(TestCase):
Expand Down
4 changes: 2 additions & 2 deletions enhydris/hcore/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import tempfile
from tempfile import mkstemp
from wsgiref.util import FileWrapper
from zipfile import ZipFile, ZIP_DEFLATED

from django.conf import settings
Expand All @@ -16,7 +17,6 @@
from django.contrib.auth.views import login as auth_login
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.geos import Polygon
from django.core.servers.basehttp import FileWrapper
from django.core.urlresolvers import reverse
from django.db import transaction
from django.db.models import Count, Q
Expand Down Expand Up @@ -842,7 +842,7 @@ def download_gentityfile(request, gf_id):
gfile = get_object_or_404(GentityFile, pk=int(gf_id))
try:
filename = gfile.content.file.name
wrapper = FileWrapper(open(filename))
wrapper = FileWrapper(open(filename, 'rb'))
except IOError:
raise Http404
download_name = gfile.content.name.split('/')[-1]
Expand Down

0 comments on commit 0a8404b

Please sign in to comment.