<commit_before>import logging

from django.conf import settings
from django.http import Http404
from django.shortcuts import render, get_object_or_404

from kitsune.community.utils import (
    top_contributors_aoa,
    top_contributors_questions,
    top_contributors_kb,
    top_contributors_l10n,
)
from kitsune.forums.models import Thread
from kitsune.products.models import Product
from kitsune.questions.models import QuestionLocale
from kitsune.sumo.parser import get_object_fallback
from kitsune.wiki.models import Document

from kitsune.search.v2.documents import ProfileDocument


log = logging.getLogger("k.community")


# Doc for the news section:
COMMUNITY_NEWS_DOC = "Community Hub News"


def home(request):
    """Community hub landing page."""

    community_news = get_object_fallback(Document, COMMUNITY_NEWS_DOC, request.LANGUAGE_CODE)
    locale = _validate_locale(request.GET.get("locale"))
    product = request.GET.get("product")
    if product:
        product = get_object_or_404(Product, slug=product)

    # Get the 5 most recent Community Discussion threads.
    recent_threads = Thread.objects.filter(forum__slug="contributors")[:5]

    data = {
        "community_news": community_news,
        "locale": locale,
        "product": product,
        "products": Product.objects.filter(visible=True),
        "threads": recent_threads,
    }

    if locale:
        data["top_contributors_aoa"], _ = top_contributors_aoa(locale=locale)

        # If the locale is en-US we should top KB contributors, else we show
        # top l10n contributors for that locale
        if locale == settings.WIKI_DEFAULT_LANGUAGE:
            data["top_contributors_kb"], _ = top_contributors_kb(product=product)
        else:
            data["top_contributors_l10n"], _ = top_contributors_l10n(
                locale=locale, product=product
            )

        # If the locale is enabled for the Support Forum, show the top
        # contributors for that locale
        if locale in QuestionLocale.objects.locales_list():
            data["top_contributors_questions"], _ = top_contributors_questions(
                locale=locale, product=product
            )
    else:
        # If no locale is specified, we show overall top contributors
        # across locales.
        data["top_contributors_aoa"], _ = top_contributors_aoa()
        data["top_contributors_kb"], _ = top_contributors_kb(product=product)
        data["top_contributors_l10n"], _ = top_contributors_l10n(product=product)
        data["top_contributors_questions"], _ = top_contributors_questions(product=product)

    return render(request, "community/index.html", data)


def search(request):
    """Find users by username and displayname.

    Uses the ES user's index.
    """
    results = []
    q = request.GET.get("q")

    if q:
        search = ProfileDocument.search().query(
            "simple_query_string",
            query=q,
            fields=["username", "name"],
            default_operator="AND",
        )

        results = search.execute().hits

    # For now, we're just truncating results at 30 and not doing any
    # pagination. If somebody complains, we can add pagination or something.
    results = list(results[:30])

    data = {"q": q, "results": results}

    return render(request, "community/search.html", data)


def metrics(request):
    return render(request, "community/metrics.html")


def top_contributors(request, area):
    """Top contributors list view."""

    try:
        page = int(request.GET.get("page", 1))
    except ValueError:
        page = 1
    page_size = 100
    exceeds_page_size = False

    locale = _validate_locale(request.GET.get("locale"))
    product = request.GET.get("product")
    if product:
        product = get_object_or_404(Product, slug=product)

    if area == "army-of-awesome":
        results, total = top_contributors_aoa(locale=locale, count=page_size, page=page)<commit_msg>tweak community search parameters (#4694)<commit_after>import logging

from django.conf import settings
from django.contrib.auth.models import Group
from django.http import Http404
from django.shortcuts import render, get_object_or_404

from kitsune.community.utils import (
    top_contributors_aoa,
    top_contributors_questions,
    top_contributors_kb,
    top_contributors_l10n,
)
from kitsune.forums.models import Thread
from kitsune.products.models import Product
from kitsune.questions.models import QuestionLocale
from kitsune.sumo.parser import get_object_fallback
from kitsune.users.models import CONTRIBUTOR_GROUP
from kitsune.wiki.models import Document

from kitsune.search.v2.documents import ProfileDocument
from elasticsearch_dsl import Q


log = logging.getLogger("k.community")


# Doc for the news section:
COMMUNITY_NEWS_DOC = "Community Hub News"


def home(request):
    """Community hub landing page."""

    community_news = get_object_fallback(Document, COMMUNITY_NEWS_DOC, request.LANGUAGE_CODE)
    locale = _validate_locale(request.GET.get("locale"))
    product = request.GET.get("product")
    if product:
        product = get_object_or_404(Product, slug=product)

    # Get the 5 most recent Community Discussion threads.
    recent_threads = Thread.objects.filter(forum__slug="contributors")[:5]

    data = {
        "community_news": community_news,
        "locale": locale,
        "product": product,
        "products": Product.objects.filter(visible=True),
        "threads": recent_threads,
    }

    if locale:
        data["top_contributors_aoa"], _ = top_contributors_aoa(locale=locale)

        # If the locale is en-US we should top KB contributors, else we show
        # top l10n contributors for that locale
        if locale == settings.WIKI_DEFAULT_LANGUAGE:
            data["top_contributors_kb"], _ = top_contributors_kb(product=product)
        else:
            data["top_contributors_l10n"], _ = top_contributors_l10n(
                locale=locale, product=product
            )

        # If the locale is enabled for the Support Forum, show the top
        # contributors for that locale
        if locale in QuestionLocale.objects.locales_list():
            data["top_contributors_questions"], _ = top_contributors_questions(
                locale=locale, product=product
            )
    else:
        # If no locale is specified, we show overall top contributors
        # across locales.
        data["top_contributors_aoa"], _ = top_contributors_aoa()
        data["top_contributors_kb"], _ = top_contributors_kb(product=product)
        data["top_contributors_l10n"], _ = top_contributors_l10n(product=product)
        data["top_contributors_questions"], _ = top_contributors_questions(product=product)

    return render(request, "community/index.html", data)


def search(request):
    """Find users by username and displayname.

    Uses the ES user's index.
    """
    results = []
    q = request.GET.get("q")

    if q:
        contributor_group_ids = list(
            Group.objects.filter(
                name__in=[
                    "Contributors",
                    CONTRIBUTOR_GROUP,
                ]
            ).values_list("id", flat=True)
        )
        search = ProfileDocument.search().query(
            "boosting",
            positive=Q(
                "simple_query_string",
                query=q,
                fields=["username", "name"],
                default_operator="AND",
            ),
            # reduce the scores of users not in the contributor groups:
            negative=Q(
                "bool",
                must_not=Q("terms", group_ids=contributor_group_ids),
            ),
            negative_boost=0.5,
        )[:30]

        results = search.execute().hits

    data = {"q": q, "results": results}

    return render(request, "community/search.html", data)


def metrics(request):
    return render(request, "community/metrics.html")


def top_contributors(request, area):
    """Top contributors list view."""

    try:
        page = int(request.GET.get("page", 1))
    except ValueError:
        page = 1
    page_size = 100
    exceeds_page_size = False

    locale = _validate_locale(request.GET.get("locale"))
    product = request.GET.get("product")
    if product:
        product = get_object_or_404(Product, slug=product)

    if area == "army-of-awesome":
        results, total = top_contributors_aoa(locale=locale, count=page_size, page=page)
{'fraction_non_alphanumeric': 0.09609467455621301, 'fraction_numerical': 0.008994082840236686, 'mean_word_length': 2.9732016925246825, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4792739', 'n_tokens_mistral': 2577, 'n_tokens_neox': 2360, 'n_words': 577}