Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
def show_translation(request, project, subproject, lang):
    obj = get_translation(request, project, subproject, lang)
    last_changes = Change.objects.prefetch().filter(translation=obj)[:10]

    # Check locks
    obj.is_locked(request.user)

    # Get form
    form = get_upload_form(request.user, obj.subproject.project)()

    # Is user allowed to do automatic translation?
    if can_automatic_translation(request.user, obj.subproject.project):
        autoform = AutoForm(obj, request.user)
    else:
        autoform = None

    # Search form for everybody
    search_form = SearchForm()

    # Review form for logged in users
    if request.user.is_anonymous():
        review_form = None
    else:
        review_form = ReviewForm(
            initial={
                'date': timezone.now().date() - datetime.timedelta(days=31)
            })

    replace_form = None
    if can_translate(request.user, obj):
        replace_form = ReplaceForm()

    return render(
        request, 'translation.html', {
            'object':
            obj,
            'project':
            obj.subproject.project,
            'form':
            form,
            'autoform':
            autoform,
            'search_form':
            search_form,
            'review_form':
            review_form,
            'replace_form':
            replace_form,
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode(obj.get_kwargs()),
            'show_only_component':
            True,
            'other_translations':
            Translation.objects.prefetch().filter(
                subproject__project=obj.subproject.project,
                language=obj.language,
            ).exclude(pk=obj.pk),
        })
Example #5
0
def show_project(request, project):
    obj = get_project(request, project)

    dict_langs = Language.objects.filter(dictionary__project=obj).annotate(
        Count('dictionary'))

    last_changes = Change.objects.prefetch().filter(project=obj)[:10]

    language_stats = sort_unicode(obj.stats.get_language_stats(),
                                  lambda x: force_text(x.language.name))

    # Is user allowed to do automatic translation?
    if request.user.has_perm('translation.auto', obj):
        mass_state_form = MassStateForm(request.user, obj)
    else:
        mass_state_form = None

    if request.user.has_perm('unit.edit', obj):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    # Paginate components of project.
    all_components = obj.component_set.select_related()
    components = prefetch_stats(get_paginator(request, all_components))

    return render(
        request, 'project.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj,
            'dicts':
            dict_langs,
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({'project': obj.slug}),
            'language_stats':
            language_stats,
            'language_count':
            Language.objects.filter(
                translation__component__project=obj).distinct().count(),
            'search_form':
            SearchForm(),
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'components':
            components,
            'licenses':
            ', '.join(
                sorted(set([x.license for x in all_components if x.license]))),
        })
Example #6
0
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)
Example #7
0
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)
Example #8
0
def show_subproject(request, project, subproject):
    obj = get_subproject(request, project, subproject)

    last_changes = Change.objects.for_component(obj)[:10]

    try:
        sample = obj.translation_set.all()[0]
        source_words = sample.total_words
        total_strings = sample.total
    except IndexError:
        source_words = 0
        total_strings = 0

    if can_translate(request.user, project=obj.project):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request, 'subproject.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.project,
            'translations':
            sort_objects(obj.translation_set.all()),
            'show_language':
            1,
            'reports_form':
            ReportsForm(),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({
                'subproject': obj.slug,
                'project': obj.project.slug
            }),
            'unit_count':
            Unit.objects.filter(translation__subproject=obj).count(),
            'words_count':
            obj.get_total_words(),
            'language_count':
            Language.objects.filter(
                translation__subproject=obj).distinct().count(),
            'strings_count':
            total_strings,
            'source_words_count':
            source_words,
            'replace_form':
            replace_form,
            'search_form':
            SearchForm(),
        })
Example #9
0
def show_project(request, project):
    obj = get_project(request, project)

    dict_langs = Language.objects.filter(dictionary__project=obj).annotate(
        Count('dictionary'))

    last_changes = Change.objects.for_project(obj)[:10]

    language_stats = sort_unicode(obj.stats.get_language_stats(),
                                  lambda x: force_text(x.language.name))

    if can_translate(request.user, project=obj):
        replace_form = ReplaceForm()
        mass_state_form = MassStateForm(request.user, obj)
    else:
        replace_form = None
        mass_state_form = None

    return render(
        request, 'project.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj,
            'dicts':
            dict_langs,
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({'project': obj.slug}),
            'language_stats':
            language_stats,
            'unit_count':
            Unit.objects.filter(translation__subproject__project=obj).count(),
            'words_count':
            obj.stats.all_words,
            'language_count':
            Language.objects.filter(
                translation__subproject__project=obj).distinct().count(),
            'strings_count':
            obj.stats.source_strings,
            'source_words_count':
            obj.stats.source_words,
            'search_form':
            SearchForm(),
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'components':
            prefetch_stats(obj.subproject_set.select_related()),
        })
Example #10
0
def show_project(request, project):
    obj = get_project(request, project)

    dict_langs = Language.objects.filter(
        dictionary__project=obj
    ).annotate(Count('dictionary'))

    last_changes = Change.objects.for_project(obj)[:10]

    language_stats = sort_unicode(
        get_per_language_stats(obj), lambda tup: force_text(tup[0])
    )

    language_stats = [
        (
            tup[0],
            translation_percent(tup[1], tup[2]),
            translation_percent(tup[3], tup[4])
        )
        for tup in language_stats
    ]

    if can_translate(request.user, project=obj):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request,
        'project.html',
        {
            'allow_index': True,
            'object': obj,
            'project': obj,
            'dicts': dict_langs,
            'last_changes': last_changes,
            'last_changes_url': urlencode(
                {'project': obj.slug}
            ),
            'language_stats': language_stats,
            'unit_count': Unit.objects.filter(
                translation__subproject__project=obj
            ).count(),
            'words_count': obj.get_total_words(),
            'language_count': Language.objects.filter(
                translation__subproject__project=obj
            ).distinct().count(),
            'strings_count': obj.get_total(),
            'source_words_count': obj.get_source_words(),
            'search_form': SearchForm(),
            'replace_form': replace_form,
        }
    )
Example #11
0
def show_translation(request, project, subproject, lang):
    obj = get_translation(request, project, subproject, lang)
    last_changes = Change.objects.for_translation(obj)[:10]

    # Get form
    form = get_upload_form(request.user, obj)

    # Is user allowed to do automatic translation?
    if can_automatic_translation(request.user, obj.subproject.project):
        autoform = AutoForm(obj, request.user)
    else:
        autoform = None

    # Search form for everybody
    search_form = SearchForm()

    # Review form for logged in users
    if request.user.is_anonymous:
        review_form = None
    else:
        review_form = ReviewForm(
            initial={'exclude_user': request.user.username}
        )

    replace_form = None
    if can_translate(request.user, translation=obj):
        replace_form = ReplaceForm()

    return render(
        request,
        'translation.html',
        {
            'allow_index': True,
            'object': obj,
            'project': obj.subproject.project,
            'form': form,
            'autoform': autoform,
            'search_form': search_form,
            'review_form': review_form,
            'replace_form': replace_form,
            'new_unit_form': NewUnitForm(),
            'last_changes': last_changes,
            'last_changes_url': urlencode(obj.get_kwargs()),
            'show_only_component': True,
            'other_translations': Translation.objects.prefetch().filter(
                subproject__project=obj.subproject.project,
                language=obj.language,
            ).exclude(
                pk=obj.pk
            ),
        }
    )
Example #12
0
def show_subproject(request, project, subproject):
    obj = get_subproject(request, project, subproject)

    last_changes = Change.objects.for_component(obj)[:10]

    if can_translate(request.user, project=obj.project):
        replace_form = ReplaceForm()
        mass_state_form = MassStateForm(request.user, obj)
    else:
        replace_form = None
        mass_state_form = None

    return render(
        request, 'subproject.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.project,
            'translations':
            sort_objects(prefetch_stats(obj.translation_set.all())),
            'show_language':
            1,
            'reports_form':
            ReportsForm(),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({
                'subproject': obj.slug,
                'project': obj.project.slug
            }),
            'unit_count':
            Unit.objects.filter(translation__subproject=obj).count(),
            'words_count':
            obj.stats.all_words,
            'language_count':
            Language.objects.filter(
                translation__subproject=obj).distinct().count(),
            'strings_count':
            obj.stats.source_strings,
            'source_words_count':
            obj.stats.source_words,
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'search_form':
            SearchForm(),
        })
Example #13
0
def show_component(request, project, component):
    obj = get_component(request, project, component)

    last_changes = Change.objects.prefetch().filter(component=obj)[:10]

    # Is user allowed to do automatic translation?
    if request.user.has_perm('translation.auto', obj):
        mass_state_form = MassStateForm(request.user, obj)
    else:
        mass_state_form = None

    if request.user.has_perm('unit.edit', obj):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request, 'component.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.project,
            'translations':
            sort_objects(prefetch_stats(obj.translation_set.all())),
            'show_language':
            1,
            'reports_form':
            ReportsForm(),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({
                'component': obj.slug,
                'project': obj.project.slug
            }),
            'language_count':
            Language.objects.filter(
                translation__component=obj).distinct().count(),
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'search_form':
            SearchForm(),
        })
Example #14
0
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)
Example #15
0
def show_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)
    obj.stats.ensure_all()
    last_changes = Change.objects.prefetch().filter(translation=obj)[:10]

    # Get form
    form = get_upload_form(request.user, obj)

    # Is user allowed to do automatic translation?
    if request.user.has_perm('translation.auto', obj):
        mass_state_form = MassStateForm(request.user, obj)
    else:
        mass_state_form = None

    # Is user allowed to do automatic translation?
    if request.user.has_perm('translation.auto', obj):
        autoform = AutoForm(obj, request.user)
    else:
        autoform = None

    # Search form for everybody
    search_form = SearchForm()

    # Review form for logged in users
    if request.user.is_anonymous:
        review_form = None
    else:
        review_form = ReviewForm(
            initial={'exclude_user': request.user.username})

    if request.user.has_perm('unit.edit', obj):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request, 'translation.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.component.project,
            'form':
            form,
            'download_form':
            DownloadForm(),
            'autoform':
            autoform,
            'search_form':
            search_form,
            'review_form':
            review_form,
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'new_unit_form':
            NewUnitForm(
                request.user,
                initial={
                    'value': Unit(translation=obj, id_hash=-1),
                },
            ),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode(obj.get_reverse_url_kwargs()),
            'show_only_component':
            True,
            'other_translations':
            prefetch_stats(Translation.objects.prefetch().filter(
                component__project=obj.component.project,
                language=obj.language,
            ).exclude(pk=obj.pk)),
            'exporters':
            list_exporters(),
        })
Example #16
0
def show_translation(request, project, subproject, lang):
    obj = get_translation(request, project, subproject, lang)
    obj.stats.ensure_all()
    last_changes = Change.objects.for_translation(obj)[:10]

    # Get form
    form = get_upload_form(request.user, obj)

    # Is user allowed to do automatic translation?
    if can_automatic_translation(request.user, obj.subproject.project):
        autoform = AutoForm(obj, request.user)
    else:
        autoform = None

    # Search form for everybody
    search_form = SearchForm()

    # Review form for logged in users
    if request.user.is_anonymous:
        review_form = None
    else:
        review_form = ReviewForm(
            initial={'exclude_user': request.user.username})

    if can_translate(request.user, translation=obj):
        replace_form = ReplaceForm()
        mass_state_form = MassStateForm(request.user, obj)
    else:
        replace_form = None
        mass_state_form = None

    return render(
        request, 'translation.html', {
            'allow_index':
            True,
            'object':
            obj,
            'project':
            obj.subproject.project,
            'form':
            form,
            'autoform':
            autoform,
            'search_form':
            search_form,
            'review_form':
            review_form,
            'replace_form':
            replace_form,
            'mass_state_form':
            mass_state_form,
            'new_unit_form':
            NewUnitForm(),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode(obj.get_kwargs()),
            'show_only_component':
            True,
            'pending_fulltext':
            obj.unit_set.filter(id__in=IndexUpdate.objects.filter(
                to_delete=False).values('unitid')).exists(),
            'other_translations':
            prefetch_stats(Translation.objects.prefetch().filter(
                subproject__project=obj.subproject.project,
                language=obj.language,
            ).exclude(pk=obj.pk)),
        })
Example #17
0
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)
Example #18
0
def show_project(request, project):
    obj = get_project(request, project)

    dict_langs = Language.objects.filter(dictionary__project=obj).annotate(
        Count('dictionary'))

    if request.method == 'POST' and can_edit_project(request.user, obj):
        settings_form = ProjectSettingsForm(request.POST, instance=obj)
        if settings_form.is_valid():
            settings_form.save()
            messages.success(request, _('Settings saved'))
            return redirect(obj)
        else:
            messages.error(
                request,
                _('Invalid settings, please check the form for errors!'))
    else:
        settings_form = ProjectSettingsForm(instance=obj)

    last_changes = Change.objects.for_project(obj)[:10]

    language_stats = sort_unicode(get_per_language_stats(obj),
                                  lambda tup: force_text(tup[0]))

    language_stats = [(tup[0], translation_percent(tup[1], tup[2]),
                       translation_percent(tup[3], tup[4]))
                      for tup in language_stats]

    if can_translate(request.user, project=obj):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request, 'project.html', {
            'object':
            obj,
            'project':
            obj,
            'dicts':
            dict_langs,
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({'project': obj.slug}),
            'settings_form':
            settings_form,
            'language_stats':
            language_stats,
            'unit_count':
            Unit.objects.filter(translation__subproject__project=obj).count(),
            'words_count':
            obj.get_total_words(),
            'language_count':
            Language.objects.filter(
                translation__subproject__project=obj).distinct().count(),
            'strings_count':
            obj.get_total(),
            'source_words_count':
            obj.get_source_words(),
            'search_form':
            SearchForm(),
            'replace_form':
            replace_form,
        })
Example #19
0
def show_subproject(request, project, subproject):
    obj = get_subproject(request, project, subproject)

    last_changes = Change.objects.for_component(obj)[:10]

    if (request.method == 'POST'
            and can_edit_subproject(request.user, obj.project)):
        settings_form = SubprojectSettingsForm(request.POST, instance=obj)
        if settings_form.is_valid():
            settings_form.save()
            messages.success(request, _('Settings saved'))
            return redirect(obj)
        else:
            messages.error(
                request,
                _('Invalid settings, please check the form for errors!'))
    else:
        settings_form = SubprojectSettingsForm(instance=obj)

    try:
        sample = obj.translation_set.all()[0]
        source_words = sample.total_words
        total_strings = sample.total
    except IndexError:
        source_words = 0
        total_strings = 0

    if can_translate(request.user, project=obj.project):
        replace_form = ReplaceForm()
    else:
        replace_form = None

    return render(
        request, 'subproject.html', {
            'object':
            obj,
            'project':
            obj.project,
            'translations':
            sort_objects(obj.translation_set.enabled()),
            'show_language':
            1,
            'reports_form':
            ReportsForm(),
            'last_changes':
            last_changes,
            'last_changes_url':
            urlencode({
                'subproject': obj.slug,
                'project': obj.project.slug
            }),
            'settings_form':
            settings_form,
            'unit_count':
            Unit.objects.filter(translation__subproject=obj).count(),
            'words_count':
            obj.get_total_words(),
            'language_count':
            Language.objects.filter(
                translation__subproject=obj).distinct().count(),
            'strings_count':
            total_strings,
            'source_words_count':
            source_words,
            'replace_form':
            replace_form,
            'search_form':
            SearchForm(),
        })
Example #20
0
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)