def kb_overview_rows()

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


def kb_overview_rows(user=None, mode=None, max=None, locale=None, product=None, category=None):
    """Return the iterable of dicts needed to draw the new KB dashboard overview"""

    if mode is None:
        mode = LAST_30_DAYS

    docs = (
        Document.objects.visible(user, locale=settings.WIKI_DEFAULT_LANGUAGE, is_archived=False)
        .exclude(html__startswith=REDIRECT_HTML)
        .select_related("current_revision")
    )

    if product:
        docs = docs.filter(products__in=[product])

    if category:
        docs = docs.filter(category__in=[category])

    docs = docs.annotate(
        num_visits=get_visits_subquery(period=mode),
        ready_for_l10n=Case(
            When(
                Q(latest_localizable_revision__isnull=False)
                & ~Exists(
                    Revision.objects.filter(
                        document=OuterRef("pk"),
                        is_approved=True,
                        is_ready_for_localization=False,
                        significance__gt=TYPO_SIGNIFICANCE,
                        id__gt=F("document__latest_localizable_revision__id"),
                    )
                ),
                then=True,
            ),
            default=False,
        ),
        unapproved_revision_comment=Subquery(
            Revision.objects.filter(
                document=OuterRef("pk"),
                reviewed=None,
            )
            .filter(
                Q(document__current_revision__isnull=True)
                | Q(id__gt=F("document__current_revision__id"))
            )
            .order_by("created")[:1]
            .values("comment")
        ),
    )

    if locale and (locale != settings.WIKI_DEFAULT_LANGUAGE):
        transdoc_subquery = Document.objects.filter(
            locale=locale,
            is_archived=False,
            parent=OuterRef("pk"),
            current_revision__isnull=False,
        )
        docs = docs.annotate(
            transdoc_exists=Exists(transdoc_subquery),
            transdoc_current_revision_based_on_id=Subquery(
                transdoc_subquery.values("current_revision__based_on__id")
            ),
        ).annotate(
            transdoc_is_outdated=Exists(
                Revision.objects.filter(
                    document=OuterRef("pk"),
                    is_approved=True,
                    is_ready_for_localization=True,
                    significance__gte=MEDIUM_SIGNIFICANCE,
                    id__gt=OuterRef("transdoc_current_revision_based_on_id"),
                )
            ),
        )

    docs = docs.order_by(F("num_visits").desc(nulls_last=True), "title")

    if max:
        docs = docs[:max]

    rows = []

    max_visits = docs[0].num_visits if docs.count() else None

    for d in docs:
        data = {
            "url": reverse("wiki.document", args=[d.slug], locale=settings.WIKI_DEFAULT_LANGUAGE),
            "trans_url": reverse(
                "wiki.show_translations", args=[d.slug], locale=settings.WIKI_DEFAULT_LANGUAGE
            ),
            "title": d.title,
            "num_visits": d.num_visits,
            "ready_for_l10n": d.ready_for_l10n,
        }

        if d.current_revision:
            data["expiry_date"] = d.current_revision.expires

        if d.num_visits and max_visits:
            data["visits_ratio"] = float(d.num_visits) / max_visits

        if "expiry_date" in data and data["expiry_date"]:
            data["stale"] = data["expiry_date"] < datetime.now()

        if d.unapproved_revision_comment is None:
            data["latest_revision"] = True
        else:
            data["revision_comment"] = d.unapproved_revision_comment

        # Get the translated doc
        if locale and (locale != settings.WIKI_DEFAULT_LANGUAGE):
            if d.transdoc_exists:
                data["needs_update"] = d.transdoc_is_outdated
        else:  # For en-US we show the needs_changes comment.
            data["needs_update"] = d.needs_change
            data["needs_update_comment"] = d.needs_change_comment

        rows.append(data)

    return rows