Commit 700d2ac4 authored by Michal Čihař's avatar Michal Čihař

Merge pull request #984 from matejcik/issue900

reject uploaded file with mismatched plural form
parents cf9e4b07 ba22db82
......@@ -1200,6 +1200,13 @@ class Translation(models.Model, URLMixin, PercentMixin, LoggerMixin):
if author is None:
author = get_author_name(request.user)
# Check valid plural forms
if hasattr(store.store, 'parseheader'):
header = store.store.parseheader()
if 'Plural-Forms' in header and \
self.language.get_plural_form() != header['Plural-Forms']:
raise Exception('Plural forms do not match the language.')
# List translations we should process
# Filter out those who don't want automatic update, but keep ourselves
translations = Translation.objects.filter(
......
# Czech translations for PACKAGE package.
# Copyright (C) 2012 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2012.
# Testing Weblate, 2015.
#
msgid ""
msgstr ""
"Project-Id-Version: Weblate Hello World 2012\n"
"Report-Msgid-Bugs-To: <noreply@example.net>\n"
"POT-Creation-Date: 2012-03-14 15:54+0100\n"
"PO-Revision-Date: 2012-03-05 15:55+0100\n"
"Last-Translator: Automatically generated\n"
"Language-Team: Test Team <noreply@weblate.org>\n"
"Language: cs\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=4; plural=(n==1) ? 0 : (n>=2 && n<=4) ? 1 : (n % 10 == 1) ? 3 : 2;\n"
#: main.c:11
#, c-format
msgid "Hello, world!\n"
msgstr "Ahoj světe!\n"
#: main.c:12
#, c-format
msgid "Orangutan has %d banana.\n"
msgid_plural "Orangutan has %d bananas.\n"
msgstr[0] ""
msgstr[1] ""
msgstr[2] ""
msgstr[3] ""
#: main.c:13
#, c-format
msgid "Try Weblate at <http://demo.weblate.org/>!\n"
msgstr ""
#: main.c:14
msgid "Thank you for using Weblate."
msgstr ""
......@@ -33,6 +33,7 @@ TEST_PO = get_test_file('cs.po')
TEST_CSV = get_test_file('cs.csv')
TEST_PO_BOM = get_test_file('cs-bom.po')
TEST_FUZZY_PO = get_test_file('cs-fuzzy.po')
TEST_BADPLURALS = get_test_file('cs-badplurals.po')
TEST_MO = get_test_file('cs.mo')
TEST_ANDROID = get_test_file('strings-cs.xml')
......@@ -52,11 +53,14 @@ class ImportBaseTest(ViewTestCase):
self.user.is_superuser = True
self.user.save()
def do_import(self, **kwargs):
def do_import(self, test_file=None, follow=False, **kwargs):
'''
Helper to perform file import.
'''
with open(self.test_file) as handle:
if test_file is None:
test_file = self.test_file
with open(test_file) as handle:
params = {'file': handle}
params.update(kwargs)
return self.client.post(
......@@ -64,7 +68,8 @@ class ImportBaseTest(ViewTestCase):
'upload_translation',
kwargs=self.kw_translation
),
params
params,
follow=follow
)
......@@ -209,6 +214,24 @@ class ImportTest(ImportBaseTest):
)
class ImportErrorTest(ImportBaseTest):
'''
Testing import of broken files.
'''
def test_mismatched_plurals(self):
'''
Test importing a file with different number of plural forms.
In response to issue #900
'''
from django.contrib.messages import ERROR
response = self.do_import(test_file=TEST_BADPLURALS, follow=True)
self.assertRedirects(response, self.translation_url)
messages = list(response.context["messages"])
self.assertEquals(len(messages), 1)
self.assertEquals(messages[0].level, ERROR)
self.assertIn("Plural forms do not match", messages[0].message)
class BOMImportTest(ImportTest):
test_file = TEST_PO_BOM
......
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