in pontoon/translations/views.py [0:0]
def create_translation(request):
"""
Create a new translation.
"""
form = forms.CreateTranslationForm(request.POST)
if not form.is_valid():
problems = []
for field, errors in form.errors.items():
problems.append(
'Error validating field `{}`: "{}"'.format(field, " ".join(errors))
)
return JsonResponse(
{"status": False, "message": "\n".join(problems)}, status=400
)
entity = form.cleaned_data["entity"]
string = form.cleaned_data["translation"]
locale = form.cleaned_data["locale"]
plural_form = form.cleaned_data["plural_form"]
original = form.cleaned_data["original"]
ignore_warnings = form.cleaned_data["ignore_warnings"]
approve = form.cleaned_data["approve"]
force_suggestions = form.cleaned_data["force_suggestions"]
paths = form.cleaned_data["paths"]
machinery_sources = form.cleaned_data["machinery_sources"]
project = entity.resource.project
# Read-only translations cannot saved
if utils.readonly_exists(project, locale):
return JsonResponse(
{
"status": False,
"message": "Forbidden: This string is in read-only mode.",
},
status=403,
)
translations = Translation.objects.filter(
entity=entity,
locale=locale,
plural_form=plural_form,
)
same_translations = translations.filter(string=string)
# If same translation exists in the DB, don't save it again.
if same_translations:
return JsonResponse({"status": False, "same": True})
# Look for failed checks.
# Checks are disabled for the tutorial.
use_checks = project.slug != "tutorial"
user = request.user
failed_checks = None
if use_checks:
failed_checks = run_checks(
entity,
locale.code,
original,
string,
user.profile.quality_checks,
)
if are_blocking_checks(failed_checks, ignore_warnings):
return JsonResponse({"status": False, "failedChecks": failed_checks})
now = timezone.now()
can_translate = user.can_translate(project=project, locale=locale) and (
not force_suggestions or approve
)
translation = Translation(
entity=entity,
locale=locale,
plural_form=plural_form,
string=string,
user=user,
date=now,
approved=can_translate,
machinery_sources=machinery_sources,
)
if can_translate:
translation.approved_user = user
translation.approved_date = now
translation.save(failed_checks=failed_checks)
log_action(ActionLog.ActionType.TRANSLATION_CREATED, user, translation=translation)
if translations:
translation = entity.reset_active_translation(
locale=locale,
plural_form=plural_form,
)
# When user makes their first contribution to the team, notify team managers
first_contribution = (
not project.system_project
and user != project.contact
and user.has_one_contribution(locale)
)
if first_contribution:
description = render_to_string(
"messaging/notifications/new_contributor.html",
{
"entity": entity,
"locale": locale,
"project": project,
"user": user,
},
)
for manager in locale.managers_group.user_set.filter(
profile__new_contributor_notifications=True
):
notify.send(
sender=manager,
recipient=manager,
verb="has reviewed suggestions", # Triggers render of description only
description=description,
category="new_contributor",
)
response_data = {
"status": True,
"translation": translation.serialize(),
"stats": TranslatedResource.objects.query_stats(project, paths, locale),
}
# Send Translation Champion Badge notification information
translation_count = user.badges_translation_count
if translation_count in settings.BADGES_TRANSLATION_THRESHOLDS:
badge_name = "Translation Champion"
badge_level = (
settings.BADGES_TRANSLATION_THRESHOLDS.index(translation_count) + 1
)
_add_badge_data(response_data, user, badge_name, badge_level)
return JsonResponse(response_data)