Commit 47247d13 authored by Michal Čihař's avatar Michal Čihař

Fix dictionary import with Django 1.7

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 25b24a32
......@@ -40,6 +40,7 @@ import hashlib
import traceback
import importlib
import __builtin__
from StringIO import StringIO
FILE_FORMATS = {}
......@@ -47,6 +48,16 @@ FLAGS_RE = re.compile(r'\b[-\w]+\b')
LOCATIONS_RE = re.compile(r'^([+-]|.*, [+-]|.*:[+-])')
class StringIOMode(StringIO):
"""
StringIO with mode attribute to make ttkit happy.
"""
def __init__(self, filename, data):
StringIO.__init__(self, data)
self.mode = 'r'
self.name = filename
def register_fileformat(fileformat):
'''
Registers fileformat in dictionary.
......
......@@ -20,7 +20,7 @@
from django.db import models
from weblate.lang.models import Language
from weblate.trans.formats import AutoFormat
from weblate.trans.formats import AutoFormat, StringIOMode
from weblate.trans.models.project import Project
from translate.storage.csvl10n import csvfile
from django.core.urlresolvers import reverse
......@@ -31,8 +31,10 @@ class DictionaryManager(models.Manager):
'''
Handles dictionary update.
'''
filecopy = fileobj.read()
fileobj.close()
# Load file using translate-toolkit
store = AutoFormat.load(fileobj)
store = AutoFormat.load(StringIOMode(fileobj.name, filecopy))
ret, skipped = self.import_store(
request, project, language, store, method
......@@ -40,8 +42,10 @@ class DictionaryManager(models.Manager):
if ret == 0 and skipped > 0 and isinstance(store, csvfile):
# Retry with different CSV scheme
fileobj.seek(0)
store = csvfile(fileobj, ('source', 'target'))
store = csvfile(
StringIOMode(fileobj.name, filecopy),
('source', 'target')
)
ret, skipped = self.import_store(
request, project, language, store, method
)
......
......@@ -105,6 +105,8 @@ class DictionaryTest(ViewTestCase):
# Check correct response
self.assertRedirects(response, self.get_url('show_dictionary'))
response = self.client.get(self.get_url('show_dictionary'))
# Check number of imported objects
self.assertEqual(Dictionary.objects.count(), 164)
......
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