<commit_before>from itertools import chain

from data_france.models import CirconscriptionConsulaire, Commune
from django.conf import settings
from django.core import exceptions
from django.http.response import Http404
from rest_framework import status, permissions
from rest_framework.exceptions import ValidationError, Throttled
from rest_framework.generics import (
    CreateAPIView,
    ListAPIView,
    RetrieveUpdateAPIView,
    RetrieveAPIView,
    UpdateAPIView,
)
from rest_framework.response import Response

from agir.lib.export import dict_to_camelcase
from agir.lib.token_bucket import TokenBucket
from agir.lib.utils import get_client_ip
from agir.voting_proxies.actions import (
    get_voting_proxy_requests_for_proxy,
    accept_voting_proxy_requests,
    decline_voting_proxy_requests,
    confirm_voting_proxy_requests,
)
from agir.voting_proxies.models import VotingProxyRequest, VotingProxy
from agir.voting_proxies.serializers import (
    VotingProxyRequestSerializer,
    CommuneOrConsulateSerializer,
    VotingProxySerializer,
    CreateVotingProxySerializer,
    AcceptedVotingProxyRequestSerializer,
)
from agir.voting_proxies.tasks import send_voting_proxy_information_for_request


class CommuneOrConsulateSearchAPIView(ListAPIView):
    permission_classes = (permissions.AllowAny,)
    serializer_class = CommuneOrConsulateSerializer
    search_term_param = "q"

    def list(self, request, *args, **kwargs):
        search_term = request.GET.get(self.search_term_param)
        consulates = CirconscriptionConsulaire.objects.search(search_term)
        communes = self.filter_queryset(
            Commune.objects.filter(
                type__in=(Commune.TYPE_COMMUNE, Commune.TYPE_ARRONDISSEMENT_PLM),
            )
            .exclude(code__in=("75056", "69123", "13055"))
            .search(search_term)
        )
        queryset = sorted(<commit_msg>fix: limit CommuneOrConsulateSearchAPIView results to 20 for each type<commit_after>from itertools import chain

from data_france.models import CirconscriptionConsulaire, Commune
from django.conf import settings
from django.core import exceptions
from django.http.response import Http404
from rest_framework import status, permissions
from rest_framework.exceptions import ValidationError, Throttled
from rest_framework.generics import (
    CreateAPIView,
    ListAPIView,
    RetrieveUpdateAPIView,
    RetrieveAPIView,
    UpdateAPIView,
)
from rest_framework.response import Response

from agir.lib.export import dict_to_camelcase
from agir.lib.token_bucket import TokenBucket
from agir.lib.utils import get_client_ip
from agir.voting_proxies.actions import (
    get_voting_proxy_requests_for_proxy,
    accept_voting_proxy_requests,
    decline_voting_proxy_requests,
    confirm_voting_proxy_requests,
)
from agir.voting_proxies.models import VotingProxyRequest, VotingProxy
from agir.voting_proxies.serializers import (
    VotingProxyRequestSerializer,
    CommuneOrConsulateSerializer,
    VotingProxySerializer,
    CreateVotingProxySerializer,
    AcceptedVotingProxyRequestSerializer,
)
from agir.voting_proxies.tasks import send_voting_proxy_information_for_request


class CommuneOrConsulateSearchAPIView(ListAPIView):
    permission_classes = (permissions.AllowAny,)
    serializer_class = CommuneOrConsulateSerializer
    search_term_param = "q"

    def list(self, request, *args, **kwargs):
        search_term = request.GET.get(self.search_term_param)
        consulates = CirconscriptionConsulaire.objects.search(search_term)[:20]
        communes = self.filter_queryset(
            Commune.objects.filter(
                type__in=(Commune.TYPE_COMMUNE, Commune.TYPE_ARRONDISSEMENT_PLM),
            )
            .exclude(code__in=("75056", "69123", "13055"))
            .search(search_term)[:20]
        )
        queryset = sorted(
{'fraction_non_alphanumeric': 0.06157443491816056, 'fraction_numerical': 0.010911925175370226, 'mean_word_length': 4.636896046852123, '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': '941317', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1131, 'n_words': 221}