def test_save_failed_checks(translation_a): save_failed_checks( translation_a, { 'clErrors': [ 'compare-locales error 1', 'compare-locales error 2', ], 'clWarnings': [ 'compare-locales warning 1', ], # Warnings from Translate Toolkit can't be stored in the Database 'ttWarnings': [ 'translate-toolkit warning 1', 'translate-toolkit warning 2', ] }) error1, error2 = Error.objects.order_by('message') assert error1.library == 'cl' assert error1.message == 'compare-locales error 1' assert error2.library == 'cl' assert error2.message == 'compare-locales error 2' cl_warning, = (Warning.objects.order_by('library', 'message')) assert cl_warning.library == 'cl' assert cl_warning.message == 'compare-locales warning 1'
def test_save_failed_checks(translation_a): save_failed_checks( translation_a, { "clErrors": ["compare-locales error 1", "compare-locales error 2"], "clWarnings": ["compare-locales warning 1"], # Warnings from Translate Toolkit can't be stored in the Database "ttWarnings": [ "translate-toolkit warning 1", "translate-toolkit warning 2", ], }, ) error1, error2 = Error.objects.order_by("message") assert error1.library == "cl" assert error1.message == "compare-locales error 1" assert error2.library == "cl" assert error2.message == "compare-locales error 2" (cl_warning, ) = Warning.objects.order_by("library", "message") assert cl_warning.library == "cl" assert cl_warning.message == "compare-locales warning 1"
def test_save_no_checks(translation0): save_failed_checks( translation0, {} ) assert not Warning.objects.all().exists() assert not Error.objects.all().exists()
def test_save_failed_checks(translation0): save_failed_checks(translation0, { 'clErrors': [ 'compare-locales error 1', 'compare-locales error 2', ], 'clWarnings': [ 'compare-locales warning 1', ], 'ttWarnings': [ 'translate-toolkit warning 1', 'translate-toolkit warning 2', ] }) error1, error2 = Error.objects.order_by('message') assert error1.library == 'cl' assert error1.message == 'compare-locales error 1' assert error2.library == 'cl' assert error2.message == 'compare-locales error 2' cl_warning, tt_warning1, tt_warning2 = Warning.objects.order_by('library', 'message') assert cl_warning.library == 'cl' assert cl_warning.message == 'compare-locales warning 1' assert tt_warning1.library == 'tt' assert tt_warning1.message == 'translate-toolkit warning 1' assert tt_warning2.library == 'tt' assert tt_warning2.message == 'translate-toolkit warning 2'
def test_save_failed_checks(translation_a): save_failed_checks(translation_a, { 'clErrors': [ 'compare-locales error 1', 'compare-locales error 2', ], 'clWarnings': [ 'compare-locales warning 1', ], # Warnings from Translate Toolkit can't be stored in the Database 'ttWarnings': [ 'translate-toolkit warning 1', 'translate-toolkit warning 2', ] }) error1, error2 = Error.objects.order_by('message') assert error1.library == 'cl' assert error1.message == 'compare-locales error 1' assert error2.library == 'cl' assert error2.message == 'compare-locales error 2' cl_warning, = ( Warning.objects.order_by('library', 'message') ) assert cl_warning.library == 'cl' assert cl_warning.message == 'compare-locales warning 1'
def update_translation(request): """Update entity translation for the specified locale and user.""" try: entity = request.POST['entity'] string = request.POST['translation'] locale = request.POST['locale'] plural_form = request.POST['plural_form'] original = request.POST['original'] ignore_warnings = request.POST.get('ignore_warnings', 'false') == 'true' approve = request.POST.get('approve', 'false') == 'true' force_suggestions = request.POST.get('force_suggestions', 'false') == 'true' paths = request.POST.getlist('paths[]') except MultiValueDictKeyError as e: return HttpResponseBadRequest('Bad Request: {error}'.format(error=e)) try: e = Entity.objects.get(pk=entity) except Entity.DoesNotExist as error: log.error(str(error)) return HttpResponse("error") try: locale = Locale.objects.get(code=locale) except Locale.DoesNotExist as error: log.error(str(error)) return HttpResponse("error") if plural_form == "-1": plural_form = None user = request.user project = e.resource.project # Read-only translations cannot saved if utils.readonly_exists(project, locale): return HttpResponseForbidden( "Forbidden: This string is in read-only mode" ) try: use_ttk_checks = UserProfile.objects.get(user=user).quality_checks except UserProfile.DoesNotExist as error: use_ttk_checks = True # Disable checks for tutorial project. if project.slug == 'tutorial': use_ttk_checks = False now = timezone.now() can_translate = ( request.user.can_translate(project=project, locale=locale) and (not force_suggestions or approve) ) translations = Translation.objects.filter( entity=e, locale=locale, plural_form=plural_form) same_translations = translations.filter(string=string).order_by( '-approved', 'rejected', '-date' ) # If same translation exists in the DB, don't save it again. if utils.is_same(same_translations, can_translate): return JsonResponse({ 'same': True, }) failed_checks = run_checks( e, locale.code, original, string, use_ttk_checks, ) if are_blocking_checks(failed_checks, ignore_warnings): return JsonResponse({ 'failedChecks': failed_checks, }) # Translations exist if len(translations) > 0: if len(same_translations) > 0: t = same_translations[0] # If added by privileged user, approve and unfuzzy it if can_translate and (t.fuzzy or not t.approved): translations.update( approved=False, approved_user=None, approved_date=None, rejected=True, rejected_user=request.user, rejected_date=timezone.now(), fuzzy=False, ) t.approved = True t.fuzzy = False t.rejected = False t.rejected_user = None t.rejected_date = None if t.approved_user is None: t.approved_user = user t.approved_date = now # If added by non-privileged user and fuzzy, unfuzzy it elif t.fuzzy: t.approved = False t.approved_user = None t.approved_date = None t.fuzzy = False t.save() t.warnings.all().delete() t.errors.all().delete() save_failed_checks(t, failed_checks) return JsonResponse({ 'type': 'updated', 'translation': t.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), }) # Different translation added else: if can_translate: translations.update(approved=False, approved_user=None, approved_date=None) translations.update(fuzzy=False) t = Translation( entity=e, locale=locale, user=user, string=string, plural_form=plural_form, date=now, approved=can_translate) if can_translate: t.approved_user = user t.approved_date = now t.save() save_failed_checks(t, failed_checks) # Return active (approved or latest) translation try: active = translations.filter(approved=True).latest("date") except Translation.DoesNotExist: active = translations.latest("date") return JsonResponse({ 'type': 'added', 'translation': active.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), }) # No translations saved yet else: t = Translation( entity=e, locale=locale, user=user, string=string, plural_form=plural_form, date=now, approved=can_translate) if can_translate: t.approved_user = user t.approved_date = now t.save() save_failed_checks(t, failed_checks) return JsonResponse({ 'type': 'saved', 'translation': t.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), })
def update_translation(request): """Update entity translation for the specified locale and user. Note that this view is also used to approve a translation by the old Translate app. Once we migrate to Translate.Next, we'll want to rework this view to remove the bits about approving a translation, because that has been delegated to the `approve_translation` view. """ try: entity = request.POST['entity'] string = request.POST['translation'] locale = request.POST['locale'] plural_form = request.POST['plural_form'] original = request.POST['original'] ignore_warnings = request.POST.get('ignore_warnings', 'false') == 'true' approve = request.POST.get('approve', 'false') == 'true' force_suggestions = request.POST.get('force_suggestions', 'false') == 'true' paths = request.POST.getlist('paths[]') except MultiValueDictKeyError as e: return HttpResponseBadRequest('Bad Request: {error}'.format(error=e)) try: e = Entity.objects.get(pk=entity) except Entity.DoesNotExist as error: log.error(str(error)) return HttpResponse("error") try: locale = Locale.objects.get(code=locale) except Locale.DoesNotExist as error: log.error(str(error)) return HttpResponse("error") if plural_form == "-1": plural_form = None user = request.user project = e.resource.project # Read-only translations cannot saved if utils.readonly_exists(project, locale): return HttpResponseForbidden( "Forbidden: This string is in read-only mode" ) try: use_ttk_checks = UserProfile.objects.get(user=user).quality_checks except UserProfile.DoesNotExist as error: use_ttk_checks = True # Disable checks for tutorial project. if project.slug == 'tutorial': use_ttk_checks = False now = timezone.now() can_translate = ( request.user.can_translate(project=project, locale=locale) and (not force_suggestions or approve) ) translations = Translation.objects.filter( entity=e, locale=locale, plural_form=plural_form) same_translations = translations.filter(string=string).order_by( '-active', 'rejected', '-date' ) # If same translation exists in the DB, don't save it again. if utils.is_same(same_translations, can_translate): return JsonResponse({ 'same': True, }) failed_checks = run_checks( e, locale.code, original, string, use_ttk_checks, ) if are_blocking_checks(failed_checks, ignore_warnings): return JsonResponse({ 'failedChecks': failed_checks, }) # Translations exist if len(translations) > 0: # Same translation exists if len(same_translations) > 0: t = same_translations[0] # If added by privileged user, approve and unfuzzy it if can_translate and (t.fuzzy or not t.approved): if not t.active: translations.filter(active=True).update(active=False) t.active = True t.approved = True t.fuzzy = False t.rejected = False t.rejected_user = None t.rejected_date = None if t.approved_user is None: t.approved_user = user t.approved_date = now t.save() t.warnings.all().delete() t.errors.all().delete() save_failed_checks(t, failed_checks) return JsonResponse({ 'type': 'updated', 'translation': t.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), }) # Different translation added else: t = Translation( entity=e, locale=locale, plural_form=plural_form, string=string, user=user, date=now, approved=can_translate, ) if can_translate: t.approved_user = user t.approved_date = now t.save() save_failed_checks(t, failed_checks) active_translation = e.reset_active_translation( locale=locale, plural_form=plural_form, ) return JsonResponse({ 'type': 'added', 'translation': active_translation.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), }) # No translations saved yet else: t = Translation( entity=e, locale=locale, plural_form=plural_form, string=string, user=user, date=now, active=True, approved=can_translate, ) if can_translate: t.approved_user = user t.approved_date = now t.save() save_failed_checks(t, failed_checks) return JsonResponse({ 'type': 'saved', 'translation': t.serialize(), 'stats': TranslatedResource.objects.stats(project, paths, locale), })
def test_save_no_checks(translation_a): save_failed_checks(translation_a, {}) assert not Warning.objects.all().exists() assert not Error.objects.all().exists()