Commit bffa0477 authored by Claude Paroz's avatar Claude Paroz
Browse files

Do not offer translated file for download if it does not exist

parent 65118e59
......@@ -189,7 +189,11 @@ class Translation(models.Model):
def translated(self):
""" Returns a path relative to media_root """
base, ext = os.path.splitext(unicode(self.doc.original))
return "%s.%s%s" % (base, self.lang.code, ext)
path = "%s.%s%s" % (base, self.lang.code, ext)
if os.path.exists(os.path.join(settings.MEDIA_ROOT, path)):
return path
else:
return None
def set_upfile(self, uploaded):
self.up_file = uploaded
......@@ -233,11 +237,12 @@ class Translation(models.Model):
'mo': mo_path,
}, raise_on_error=True)
base, ext = os.path.splitext(unicode(self.doc.original))
final_path = os.path.join(settings.MEDIA_ROOT, "%s.%s%s" % (base, self.lang.code, ext))
command = "%(path)sitstool -m %(mo)s -I %(img_dir)s -o %(final)s %(current)s" % {
'path': settings.ITSTOOL_PATH,
'mo': mo_path,
'img_dir': os.path.join(settings.MEDIA_ROOT, self.doc.series.uuid, 'img'),
'final': os.path.join(settings.MEDIA_ROOT, "%s.%s%s" % (base, self.lang.code, ext)),
'final': final_path,
'current': os.path.join(settings.MEDIA_ROOT, unicode(self.doc.original)),
}
try:
......@@ -245,6 +250,7 @@ class Translation(models.Model):
except OSError, e:
self.errors = str(e)
self.save()
os.remove(final_path)
return
if self.errors:
self.errors = ""
......
......@@ -39,12 +39,14 @@ def doc_translation(doc, language):
def translated_doc(trans):
if trans is None:
return "-"
else:
return """<a href="%(media)s%(path)s">%(fname)s</a>""" % {
'media': settings.MEDIA_URL,
'path': trans.translated(),
'fname': os.path.basename(trans.translated()),
}
trans_file_path = trans.translated()
if trans_file_path is None:
return "-"
return """<a href="%(media)s%(path)s">%(fname)s</a>""" % {
'media': settings.MEDIA_URL,
'path': trans_file_path,
'fname': os.path.basename(trans_file_path),
}
@register.filter
def as_thumbnail(img):
......
......@@ -31,7 +31,8 @@ class TranslatorTest(TestCase):
self.user = User.objects.create_user('john', 'doe@example.org', 'johnpw')
self.old_media_root = settings.MEDIA_ROOT
settings.MEDIA_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "media")
os.mkdir(settings.MEDIA_ROOT)
if not os.path.exists(settings.MEDIA_ROOT):
os.mkdir(settings.MEDIA_ROOT)
models.overwrite_fs.location = os.path.abspath(settings.MEDIA_ROOT)
self.lang = models.Language.objects.create(code='fr', name='French')
......@@ -121,6 +122,7 @@ class TranslatorTest(TestCase):
response = self.client.post(reverse('translation_upload', args=[self.series.uuid, self.doc.pk, 'fr']), post_data)
trans_fr = self.doc.translation(self.lang)
self.assertTrue(len(trans_fr.errors) > 10)
self.assertEqual(trans_fr.translated(), None)
def test_anonymous_access(self):
self._create_series()
......
Supports Markdown
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