コード例 #1
0
def fetch_componentlists(user, user_translations):
    componentlists = list(
        ComponentList.objects.filter(
            show_dashboard=True,
            components__project_id__in=user.allowed_project_ids,
        ).distinct().order())
    for componentlist in componentlists:
        components = componentlist.components.filter_access(user)
        # Force fetching the query now
        list(components)

        translations = translation_prefetch_tasks(
            prefetch_stats(
                list(user_translations.filter(component__in=components))))

        # Show ghost translations for user languages
        existing = {(translation.component.slug, translation.language.code)
                    for translation in translations}
        languages = user.profile.languages.all()
        for component in components:
            for language in languages:
                if (
                        component.slug,
                        language.code,
                ) in existing or not component.can_add_new_language(user,
                                                                    fast=True):
                    continue
                translations.append(GhostTranslation(component, language))

        componentlist.translations = translations

    # Filter out component lists with translations
    # This will remove the ones where user doesn't have access to anything
    return [c for c in componentlists if c.translations]
コード例 #2
0
ファイル: basic.py プロジェクト: xiaozwu2018/weblate
def show_translation(request, project, component, lang):
    obj = get_translation(request, project, component, lang)
    obj.stats.ensure_all()
    last_changes = Change.objects.prefetch().order().filter(
        translation=obj)[:10]
    user = request.user

    # Get form
    form = get_upload_form(user, obj)

    search_form = SearchForm(request.user)

    # Translations to same language from other components in this project
    other_translations = prefetch_stats(
        list(Translation.objects.prefetch().filter(
            component__project=obj.component.project,
            language=obj.language).exclude(pk=obj.pk)))

    # Include ghost translations for other components, this
    # adds quick way to create translations in other components
    existing = {
        translation.component.slug
        for translation in other_translations
    }
    existing.add(obj.component.slug)
    for test_component in obj.component.project.component_set.filter_access(
            user).exclude(slug__in=existing):
        if test_component.can_add_new_language(user):
            other_translations.append(
                GhostTranslation(test_component, obj.language))

    # Limit the number of other components displayed to 10, preferring untranslated ones
    other_translations = sorted(other_translations,
                                key=lambda t: t.stats.translated_percent)[:10]

    return render(
        request,
        "translation.html",
        {
            "allow_index":
            True,
            "object":
            obj,
            "project":
            obj.component.project,
            "form":
            form,
            "download_form":
            DownloadForm(auto_id="id_dl_%s"),
            "autoform":
            optional_form(
                AutoForm, user, "translation.auto", obj, obj=obj.component),
            "search_form":
            search_form,
            "replace_form":
            optional_form(ReplaceForm, user, "unit.edit", obj),
            "bulk_state_form":
            optional_form(
                BulkEditForm,
                user,
                "translation.auto",
                obj,
                user=user,
                obj=obj,
                project=obj.component.project,
                auto_id="id_bulk_%s",
            ),
            "new_unit_form":
            NewUnitForm(user,
                        initial={"value": Unit(translation=obj, id_hash=-1)}),
            "announcement_form":
            optional_form(AnnouncementForm, user, "component.edit", obj),
            "delete_form":
            optional_form(TranslationDeleteForm,
                          user,
                          "translation.delete",
                          obj,
                          obj=obj),
            "last_changes":
            last_changes,
            "last_changes_url":
            urlencode(obj.get_reverse_url_kwargs()),
            "other_translations":
            other_translations,
            "exporters":
            EXPORTERS.list_exporters(obj),
        },
    )
コード例 #3
0
ファイル: basic.py プロジェクト: dwaynebailey/weblate
def show_component(request, project, component):
    obj = get_component(request, project, component)
    user = request.user

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

    translations = prefetch_stats(list(obj.translation_set.prefetch()))

    # Show ghost translations for user languages
    if obj.can_add_new_language(request.user):
        existing = {translation.language.code for translation in translations}
        for language in user.profile.languages.all():
            if language.code in existing:
                continue
            translations.append(GhostTranslation(obj, language))

    return render(
        request,
        "component.html",
        {
            "allow_index":
            True,
            "object":
            obj,
            "project":
            obj.project,
            "translations":
            sort_objects(translations),
            "reports_form":
            ReportsForm(),
            "last_changes":
            last_changes,
            "last_changes_url":
            urlencode({
                "component": obj.slug,
                "project": obj.project.slug
            }),
            "replace_form":
            optional_form(ReplaceForm, user, "unit.edit", obj),
            "bulk_state_form":
            optional_form(
                BulkEditForm,
                user,
                "translation.auto",
                obj,
                user=user,
                obj=obj,
                project=obj.project,
                auto_id="id_bulk_%s",
            ),
            "announcement_form":
            optional_form(AnnouncementForm, user, "component.edit", obj),
            "delete_form":
            optional_form(
                ComponentDeleteForm, user, "component.edit", obj, obj=obj),
            "rename_form":
            optional_form(
                ComponentRenameForm,
                user,
                "component.edit",
                obj,
                request=request,
                instance=obj,
            ),
            "move_form":
            optional_form(
                ComponentMoveForm,
                user,
                "component.edit",
                obj,
                request=request,
                instance=obj,
            ),
            "search_form":
            SearchForm(request.user),
            "alerts":
            obj.all_alerts,
        },
    )
コード例 #4
0
ファイル: views.py プロジェクト: mashanz/weblate
def show_project(request, lang, project):
    try:
        language_object = Language.objects.get(code=lang)
    except Language.DoesNotExist:
        language_object = Language.objects.fuzzy_get(lang)
        if isinstance(language_object, Language):
            return redirect(language_object)
        raise Http404("No Language matches the given query.")

    project_object = get_project(request, project)
    obj = ProjectLanguage(project_object, language_object)
    user = request.user

    last_changes = Change.objects.last_changes(user).filter(
        language=language_object, project=project_object)[:10]

    translations = list(obj.translation_set)

    # Add ghost translations
    if user.is_authenticated:
        existing = {translation.component.slug for translation in translations}
        for component in project_object.child_components:
            if component.slug in existing:
                continue
            if component.can_add_new_language(user, fast=True):
                translations.append(
                    GhostTranslation(component, language_object))

    return render(
        request,
        "language-project.html",
        {
            "allow_index":
            True,
            "language":
            language_object,
            "project":
            project_object,
            "object":
            obj,
            "last_changes":
            last_changes,
            "last_changes_url":
            urlencode({
                "lang": language_object.code,
                "project": project_object.slug
            }),
            "translations":
            translations,
            "title":
            f"{project_object} - {language_object}",
            "search_form":
            SearchForm(user, language=language_object),
            "licenses":
            project_object.component_set.exclude(
                license="").order_by("license"),
            "language_stats":
            project_object.stats.get_single_language_stats(language_object),
            "delete_form":
            optional_form(ProjectLanguageDeleteForm,
                          user,
                          "translation.delete",
                          obj,
                          obj=obj),
        },
    )