Created
March 25, 2018 13:37
-
-
Save jotes/3b7e236db59cf205e6ba23d3aa98f712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pontoon/base/utils.py b/pontoon/base/utils.py | |
index 3774ea4c..1efa0a21 100644 | |
--- a/pontoon/base/utils.py | |
+++ b/pontoon/base/utils.py | |
@@ -23,9 +23,7 @@ from xml.sax.saxutils import ( | |
) | |
from django.db.models import Prefetch | |
-from django.http import ( | |
- HttpResponseBadRequest, | |
-) | |
+from django.http import HttpResponseBadRequest | |
from django.shortcuts import get_object_or_404 | |
from django.utils import timezone | |
from django.utils.translation import trans_real | |
@@ -645,3 +643,21 @@ def get_m2m_changes(current_qs, new_qs): | |
) | |
return list(add_items), list(remove_items) | |
+ | |
+ | |
+def translation_is_duplicate(same_translations, can_translate): | |
+ """ | |
+ Check | |
+ :return: | |
+ """ | |
+ # Same translation | |
+ st = same_translations[0] | |
+ | |
+ if can_translate and st.approved and not st.fuzzy: | |
+ return True | |
+ | |
+ elif not st.fuzzy: | |
+ return True | |
+ | |
+ return False | |
+ | |
diff --git a/pontoon/base/views.py b/pontoon/base/views.py | |
index 102c3e25..2142a44f 100755 | |
--- a/pontoon/base/views.py | |
+++ b/pontoon/base/views.py | |
@@ -28,6 +28,7 @@ from django.views.decorators.http import ( | |
) | |
from django.views.generic.edit import FormView | |
+from base.utils import translation_is_duplicate | |
from pontoon.base import forms | |
from pontoon.base import utils | |
from pontoon.base.models import ( | |
@@ -42,7 +43,7 @@ from pontoon.base.models import ( | |
UserProfile, | |
) | |
-from pontoon.checks.utils import get_quality_checks | |
+from pontoon.checks.utils import run_checks | |
log = logging.getLogger(__name__) | |
@@ -545,33 +546,32 @@ def update_translation(request): | |
translations = Translation.objects.filter( | |
entity=e, locale=locale, plural_form=plural_form) | |
- # Newlines are not allowed in .lang files (bug 1190754) | |
- if e.resource.format == 'lang' and '\n' in string: | |
- return HttpResponse('Newline characters are not allowed.') | |
+ # Same translation exists | |
+ same_translations = translations.filter(string=string).order_by( | |
+ '-approved', 'rejected', '-date' | |
+ ) | |
+ | |
+ | |
+ checks = run_checks( | |
+ e, | |
+ locale, | |
+ plural_form, | |
+ string, | |
+ ignore, | |
+ translation_is_duplicate(same_translations, can_translate) | |
+ ) | |
- checks = get_quality_checks(e, locale, plural_form, string, ignore) | |
if checks: | |
return checks | |
# Translations exist | |
if len(translations) > 0: | |
- # Same translation exists | |
- same_translations = translations.filter(string=string).order_by( | |
- '-approved', 'rejected', '-date' | |
- ) | |
if len(same_translations) > 0: | |
t = same_translations[0] | |
# If added by privileged user, approve and unfuzzy it | |
if can_translate: | |
- | |
- # Unless there's nothing to be changed | |
- if t.approved and not t.fuzzy: | |
- return JsonResponse({ | |
- 'same': True, | |
- 'message': 'Same translation already exists.', | |
- }) | |
- | |
+ # Check if there are errors that should be fixed | |
translations.update( | |
approved=False, | |
approved_user=None, | |
@@ -601,24 +601,18 @@ def update_translation(request): | |
}) | |
# If added by non-privileged user, unfuzzy it | |
- else: | |
- if t.fuzzy: | |
- t.approved = False | |
- t.approved_user = None | |
- t.approved_date = None | |
- t.fuzzy = False | |
- | |
- t.save() | |
+ elif t.fuzzy: | |
+ t.approved = False | |
+ t.approved_user = None | |
+ t.approved_date = None | |
+ t.fuzzy = False | |
- return JsonResponse({ | |
- 'type': 'updated', | |
- 'translation': t.serialize(), | |
- 'stats': TranslatedResource.objects.stats(project, paths, locale), | |
- }) | |
+ t.save() | |
return JsonResponse({ | |
- 'same': True, | |
- 'message': 'Same translation already exists.', | |
+ 'type': 'updated', | |
+ 'translation': t.serialize(), | |
+ 'stats': TranslatedResource.objects.stats(project, paths, locale), | |
}) | |
# Different translation added | |
diff --git a/pontoon/checks/utils/__init__.py b/pontoon/checks/utils/__init__.py | |
index 6afefe5e..de9c4ca8 100644 | |
--- a/pontoon/checks/utils/__init__.py | |
+++ b/pontoon/checks/utils/__init__.py | |
@@ -4,7 +4,7 @@ import compare | |
import translate_toolkit | |
-def get_quality_checks(entity, locale, plural_form, string, ignore_warnings): | |
+def run_checks(entity, locale, plural_form, string, ignore_warnings, same): | |
""" | |
Main function that performs all quality checks from frameworks handled in Pontoon. | |
@@ -13,6 +13,8 @@ def get_quality_checks(entity, locale, plural_form, string, ignore_warnings): | |
:arg basestring string: a translation | |
:arg int plural_form: plural form of a translation | |
:arg bool ignore_warnings: omit warnings | |
+ :arg bool same: omit warnings | |
+ | |
:return: Return types: | |
* JsonResponse - If there are errors or | |
* None - If there's no errors and non-omitted warnings. | |
@@ -42,7 +44,19 @@ def get_quality_checks(entity, locale, plural_form, string, ignore_warnings): | |
**(cl_checks or {}) | |
) | |
- if (not ignore_warnings and checks) or ('clErrors' in checks): | |
+ | |
+ # Newlines are not allowed in .lang files (bug 1190754) | |
+ if entity.resource.format == 'lang' and '\n' in string: | |
+ checks.setdefault('pErrors', []).append( | |
+ 'Newline characters are not allowed.' | |
+ ) | |
+ | |
+ if same: | |
+ checks.setdefault('pErrors', []).append( | |
+ 'Same translation already exists.' | |
+ ) | |
+ | |
+ if (not ignore_warnings and checks) or ('clErrors' in checks) or ('pErrors' in checks): | |
return JsonResponse({ | |
'failedChecks': checks | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment