Commit 140d8a40 authored by Michal Čihař's avatar Michal Čihař

Move file dowloading to separate helper function

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent b9e95a3f
...@@ -56,15 +56,19 @@ class BaseExporter(object): ...@@ -56,15 +56,19 @@ class BaseExporter(object):
translation=None): translation=None):
if translation is not None: if translation is not None:
self.project = translation.subproject.project self.project = translation.subproject.project
self.langauge = obj.language self.language = translation.language
self.url = obj.get_absolute_url() self.url = translation.get_absolute_url()
else: else:
self.project = project self.project = project
self.language = language self.language = language
self.url = url self.url = url
self.storage = self.get_storage() self.storage = self.get_storage()
self.storage.setsourcelanguage(project.source_language.code) self.storage.setsourcelanguage(
self.storage.settargetlanguage(language.code) self.project.source_language.code
)
self.storage.settargetlanguage(
self.language.code
)
def get_storage(self): def get_storage(self):
raise NotImplementedError() raise NotImplementedError()
...@@ -79,7 +83,7 @@ class BaseExporter(object): ...@@ -79,7 +83,7 @@ class BaseExporter(object):
self.storage.addunit(unit) self.storage.addunit(unit)
def add_units(self, translation): def add_units(self, translation):
for unit intranslation .unit_set.iterator(): for unit in translation.unit_set.iterator():
self.add_unit(unit) self.add_unit(unit)
def add_unit(self, unit): def add_unit(self, unit):
......
...@@ -28,10 +28,11 @@ from django.contrib import messages ...@@ -28,10 +28,11 @@ from django.contrib import messages
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from weblate.trans.exporters import get_exporter
from weblate.trans.util import report_error from weblate.trans.util import report_error
from weblate.trans.forms import get_upload_form from weblate.trans.forms import get_upload_form
from weblate.trans.views.helper import get_translation, import_message from weblate.trans.views.helper import (
get_translation, import_message, download_translation_file
)
from weblate.trans.permissions import ( from weblate.trans.permissions import (
can_author_translation, can_overwrite_translation can_author_translation, can_overwrite_translation
) )
...@@ -40,41 +41,13 @@ from weblate.trans.permissions import ( ...@@ -40,41 +41,13 @@ from weblate.trans.permissions import (
def download_translation_format(request, project, subproject, lang, fmt): def download_translation_format(request, project, subproject, lang, fmt):
obj = get_translation(request, project, subproject, lang) obj = get_translation(request, project, subproject, lang)
try: return download_translation_file(obj, fmt)
exporter = get_exporter(fmt)(translation=obj)
except KeyError:
raise Http404('File format not supported')
exporter.add_units(obj)
# Save to response
return exporter.get_response(
'{{project}}-{0}-{{language}}.{{extension}}'.format(
subproject
)
)
def download_translation(request, project, subproject, lang): def download_translation(request, project, subproject, lang):
obj = get_translation(request, project, subproject, lang) obj = get_translation(request, project, subproject, lang)
srcfilename = obj.get_filename() return download_translation_file(obj)
# Construct file name (do not use real filename as it is usually not
# that useful)
filename = '%s-%s-%s.%s' % (project, subproject, lang, obj.store.extension)
# Create response
with open(srcfilename) as handle:
response = HttpResponse(
handle.read(),
content_type=obj.store.mimetype
)
# Fill in response headers
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
def download_language_pack(request, project, subproject, lang): def download_language_pack(request, project, subproject, lang):
......
...@@ -22,10 +22,12 @@ Helper methods for views. ...@@ -22,10 +22,12 @@ Helper methods for views.
''' '''
from django.contrib import messages from django.contrib import messages
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
import django.utils.translation import django.utils.translation
from django.utils.translation import trans_real from django.utils.translation import trans_real
from weblate.trans.exporters import get_exporter
from weblate.trans.models import Project, SubProject, Translation from weblate.trans.models import Project, SubProject, Translation
from weblate.lang.models import Language from weblate.lang.models import Language
...@@ -122,3 +124,40 @@ def import_message(request, count, message_none, message_ok): ...@@ -122,3 +124,40 @@ def import_message(request, count, message_none, message_ok):
messages.warning(request, message_none) messages.warning(request, message_none)
else: else:
messages.success(request, message_ok % count) messages.success(request, message_ok % count)
def download_translation_file(translation, fmt=None):
if fmt is not None:
try:
exporter = get_exporter(fmt)(translation=translation)
except KeyError:
raise Http404('File format not supported')
exporter.add_units(translation)
return exporter.get_response(
'{{project}}-{0}-{{language}}.{{extension}}'.format(
translation.subproject.slug
)
)
srcfilename = translation.get_filename()
# Construct file name (do not use real filename as it is usually not
# that useful)
filename = '{0}-{1}-{2}.{3}'.format(
translation.subproject.project.slug,
translation.subproject.slug,
translation.language.code,
translation.store.extension
)
# Create response
with open(srcfilename) as handle:
response = HttpResponse(
handle.read(),
content_type=translation.store.mimetype
)
# Fill in response headers
response['Content-Disposition'] = 'attachment; filename=%s' % filename
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