Commit 40e845b9 authored by Michal Čihař's avatar Michal Čihař

Fix dictionary downloads with ttkit 1.14.0

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 5b127214
......@@ -417,6 +417,16 @@ class FileFormat(object):
new_translation = None
autoload = ()
@staticmethod
def serialize(store):
"""Serialize given ttkit store"""
if hasattr(store, 'serialize'):
# ttkit API since 1.14.0
return bytes(store)
else:
# ttkit API 1.13.0 and older
return str(store)
@classmethod
def parse(cls, storefile, template_store=None, language_code=None):
"""Parses store and returns FileFormat instance."""
......@@ -676,13 +686,7 @@ class FileFormat(object):
return False
if cls.monolingual is False:
if hasattr(store, 'serialize'):
# ttkit API since 1.14.0
storebytes = bytes(store)
else:
# ttkit API 1.13.0 and older
storebytes = str(store)
if storebytes == b'':
if cls.serialize(store) == b'':
return False
return True
......@@ -832,7 +836,7 @@ class PoFormat(FileFormat):
mounit.msgctxt = [context]
mounit.target = unit.target
outputfile.addunit(mounit)
return str(outputfile)
return self.serialize(outputfile)
def get_language_pack_meta(self):
'''
......@@ -1248,7 +1252,7 @@ class CSVFormat(FileFormat):
if store.fieldnames != ['location', 'source', 'target']:
return store
if not isinstance(content, six.string_types):
if not isinstance(content, six.string_types) and six.PY3:
content = content.decode('utf-8')
fileobj = csv.StringIO(content)
......@@ -1264,7 +1268,10 @@ class CSVFormat(FileFormat):
return store
result = storeclass(fieldnames=['source', 'target'])
if six.PY3:
result.parse(content.encode('utf-8'))
else:
result.parse(content)
return result
......
......@@ -48,7 +48,7 @@ class DictionaryTest(ViewTestCase):
})
def import_file(self, filename, **kwargs):
with open(filename) as handle:
with open(filename, 'rb') as handle:
params = {'file': handle}
params.update(kwargs)
return self.client.post(
......
......@@ -30,10 +30,12 @@ from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core.urlresolvers import reverse
import six
from six.moves.urllib.parse import urlencode
from weblate.trans.models import Translation, Dictionary, Change
from weblate.lang.models import Language
from weblate.trans.formats import FileFormat
from weblate.trans.site import get_site_url
from weblate.trans.util import report_error
from weblate.trans.forms import WordForm, DictUploadForm, LetterForm
......@@ -164,6 +166,7 @@ def upload_dictionary(request, project, lang):
)
)
except Exception as error:
raise
report_error(error, sys.exc_info(), request)
messages.error(
request, _('File upload has failed: %s') % force_text(error)
......@@ -231,7 +234,7 @@ def download_dictionary_ttkit(export_format, prj, lang, words):
store.addunit(unit)
# Save to response
response.write(str(store))
response.write(FileFormat.serialize(store))
return response
......@@ -271,9 +274,14 @@ def download_dictionary(request, project, lang):
writer.writerow(('source', 'target'))
for word in words.iterator():
if six.PY2:
writer.writerow((
word.source.encode('utf8'), word.target.encode('utf8')
))
else:
writer.writerow((
word.source, word.target
))
return response
......
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