<commit_before>from django.db.models import Prefetch, Q, Subquery, OuterRef
from django.utils import timezone

from .models import Activity, Announcement
from .serializers import ActivitySerializer, AnnouncementSerializer
from ..events.models import Event


def get_activities(person):
    return (
        Activity.objects.without_required_action()
        .filter(recipient=person)
        .select_related("supportgroup", "individual")
        .prefetch_related(
            Prefetch("event", Event.objects.with_serializer_prefetch(person),)
        )[:40]
    )


def get_required_action_activities(person):
    required_action_activities = (
        Activity.objects.with_required_action()
        .filter(recipient=person)
        .prefetch_related(
            Prefetch("event", Event.objects.with_serializer_prefetch(person),)
        )
    )
    # On affiche toutes les activités avec action requise non traitées
    unread_required_action_activities = required_action_activities.exclude(
        status=Activity.STATUS_INTERACTED
    )
    # On affiche les 20 dernières activités avec action requise déjà traitées
    read_required_action_activities = required_action_activities.filter(
        status=Activity.STATUS_INTERACTED
    ).order_by("-created")[:20]

    return required_action_activities.filter(
        pk__in=[a.pk for a in unread_required_action_activities]
        + [a.pk for a in read_required_action_activities]
    ).order_by("-created")


def get_announcements(person=None):
    today = timezone.now()
    cond = Q(start_date__lt=today) & (Q(end_date__isnull=True) | Q(end_date__gt=today))

    # Les annonces sont affichés :
    # - avec les plus grandes priorités d'abord
    # - à priorité égale, les plus récentes d'abord
    # - à priorité et date de début égales, celles qui disparaitront les premières d'abord
    announcements = Announcement.objects.filter(cond).order_by(
        "-priority", "-start_date", "end_date"
    )

    if person:
        return [
            a
            for a in announcements.annotate(
                activity_id=Subquery(
                    Activity.objects.filter(
                        recipient_id=person.id, announcement_id=OuterRef("id")
                    ).values("id")[:1]
                )
            ).select_related("segment")
            if a.segment is None
            or a.segment.get_subscribers_queryset().filter(pk=person.id).exists()
        ]
    else:
        return announcements.filter(segment__isnull=True)
<commit_msg>feat: select related individual and support group on required_actions<commit_after>from django.db.models import Prefetch, Q, Subquery, OuterRef
from django.utils import timezone

from .models import Activity, Announcement
from .serializers import ActivitySerializer, AnnouncementSerializer
from ..events.models import Event


def get_activities(person):
    return (
        Activity.objects.without_required_action()
        .filter(recipient=person)
        .select_related("supportgroup", "individual")
        .prefetch_related(
            Prefetch("event", Event.objects.with_serializer_prefetch(person),)
        )[:40]
    )


def get_required_action_activities(person):
    required_action_activities = (
        Activity.objects.with_required_action()
        .filter(recipient=person)
        .select_related("supportgroup", "individual")
        .prefetch_related(
            Prefetch("event", Event.objects.with_serializer_prefetch(person),)
        )
    )
    # On affiche toutes les activités avec action requise non traitées
    unread_required_action_activities = required_action_activities.exclude(
        status=Activity.STATUS_INTERACTED
    )
    # On affiche les 20 dernières activités avec action requise déjà traitées
    read_required_action_activities = required_action_activities.filter(
        status=Activity.STATUS_INTERACTED
    ).order_by("-created")[:20]

    return required_action_activities.filter(
        pk__in=[a.pk for a in unread_required_action_activities]
        + [a.pk for a in read_required_action_activities]
    ).order_by("-created")


def get_announcements(person=None):
    today = timezone.now()
    cond = Q(start_date__lt=today) & (Q(end_date__isnull=True) | Q(end_date__gt=today))

    # Les annonces sont affichés :
    # - avec les plus grandes priorités d'abord
    # - à priorité égale, les plus récentes d'abord
    # - à priorité et date de début égales, celles qui disparaitront les premières d'abord
    announcements = Announcement.objects.filter(cond).order_by(
        "-priority", "-start_date", "end_date"
    )

    if person:
        return [
            a
            for a in announcements.annotate(
                activity_id=Subquery(
                    Activity.objects.filter(
                        recipient_id=person.id, announcement_id=OuterRef("id")
                    ).values("id")[:1]
                )
            ).select_related("segment")
            if a.segment is None
            or a.segment.get_subscribers_queryset().filter(pk=person.id).exists()
        ]
    else:
        return announcements.filter(segment__isnull=True)

{'fraction_non_alphanumeric': 0.08912621359223301, 'fraction_numerical': 0.0027184466019417475, 'mean_word_length': 2.914133738601824, 'pattern_counts': {'":': 0, '<': 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': '2938432', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1486, 'n_words': 312}