Commit 197bd20f by Luke Plant

Fixed uploading of attachments using Python3

Previously it was failing with "TypeError: Unicode-objects must be encoded
before hashing" when taking the md5 of a Python3 str object.
parent 61ffee06
...@@ -61,9 +61,9 @@ argv = [sys.argv[0], "test"] ...@@ -61,9 +61,9 @@ argv = [sys.argv[0], "test"]
if len(sys.argv) == 1: if len(sys.argv) == 1:
# Nothing following 'runtests.py': # Nothing following 'runtests.py':
if django.VERSION < (1,6): if django.VERSION < (1,6):
argv.append("wiki") argv.extend(["wiki", "attachments"])
else: else:
argv.append("wiki.tests") argv.extend(["wiki.tests", "wiki.plugins.attachments.tests"])
else: else:
# Allow tests to be specified: # Allow tests to be specified:
argv.extend(sys.argv[1:]) argv.extend(sys.argv[1:])
......
...@@ -81,7 +81,7 @@ def upload_path(instance, filename): ...@@ -81,7 +81,7 @@ def upload_path(instance, filename):
upload_path = upload_path.replace('%aid', str(instance.attachment.article.id)) upload_path = upload_path.replace('%aid', str(instance.attachment.article.id))
if settings.UPLOAD_PATH_OBSCURIFY: if settings.UPLOAD_PATH_OBSCURIFY:
import random, hashlib import random, hashlib
m=hashlib.md5(str(random.randint(0,100000000000000))) m=hashlib.md5(str(random.randint(0,100000000000000)).encode('ascii'))
upload_path = path.join(upload_path, m.hexdigest()) upload_path = path.join(upload_path, m.hexdigest())
if settings.APPEND_EXTENSION: if settings.APPEND_EXTENSION:
......
from __future__ import absolute_import
import django
if django.VERSION < (1, 6):
# New style autodiscovery of tests doesn't work for Django < 1.6
from .test_views import *
from __future__ import print_function, unicode_literals
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.urlresolvers import reverse
from wiki.tests.base import ArticleTestBase
class AttachmentTests(ArticleTestBase):
def test_upload(self):
data = "This is a plain text file".encode('utf-8')
filedata = BytesIO(data)
filestream = InMemoryUploadedFile(filedata, None, 'test.txt', 'text', len(data), None)
article = self.root_article
url = reverse('wiki:attachments_index', kwargs={'path': ''})
response = self.c.post(url,
{'description': 'My file',
'file': filestream,
'save': '1',
})
self.assertRedirects(response, url)
# Check the object was created.
attachment = article.shared_plugins_set.all()[0].attachment
self.assertEqual(attachment.original_filename, 'test.txt')
self.assertEqual(attachment.current_revision.file.file.read(),
data)
...@@ -2,6 +2,7 @@ from django.core.urlresolvers import reverse ...@@ -2,6 +2,7 @@ from django.core.urlresolvers import reverse
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from wiki.models import URLPath
class WebTestBase(TestCase): class WebTestBase(TestCase):
def setUp(self): def setUp(self):
...@@ -22,6 +23,7 @@ class ArticleTestBase(WebTestBase): ...@@ -22,6 +23,7 @@ class ArticleTestBase(WebTestBase):
super(ArticleTestBase, self).setUp() super(ArticleTestBase, self).setUp()
response = self.c.post(reverse('wiki:root_create'), {'content': 'root article content', 'title': 'Root Article'}, follow=True) response = self.c.post(reverse('wiki:root_create'), {'content': 'root article content', 'title': 'Root Article'}, follow=True)
self.assertEqual(response.status_code, 200) # sanity check self.assertEqual(response.status_code, 200) # sanity check
self.root_article = URLPath.root().article
self.example_data = { self.example_data = {
'content': 'The modified text', 'content': 'The modified text',
'current_revision': '1', 'current_revision': '1',
......
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