def l10n_overview_rows()

in kitsune/dashboards/readouts.py [0:0]


def l10n_overview_rows(locale, product=None, user=None):
    """Return the iterable of dicts needed to draw the Overview table."""
    # The Overview table is a special case: it has only a static number of
    # rows, so it has no expanded, all-rows view, and thus needs no slug, no
    # "max" kwarg on rows(), etc. It doesn't fit the Readout signature, so we
    # don't shoehorn it in.

    def percent_or_100(num, denom):
        return int(round(num / float(denom) * 100)) if denom else 100

    ignore_categories = [
        ADMINISTRATION_CATEGORY,
        NAVIGATION_CATEGORY,
        HOW_TO_CONTRIBUTE_CATEGORY,
    ]

    total = Document.objects.visible(
        user,
        locale=settings.WIKI_DEFAULT_LANGUAGE,
        is_archived=False,
        is_localizable=True,
        current_revision__isnull=False,
        latest_localizable_revision__isnull=False,
    ).exclude(html__startswith=REDIRECT_HTML)

    if product:
        total = total.filter(products=product)
        if not product.questions_enabled(locale):
            # The product does not have a forum for this locale.
            ignore_categories.append(CANNED_RESPONSES_CATEGORY)

    total = total.exclude(category__in=ignore_categories)

    total_docs = total.filter(is_template=False).count()
    total_templates = total.filter(is_template=True).count()

    # Translations whose based_on revision has no >10-significance, ready-for-
    # l10n revisions after it.

    any_significant_updates_exist = Exists(
        Revision.objects.filter(
            document=OuterRef("parent"),
            is_ready_for_localization=True,
            significance__gte=MEDIUM_SIGNIFICANCE,
            id__gt=OuterRef("current_revision__based_on__id"),
        )
    )

    up_to_date_translation_count = (
        Document.objects.visible(
            user,
            locale=locale,
            is_archived=False,
            parent__isnull=False,
            parent__is_archived=False,
            parent__is_localizable=True,
            current_revision__isnull=False,
            parent__latest_localizable_revision__isnull=False,
        )
        .exclude(parent__category__in=ignore_categories)
        .exclude(parent__html__startswith=REDIRECT_HTML)
        .exclude(any_significant_updates_exist)
    )

    if product:
        up_to_date_translation_count = up_to_date_translation_count.filter(
            parent__products=product
        )

    translated_docs = up_to_date_translation_count.filter(is_template=False).count()
    translated_templates = up_to_date_translation_count.filter(is_template=True).count()

    top_n_query = (
        Document.objects.visible(
            user,
            locale=settings.WIKI_DEFAULT_LANGUAGE,
            is_archived=False,
            is_template=False,
            is_localizable=True,
            parent__isnull=True,
            current_revision__isnull=False,
            latest_localizable_revision__isnull=False,
        )
        .exclude(category__in=ignore_categories)
        .exclude(html__startswith=REDIRECT_HTML)
    )

    if product:
        top_n_query = top_n_query.filter(products=product)

    top_n_query = (
        top_n_query.annotate(
            is_translated=Exists(
                Document.objects.filter(
                    locale=locale,
                    parent=OuterRef("pk"),
                    current_revision__isnull=False,
                )
                .filter(
                    Exists(
                        Revision.objects.filter(document=OuterRef("pk")).filter(
                            Q(is_approved=True) | Q(reviewed__isnull=True)
                        )
                    )
                )
                .exclude(any_significant_updates_exist)
            )
        )
        .alias(num_visits=get_visits_subquery())
        .order_by(F("num_visits").desc(nulls_last=True), "title")
    )

    top_20_translated = top_n_query[:20].aggregate(
        num_translated=Count("pk", filter=Q(is_translated=True))
    )["num_translated"]

    top_50_translated = top_n_query[:50].aggregate(
        num_translated=Count("pk", filter=Q(is_translated=True))
    )["num_translated"]

    top_100_translated = top_n_query[:100].aggregate(
        num_translated=Count("pk", filter=Q(is_translated=True))
    )["num_translated"]

    return {
        "top-20": {
            "title": _("Top 20 Articles"),
            "numerator": top_20_translated,
            "denominator": 20 if total_docs > 20 else total_docs,
            "percent": percent_or_100(top_20_translated, 20 if total_docs > 20 else total_docs),
            "description": _(
                "These are the top 20 most visited articles "
                "in the last 30 days, which account for over "
                "50% of the traffic to the Knowledge Base."
            ),
        },
        "top-50": {
            "title": _("Top 50 Articles"),
            "numerator": top_50_translated,
            "denominator": 50 if total_docs > 50 else total_docs,
            "percent": percent_or_100(top_50_translated, 50 if total_docs > 50 else total_docs),
            "description": _("These are the top 50 most visited articles " "in the last 30 days."),
        },
        "top-100": {
            "title": _("Top 100 Articles"),
            "numerator": top_100_translated,
            "denominator": 100 if total_docs > 100 else total_docs,
            "percent": percent_or_100(top_100_translated, 100 if total_docs > 100 else total_docs),
            "description": _(
                "These are the top 100 most visited articles "
                "in the last 30 days, which account for over "
                "99% of the traffic to the Knowledge Base."
            ),
        },
        "templates": {
            "title": _("Templates"),
            "url": "#" + TemplateTranslationsReadout.slug,
            "numerator": translated_templates,
            "denominator": total_templates,
            "percent": percent_or_100(translated_templates, total_templates),
            "description": _(
                "Templates are a way of reusing pieces of "
                "content across KB articles. You can create and "
                "update a set of instructions in one place, and "
                "then refer to it in other pages."
            ),
        },
        "all": {
            "title": _("All Knowledge Base Articles"),
            "numerator": translated_docs,
            "denominator": total_docs,
            "percent": percent_or_100(translated_docs, total_docs),
            "description": _(
                "This is the number of all Knowledge Base "
                "articles that are ready to be localized."
            ),
        },
    }