def search_replace(request, project, subproject, lang): translation = get_translation(request, project, subproject, lang) if not can_translate(request.user, translation): raise PermissionDenied() form = ReplaceForm(request.POST) if translation.subproject.locked or not form.is_valid(): messages.error(request, _('Failed to process form!')) return redirect(translation) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = translation.unit_set.filter(target__contains=search_text) updated = matching.count() for unit in matching.iterator(): unit.target = unit.target.replace(search_text, replacement) unit.save_backend(request, change_action=Change.ACTION_REPLACE) import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext('Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated)) return redirect(translation)
def search_replace(request, project, subproject, lang): translation = get_translation(request, project, subproject, lang) if not can_translate(request.user, translation): raise PermissionDenied() form = ReplaceForm(request.POST) if translation.subproject.locked or not form.is_valid(): messages.error(request, _('Failed to process form!')) return redirect(translation) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = translation.unit_set.filter(target__contains=search_text) updated = matching.count() for unit in matching.iterator(): unit.target = unit.target.replace(search_text, replacement) unit.save_backend(request, change_action=Change.ACTION_REPLACE) import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext( 'Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated ) ) return redirect(translation)
def search_replace(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) form = ReplaceForm(request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = unit_set.filter(target__contains=search_text) updated = 0 if matching.exists(): confirm = ReplaceConfirmForm(matching, request.POST) limited = False if matching.count() > 300: matching = matching.order_by('id')[:250] limited = True if not confirm.is_valid(): for unit in matching: unit.replacement = unit.target.replace(search_text, replacement) context.update({ 'matching': matching, 'search_query': search_text, 'replacement': replacement, 'form': form, 'limited': limited, 'confirm': ReplaceConfirmForm(matching), }) return render(request, 'replace.html', context) matching = confirm.cleaned_data['units'] with transaction.atomic(): for unit in matching.select_for_update(): if not request.user.has_perm('unit.edit', unit): continue unit.translate(request, unit.target.replace(search_text, replacement), unit.state, change_action=Change.ACTION_REPLACE) updated += 1 import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext('Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated)) return redirect(obj)
def search_replace(request, project, subproject=None, lang=None): if subproject is None: obj = get_project(request, project) perms = {'project': obj} unit_set = Unit.objects.filter(translation__subproject__project=obj) elif lang is None: obj = get_subproject(request, project, subproject) perms = {'project': obj.project} unit_set = Unit.objects.filter(translation__subproject=obj) else: obj = get_translation(request, project, subproject, lang) perms = {'translation': obj} unit_set = obj.unit_set if not can_translate(request.user, **perms): raise PermissionDenied() form = ReplaceForm(request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) return redirect(obj) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = unit_set.filter(target__contains=search_text) updated = matching.count() for unit in matching.iterator(): unit.target = unit.target.replace(search_text, replacement) unit.save_backend(request, change_action=Change.ACTION_REPLACE) import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext( 'Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated ) ) return redirect(obj)
def search_replace(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) form = ReplaceForm(request.POST) if not form.is_valid(): messages.error(request, _("Failed to process form!")) show_form_errors(request, form) return redirect(obj) search_text = form.cleaned_data["search"] replacement = form.cleaned_data["replacement"] matching = unit_set.filter(target__contains=search_text) updated = 0 if matching.exists(): confirm = ReplaceConfirmForm(matching, request.POST) limited = False if matching.count() > 300: matching = matching.order_by("id")[:250] limited = True if not confirm.is_valid(): for unit in matching: unit.replacement = unit.target.replace(search_text, replacement) context.update({ "matching": matching, "search_query": search_text, "replacement": replacement, "form": form, "limited": limited, "confirm": ReplaceConfirmForm(matching), }) return render(request, "replace.html", context) matching = confirm.cleaned_data["units"] with transaction.atomic(): for unit in matching.select_for_update(**get_nokey_args()): if not request.user.has_perm("unit.edit", unit): continue unit.translate( request.user, unit.target.replace(search_text, replacement), unit.state, change_action=Change.ACTION_REPLACE, ) updated += 1 import_message( request, updated, _("Search and replace completed, no strings were updated."), ngettext( "Search and replace completed, %d string was updated.", "Search and replace completed, %d strings were updated.", updated, ), ) return redirect(obj)
def search_replace(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) form = ReplaceForm(request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = unit_set.filter(target__contains=search_text) updated = 0 if matching.exists(): confirm = ReplaceConfirmForm(matching, request.POST) if not confirm.is_valid(): for unit in matching: unit.replacement = unit.target.replace( search_text, replacement ) context.update({ 'matching': matching, 'search_query': search_text, 'replacement': replacement, 'form': form, 'confirm': ReplaceConfirmForm(matching), }) return render( request, 'replace.html', context ) matching = confirm.cleaned_data['units'] obj.commit_pending(request) with transaction.atomic(): for unit in matching.select_for_update(): if not request.user.has_perm('unit.edit', unit): continue unit.translate( request, unit.target.replace(search_text, replacement), unit.state, change_action=Change.ACTION_REPLACE ) updated += 1 import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext( 'Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated ) ) return redirect(obj)
def search_replace(request, project, subproject=None, lang=None): context = {} if subproject is None: obj = get_project(request, project) perms = {'project': obj} unit_set = Unit.objects.filter(translation__subproject__project=obj) context['project'] = obj elif lang is None: obj = get_subproject(request, project, subproject) perms = {'project': obj.project} unit_set = Unit.objects.filter(translation__subproject=obj) context['subproject'] = obj context['project'] = obj.project else: obj = get_translation(request, project, subproject, lang) perms = {'translation': obj} unit_set = obj.unit_set context['translation'] = obj context['subproject'] = obj.subproject context['project'] = obj.subproject.project if not can_translate(request.user, **perms): raise PermissionDenied() form = ReplaceForm(request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) return redirect(obj) search_text = form.cleaned_data['search'] replacement = form.cleaned_data['replacement'] matching = unit_set.filter(target__contains=search_text) if matching.count() == 0: updated = 0 else: confirm = ReplaceConfirmForm(matching, request.POST) if not confirm.is_valid(): for unit in matching: unit.replacement = unit.target.replace(search_text, replacement) context.update({ 'matching': matching, 'search_query': search_text, 'replacement': replacement, 'form': form, 'confirm': ReplaceConfirmForm(matching), }) return render(request, 'replace.html', context) matching = confirm.cleaned_data['units'] updated = matching.count() for unit in matching.iterator(): if not can_translate(request.user, unit): continue unit.target = unit.target.replace(search_text, replacement) unit.save_backend(request, change_action=Change.ACTION_REPLACE) import_message( request, updated, _('Search and replace completed, no strings were updated.'), ungettext('Search and replace completed, %d string was updated.', 'Search and replace completed, %d strings were updated.', updated)) return redirect(obj)