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