def handle_suggestion_comment(request, suggestion, unit, comment, action): kwargs = { 'comment': comment, 'user': request.user, } comment_form = UnsecuredCommentForm(suggestion, kwargs) if comment_form.is_valid(): comment_form.save() if (action not in ("accepted", "rejected") or not settings.POOTLE_EMAIL_FEEDBACK_ENABLED): return ctx = { 'suggestion_id': suggestion.id, 'unit_url': request.build_absolute_uri(unit.get_translate_url()), 'comment': comment, } if action == "rejected": message = loader.render_to_string( 'editor/email/suggestion_rejected_with_comment.txt', ctx) subject = _(u"Suggestion rejected with comment") else: message = loader.render_to_string( 'editor/email/suggestion_accepted_with_comment.txt', ctx) subject = _(u"Suggestion accepted with comment") send_mail(subject, message, from_email=None, recipient_list=[suggestion.user.email], fail_silently=True)
def reject_suggestion(request, unit, suggid): json = { 'udbid': unit.id, 'sugid': suggid, } try: sugg = unit.suggestion_set.get(id=suggid) except ObjectDoesNotExist: raise Http404 # In order to be able to reject a suggestion, users have to either: # 1. Have `review` rights, or # 2. Be the author of the suggestion being rejected if (not check_permission('review', request) and (request.user.is_anonymous() or request.user != sugg.user)): raise PermissionDenied(_('Insufficient rights to access review mode.')) unit.reject_suggestion(sugg, request.translation_project, request.user) r_data = QueryDict(request.body) if "comment" in r_data and r_data["comment"]: kwargs = dict( comment=r_data["comment"], user=request.user, ) comment_form = UnsecuredCommentForm(sugg, kwargs) if comment_form.is_valid(): comment_form.save() json['user_score'] = request.user.public_score return JsonResponse(json)
def reject_suggestion(request, unit, suggid): json = { 'udbid': unit.id, 'sugid': suggid, } try: sugg = unit.suggestion_set.get(id=suggid) except ObjectDoesNotExist: raise Http404 # In order to be able to reject a suggestion, users have to either: # 1. Have `review` rights, or # 2. Be the author of the suggestion being rejected if (not check_permission('review', request) and (request.user.is_anonymous() or request.user != sugg.user)): raise PermissionDenied(_('Insufficient rights to access review mode.')) unit.reject_suggestion(sugg, request.translation_project, request.user) r_data = QueryDict(request.body) if "comment" in r_data and r_data["comment"]: kwargs = dict( comment=r_data["comment"], user=request.user, ) comment_form = UnsecuredCommentForm(sugg, kwargs) if comment_form.is_valid(): comment_form.save() json['user_score'] = request.user.public_score return JsonResponse(json)
def handle_suggestion_comment(request, suggestion, unit, comment, action): kwargs = { 'comment': comment, 'user': request.user, } comment_form = UnsecuredCommentForm(suggestion, kwargs) if comment_form.is_valid(): comment_form.save() if (action not in ("accepted", "rejected") or not settings.POOTLE_EMAIL_FEEDBACK_ENABLED): return ctx = { 'suggestion_id': suggestion.id, 'unit_url': request.build_absolute_uri(unit.get_translate_url()), 'comment': comment, } if action == "rejected": message = loader.render_to_string( 'editor/email/suggestion_rejected_with_comment.txt', ctx) subject = _(u"Suggestion rejected with comment") else: message = loader.render_to_string( 'editor/email/suggestion_accepted_with_comment.txt', ctx) subject = _(u"Suggestion accepted with comment") send_mail(subject, message, from_email=None, recipient_list=[suggestion.user.email], fail_silently=True)
def test_timeline_unit_with_suggestion_and_comment(store0, admin): suggestion = Suggestion.objects.filter(unit__store=store0, state__name="pending", unit__state=UNTRANSLATED).first() unit = suggestion.unit review.get(Suggestion)([suggestion], admin).accept() comment = 'This is a comment!' form = UnsecuredCommentForm(suggestion, admin, dict(comment=comment)) assert form.is_valid() form.save() suggestion.refresh_from_db() unit.refresh_from_db() timeline = Timeline(unit) groups = timeline.grouped_events(start=suggestion.review_time) assert len(groups) == 1 group = groups[0] assert len(group['events']) == 2 assert group['events'][0]['value'] == unit.target assert group['events'][0]['translation'] assert (group['events'][0]['description'] == _get_sugg_accepted_with_comment_desc(suggestion, comment)) submission = Submission.objects.get(field=SubmissionFields.STATE, unit=suggestion.unit, creation_time=suggestion.review_time) assert group['events'][1]['value'] == _get_state_changed_value(submission) assert group['via_upload'] is False assert group['datetime'] == suggestion.review_time assert group['user'].username == admin.username
def accept_suggestion(request, unit, suggid): json = { 'udbid': unit.id, 'sugid': suggid, } try: suggestion = unit.suggestion_set.get(id=suggid) except ObjectDoesNotExist: raise Http404 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() json['user_score'] = request.user.public_score json['newtargets'] = [highlight_whitespace(target) for target in unit.target.strings] json['newdiffs'] = {} for sugg in unit.get_suggestions(): json['newdiffs'][sugg.id] = [highlight_diffs(unit.target.strings[i], target) for i, target in enumerate(sugg.target.strings)] json['checks'] = _get_critical_checks_snippet(request, unit) return JsonResponse(json)
def accept_suggestion(request, unit, suggid): json = { 'udbid': unit.id, 'sugid': suggid, } try: suggestion = unit.suggestion_set.get(id=suggid) except ObjectDoesNotExist: raise Http404 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() json['user_score'] = request.user.public_score json['newtargets'] = [highlight_whitespace(target) for target in unit.target.strings] json['newdiffs'] = {} for sugg in unit.get_suggestions(): json['newdiffs'][sugg.id] = [highlight_diffs(unit.target.strings[i], target) for i, target in enumerate(sugg.target.strings)] json['checks'] = _get_critical_checks_snippet(request, unit) return JsonResponse(json)
def test_unsecured_comment_form_should_save(admin): unit = Unit.objects.first() kwargs = dict( comment="You can say linux though, or gnu/linux if you prefer", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) assert post_form.is_valid()
def test_timeline_unit_with_suggestion_and_comment(store0, admin): suggestion = Suggestion.objects.filter( unit__store=store0, state__name="pending", unit__state=UNTRANSLATED).first() unit = suggestion.unit review.get(Suggestion)([suggestion], admin).accept() comment = 'This is a comment!' form = UnsecuredCommentForm(suggestion, admin, dict( comment=comment)) assert form.is_valid() form.save() suggestion.refresh_from_db() unit.refresh_from_db() timeline = Timeline(unit) groups = timeline.grouped_events(start=suggestion.review_time) assert len(groups) == 1 group = groups[0] assert len(group['events']) == 2 assert group['events'][0]['value'] == unit.target assert group['events'][0]['translation'] assert (group['events'][0]['description'] == _get_sugg_accepted_with_comment_desc(suggestion, comment)) submission = Submission.objects.get(field=SubmissionFields.STATE, unit=suggestion.unit, creation_time=suggestion.review_time) assert group['events'][1]['value'] == _get_state_changed_value(submission) assert group['via_upload'] is False assert group['datetime'] == suggestion.review_time assert group['user'].username == admin.username
def handle_suggestion_comment(request, suggestion, unit, comment, action): kwargs = { 'comment': comment, 'user': request.user, } comment_form = UnsecuredCommentForm(suggestion, kwargs) if comment_form.is_valid(): comment_form.save()
def test_unsecured_comment_form_post(admin): unit = Unit.objects.first() kwargs = dict(comment="Foo!", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) if post_form.is_valid(): post_form.save() comment = Comment.objects.first() assert comment.comment == "Foo!" assert ".".join(comment.content_type.natural_key()) == str(unit._meta) assert comment.object_pk == str(unit.pk) assert comment.user == admin assert comment.name == admin.display_name assert comment.email == admin.email
def test_unsecured_comment_form_post(admin): unit = Unit.objects.first() kwargs = dict( comment="Foo!", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) if post_form.is_valid(): post_form.save() comment = Comment.objects.first() assert comment.comment == "Foo!" assert ".".join(comment.content_type.natural_key()) == str(unit._meta) assert comment.object_pk == str(unit.pk) assert comment.user == admin assert comment.name == admin.display_name assert comment.email == admin.email
def submit(request, unit, **kwargs_): """Processes translation submissions and stores them in the database. :return: An object in JSON notation that contains the previous and last units for the unit next to unit ``uid``. """ json = {} translation_project = request.translation_project language = translation_project.language if unit.hasplural(): snplurals = len(unit.source.strings) else: snplurals = None form_class = unit_form_factory(language, snplurals, request) form = form_class(request.POST, instance=unit, request=request) if form.is_valid(): suggestion = form.cleaned_data['suggestion'] if suggestion: review.get(Suggestion)([suggestion], request.user, SubmissionTypes.NORMAL).accept() if form.cleaned_data['comment']: kwargs = dict( comment=form.cleaned_data['comment'], user=request.user, ) comment_form = UnsecuredCommentForm(suggestion, kwargs) if comment_form.is_valid(): comment_form.save() if form.updated_fields: # Update current unit instance's attributes # important to set these attributes after saving Submission # because we need to access the unit's state before it was saved form.save(changed_with=SubmissionTypes.NORMAL) json['checks'] = _get_critical_checks_snippet(request, unit) json['user_score'] = request.user.public_score json['newtargets'] = [ target for target in form.instance.target.strings ] return JsonResponse(json) return JsonResponseBadRequest({'msg': _("Failed to process submission.")})
def test_timeline_view_unit_with_suggestion_and_comment( client, request_users, system, admin, store0 ): # test with "state change" subission - apparently this is what is required # to get one suggestion = Suggestion.objects.filter( unit__store=store0, state=SuggestionStates.PENDING, unit__state=UNTRANSLATED ).first() unit = suggestion.unit unit.state = FUZZY unit.save() unit.accept_suggestion(suggestion, unit.store.translation_project, admin) form = UnsecuredCommentForm( suggestion, dict(comment="This is a comment!", user=admin,) ) if form.is_valid(): form.save() _timeline_test(client, request_users, unit)
def test_timeline_view_unit_with_suggestion_and_comment( client, request_users, system, admin, store0): # test with "state change" subission - apparently this is what is required # to get one suggestion = Suggestion.objects.filter(unit__store=store0, state__name="pending", unit__state=UNTRANSLATED).first() unit = suggestion.unit unit.state = FUZZY unit.save() review.get(Suggestion)([suggestion], admin).accept() form = UnsecuredCommentForm( suggestion, dict( comment='This is a comment!', user=admin, )) if form.is_valid(): form.save() _timeline_test(client, request_users, unit)
def test_unsecured_comment_form_should_save(admin): unit = Unit.objects.first() BAD_WORDS = ["google", "oracle", "apple", "microsoft"] @getter(comment_should_not_be_saved, sender=Unit) def comment_handler(sender, **kwargs): for bad_word in BAD_WORDS: if bad_word in kwargs["comment"].comment: return "You cant say '%s' round here" % bad_word kwargs = dict( comment="google is foo!", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) assert not post_form.is_valid() assert post_form.errors["comment"] == ["You cant say 'google' round here"] kwargs = dict( comment="You can say linux though, or gnu/linux if you prefer", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) assert post_form.is_valid()
def test_timeline_view_unit_with_suggestion_and_comment(client, request_users, system, admin): # test with "state change" subission - apparently this is what is required # to get one suggestion = Suggestion.objects.filter( state=SuggestionStates.PENDING, unit__state=UNTRANSLATED).first() unit = suggestion.unit unit.state = FUZZY unit.save() unit.accept_suggestion(suggestion, unit.store.translation_project, admin) form = UnsecuredCommentForm(suggestion, dict( comment='This is a comment!', user=admin, )) if form.is_valid(): form.save() _timeline_test( client, request_users, unit)
def test_timeline_view_unit_with_suggestion_and_comment(client, request_users, system, admin, store0): # test with "state change" subission - apparently this is what is required # to get one suggestion = Suggestion.objects.filter( unit__store=store0, state__name="pending", unit__state=UNTRANSLATED).first() unit = suggestion.unit unit.state = FUZZY unit.save() review.get(Suggestion)([suggestion], admin).accept() form = UnsecuredCommentForm(suggestion, dict( comment='This is a comment!', user=admin, )) if form.is_valid(): form.save() _timeline_test( client, request_users, unit)
def test_unsecured_comment_form_should_save(admin): unit = Unit.objects.first() BAD_WORDS = ["google", "oracle", "apple", "microsoft"] @getter(comment_should_not_be_saved, sender=Unit) def comment_handler(sender, **kwargs): for bad_word in BAD_WORDS: if bad_word in kwargs["comment"].comment: return "You cant say '%s' round here" % bad_word kwargs = dict(comment="google is foo!", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) assert not post_form.is_valid() assert post_form.errors["comment"] == ["You cant say 'google' round here"] kwargs = dict( comment="You can say linux though, or gnu/linux if you prefer", user=admin) post_form = UnsecuredCommentForm(unit, kwargs) assert post_form.is_valid()
def add_comments(self, comment): for suggestion in self.suggestions: UnsecuredCommentForm(suggestion, dict(comment=comment, user=self.reviewer)).save()
def submit(request, unit): """Processes translation submissions and stores them in the database. :return: An object in JSON notation that contains the previous and last units for the unit next to unit ``uid``. """ json = {} translation_project = request.translation_project language = translation_project.language old_unit = copy.copy(unit) if unit.hasplural(): snplurals = len(unit.source.strings) else: snplurals = None # Store current time so that it is the same for all submissions current_time = timezone.now() form_class = unit_form_factory(language, snplurals, request) form = form_class(request.POST, instance=unit, request=request) if form.is_valid(): suggestion = form.cleaned_data['suggestion'] if suggestion: old_unit.accept_suggestion(suggestion, request.translation_project, request.user) if form.cleaned_data['comment']: kwargs = dict( comment=form.cleaned_data['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: old_value = str(suggestion.target_f) sub = Submission( creation_time=current_time, translation_project=translation_project, submitter=request.user, unit=unit, store=unit.store, field=field, type=SubmissionTypes.NORMAL, old_value=old_value, new_value=new_value, similarity=form.cleaned_data['similarity'], mt_similarity=form.cleaned_data['mt_similarity'], ) sub.save() # Update current unit instance's attributes # important to set these attributes after saving Submission # because we need to access the unit's state before it was saved if SubmissionFields.TARGET in (f[0] for f in form.updated_fields): form.instance.submitted_by = request.user form.instance.submitted_on = current_time form.instance.reviewed_by = None form.instance.reviewed_on = None form.instance._log_user = request.user form.save() json['checks'] = _get_critical_checks_snippet(request, unit) json['user_score'] = request.user.public_score return JsonResponse(json) return JsonResponseBadRequest({'msg': _("Failed to process submission.")})
def submit(request, unit): """Processes translation submissions and stores them in the database. :return: An object in JSON notation that contains the previous and last units for the unit next to unit ``uid``. """ json = {} translation_project = request.translation_project language = translation_project.language old_unit = copy.copy(unit) if unit.hasplural(): snplurals = len(unit.source.strings) else: snplurals = None # Store current time so that it is the same for all submissions current_time = timezone.now() form_class = unit_form_factory(language, snplurals, request) form = form_class(request.POST, instance=unit, request=request) if form.is_valid(): suggestion = form.cleaned_data['suggestion'] if suggestion: old_unit.accept_suggestion(suggestion, request.translation_project, request.user) if form.cleaned_data['comment']: kwargs = dict( comment=form.cleaned_data['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: old_value = str(suggestion.target_f) sub = Submission( creation_time=current_time, translation_project=translation_project, submitter=request.user, unit=unit, store=unit.store, field=field, type=SubmissionTypes.NORMAL, old_value=old_value, new_value=new_value, similarity=form.cleaned_data['similarity'], mt_similarity=form.cleaned_data['mt_similarity'], ) sub.save() # Update current unit instance's attributes # important to set these attributes after saving Submission # because we need to access the unit's state before it was saved if SubmissionFields.TARGET in (f[0] for f in form.updated_fields): form.instance.submitted_by = request.user form.instance.submitted_on = current_time form.instance.reviewed_by = None form.instance.reviewed_on = None form.instance._log_user = request.user form.save() json['checks'] = _get_critical_checks_snippet(request, unit) json['user_score'] = request.user.public_score return JsonResponse(json) return JsonResponseBadRequest({'msg': _("Failed to process submission.")})