From c093e7c6eab11add2028bc33219490b41ca9f2ad Mon Sep 17 00:00:00 2001 From: Taras Semenenko Date: Tue, 19 Apr 2016 11:51:05 +0300 Subject: [PATCH] Add backend support for suggestion alterations via submit Accepting a suggestion with alteration can be handled as: - accepting the suggestion without alterations; - editing accepted suggestion and submit new translation. Refs. #4629 --- pootle/apps/pootle_store/forms.py | 1 + pootle/apps/pootle_store/views.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pootle/apps/pootle_store/forms.py b/pootle/apps/pootle_store/forms.py index 09096c87f7d..489f3be026a 100644 --- a/pootle/apps/pootle_store/forms.py +++ b/pootle/apps/pootle_store/forms.py @@ -259,6 +259,7 @@ class Meta(object): ) similarity = forms.FloatField(required=False) mt_similarity = forms.FloatField(required=False) + suggestion_id = forms.IntegerField(required=False) def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) diff --git a/pootle/apps/pootle_store/views.py b/pootle/apps/pootle_store/views.py index 661540a282b..cce077d6f64 100644 --- a/pootle/apps/pootle_store/views.py +++ b/pootle/apps/pootle_store/views.py @@ -6,6 +6,8 @@ # or later license. See the LICENSE file for a copy of the license and the # AUTHORS file for copyright and authorship information. +import copy + from translate.lang import data from django import forms @@ -506,6 +508,7 @@ def submit(request, unit): translation_project = request.translation_project language = translation_project.language + old_unit = copy.copy(unit) if unit.hasplural(): snplurals = len(unit.source.strings) @@ -519,8 +522,29 @@ def submit(request, unit): form = form_class(request.POST, instance=unit, request=request) if form.is_valid(): + suggestion = None + suggid = form.cleaned_data['suggestion_id'] + if suggid is not None: + try: + suggestion = unit.suggestion_set.get(id=suggid) + except ObjectDoesNotExist: + raise Http404 + + old_unit.accept_suggestion(suggestion, + request.translation_project, request.user) + if "comment" in request.POST and request.POST["comment"]: + kwargs = dict( + comment=request.POST["comment"], + user=request.user, + ) + comment_form = UnsecuredCommentForm(suggestion, kwargs) + if comment_form.is_valid(): + comment_form.save() + if form.updated_fields: for field, old_value, new_value in form.updated_fields: + if field == SubmissionFields.TARGET and suggestion is not None: + old_value = suggestion.target_f sub = Submission( creation_time=current_time, translation_project=translation_project,