def upload_dictionary(request, project, lang): prj = get_project(request, project) if not request.user.has_perm('glossary.upload', prj): raise PermissionDenied() lang = get_object_or_404(Language, code=lang) form = DictUploadForm(request.POST, request.FILES) if form.is_valid(): try: count = Dictionary.objects.upload( request, prj, lang, request.FILES['file'], form.cleaned_data['method'] ) import_message( request, count, _('No words to import found in file.'), ungettext( 'Imported %d word from the uploaded file.', 'Imported %d words from the uploaded file.', count, ), ) except Exception as error: report_error(error, request, prefix='Failed to handle upload') messages.error(request, _('File upload has failed: %s') % force_text(error)) else: messages.error(request, _('Failed to process form!')) return redirect('show_dictionary', project=prj.slug, lang=lang.code)
def auto_translation(request, project, component, lang): translation = get_translation(request, project, component, lang) project = translation.component.project if not request.user.has_perm('translation.auto', project): raise PermissionDenied() autoform = AutoForm(request.user, translation, request.POST) if translation.component.locked or not autoform.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, autoform) return redirect(translation) auto = AutoTranslate(request.user, translation, autoform.cleaned_data['type'], request=request) if autoform.cleaned_data['auto_source'] == 'mt': auto.process_mt( autoform.cleaned_data['engines'], autoform.cleaned_data['threshold'], ) else: auto.process_others(autoform.cleaned_data['component'], ) import_message( request, auto.updated, _('Automatic translation completed, no strings were updated.'), ungettext('Automatic translation completed, %d string was updated.', 'Automatic translation completed, %d strings were updated.', auto.updated)) return redirect(translation)
def upload_glossary(request, project, lang): prj = get_project(request, project) if not request.user.has_perm("glossary.upload", prj): raise PermissionDenied() lang = get_object_or_404(Language, code=lang) form = GlossaryUploadForm(prj, request.POST, request.FILES) if form.is_valid(): try: count = Term.objects.upload( request, form.cleaned_data["glossary"], lang, request.FILES["file"], form.cleaned_data["method"], ) import_message( request, count, _("No terms to import found in file."), ngettext( "Imported %d term from the uploaded file.", "Imported %d terms from the uploaded file.", count, ), ) except Exception as error: report_error(cause="Failed to handle upload") messages.error(request, _("File upload has failed: %s") % force_str(error)) else: messages.error(request, _("Failed to process form!")) return redirect("show_glossary", project=prj.slug, lang=lang.code)
def bulk_edit(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) if not request.user.has_perm('translation.auto', obj): raise PermissionDenied() form = BulkEditForm(request.user, obj, request.POST, project=context['project']) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) target_state = int(form.cleaned_data['state']) add_flags = Flags(form.cleaned_data['add_flags']) remove_flags = Flags(form.cleaned_data['remove_flags']) add_labels = form.cleaned_data['add_labels'] remove_labels = form.cleaned_data['remove_labels'] matching = unit_set.search(form.cleaned_data['q']) updated = 0 with transaction.atomic(): for unit in matching.select_for_update(): if not request.user.has_perm('unit.edit', unit): continue if target_state != -1 and unit.state: unit.translate( request.user, unit.target, target_state, change_action=Change.ACTION_MASS_STATE, ) updated += 1 if add_flags or remove_flags: flags = Flags(unit.source_info.extra_flags) flags.merge(add_flags) flags.remove(remove_flags) unit.source_info.extra_flags = flags.format() unit.source_info.save(update_fields=['extra_flags']) updated += 1 if add_labels: unit.source_info.labels.add(*add_labels) updated += 1 if remove_labels: unit.source_info.labels.remove(*remove_labels) updated += 1 import_message( request, updated, _('Bulk edit completed, no strings were updated.'), ungettext( 'Bulk edit completed, %d string was updated.', 'Bulk edit 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(): 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 state_change(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) if not request.user.has_perm('translation.auto', obj): raise PermissionDenied() form = BulkStateForm(request.user, obj, request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) matching = unit_set.filter_type( form.cleaned_data['type'], context['project'], context['translation'].language if 'translation' in context else None, ).exclude( state=STATE_EMPTY ) updated = 0 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, int(form.cleaned_data['state']), change_action=Change.ACTION_MASS_STATE, ) updated += 1 import_message( request, updated, _('Bulk status change completed, no strings were updated.'), ungettext( 'Bulk status change completed, %d string was updated.', 'Bulk status change completed, %d strings were updated.', updated ) ) return redirect(obj)
def state_change(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) if not request.user.has_perm('translation.auto', obj): raise PermissionDenied() form = MassStateForm(request.user, obj, request.POST) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) matching = unit_set.filter_type( form.cleaned_data['type'], context['project'], context['translation'].language if 'translation' in context else None, ).exclude( state=STATE_EMPTY ) updated = 0 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, int(form.cleaned_data['state']), change_action=Change.ACTION_MASS_STATE, ) updated += 1 import_message( request, updated, _('Mass state change completed, no strings were updated.'), ungettext( 'Mass state change completed, %d string was updated.', 'Mass state change completed, %d strings were updated.', updated ) ) return redirect(obj)
def bulk_edit(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) if not request.user.has_perm("translation.auto", obj): raise PermissionDenied() form = BulkEditForm(request.user, obj, request.POST, project=context["project"]) if not form.is_valid(): messages.error(request, _("Failed to process form!")) show_form_errors(request, form) return redirect(obj) updated = bulk_perform( request.user, unit_set, query=form.cleaned_data["q"], target_state=form.cleaned_data["state"], add_flags=form.cleaned_data["add_flags"], remove_flags=form.cleaned_data["remove_flags"], add_labels=form.cleaned_data["add_labels"], remove_labels=form.cleaned_data["remove_labels"], project=context["project"], components=context["components"], ) import_message( request, updated, _("Bulk edit completed, no strings were updated."), ngettext( "Bulk edit completed, %d string was updated.", "Bulk edit completed, %d strings were updated.", updated, ), ) return redirect(obj)
def auto_translation(request, project, component, lang): translation = get_translation(request, project, component, lang) project = translation.component.project if not request.user.has_perm('translation.auto', project): raise PermissionDenied() autoform = AutoForm(request.user, translation, request.POST) if translation.component.locked or not autoform.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, autoform) return redirect(translation) auto = AutoTranslate( request.user, translation, autoform.cleaned_data['type'], request=request ) if autoform.cleaned_data['auto_source'] == 'mt': auto.process_mt( autoform.cleaned_data['engines'], autoform.cleaned_data['threshold'], ) else: auto.process_others( autoform.cleaned_data['component'], ) import_message( request, auto.updated, _('Automatic translation completed, no strings were updated.'), ungettext( 'Automatic translation completed, %d string was updated.', 'Automatic translation completed, %d strings were updated.', auto.updated ) ) return redirect(translation)
def bulk_edit(request, project, component=None, lang=None): obj, unit_set, context = parse_url(request, project, component, lang) if not request.user.has_perm('translation.auto', obj): raise PermissionDenied() form = BulkEditForm(request.user, obj, request.POST, project=context['project']) if not form.is_valid(): messages.error(request, _('Failed to process form!')) show_form_errors(request, form) return redirect(obj) updated = bulk_perform( request.user, unit_set, query=form.cleaned_data['q'], target_state=form.cleaned_data['state'], add_flags=form.cleaned_data['add_flags'], remove_flags=form.cleaned_data['remove_flags'], add_labels=form.cleaned_data['add_labels'], remove_labels=form.cleaned_data['remove_labels'], ) import_message( request, updated, _('Bulk edit completed, no strings were updated.'), ngettext( 'Bulk edit completed, %d string was updated.', 'Bulk edit completed, %d strings were updated.', updated, ), ) return redirect(obj)
def upload_dictionary(request, project, lang): prj = get_project(request, project) if not request.user.has_perm('glossary.upload', prj): raise PermissionDenied() lang = get_object_or_404(Language, code=lang) form = DictUploadForm(request.POST, request.FILES) if form.is_valid(): try: count = Dictionary.objects.upload( request, prj, lang, request.FILES['file'], form.cleaned_data['method'] ) import_message( request, count, _('No words to import found in file.'), ungettext( 'Imported %d word from the uploaded file.', 'Imported %d words from the uploaded file.', count ) ) except Exception as error: report_error(error, request) messages.error( request, _('File upload has failed: %s') % force_text(error) ) else: messages.error(request, _('Failed to process form!')) return redirect( 'show_dictionary', project=prj.slug, lang=lang.code )
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) 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)