<commit_before>import logging
log = logging.getLogger(__name__)

from rest_framework import (
    # mixins,
    viewsets,
    # filters,
)

from django.contrib.auth import get_user_model

from .models import (
    Convention,
    Contest,
    Group,
    Contestant,
    Performance,
    # Note,
)

from .filters import (
    PerformanceFilter,
    GroupFilter,
)


from .serializers import (
    ConventionSerializer,
    ContestSerializer,
    GroupSerializer,
    ContestantSerializer,
    PerformanceSerializer,
    NoteSerializer,
    UserSerializer,
)

User = get_user_model()


class ConventionViewSet(viewsets.ModelViewSet):
    queryset = Convention.objects.filter(
        name__in=[
            # 'Philadelphia 2010',
            # 'Kansas City 2011',
            # 'Portland 2012',
            # 'Toronto 2013',
            'International 2014',
            'International 2015',
        ]
    ).prefetch_related(
        'contests',
        # 'contests__contestants__group',
        # 'contests__contestants__group__contestants',
        # 'contests__contestants__performances',
        # 'contests__contestants__group__lead',
        # 'contests__contestants__group__tenor',
        # 'contests__contestants__group__baritone',
        # 'contests__contestants__group__bass',
    )
    serializer_class = ConventionSerializer
    lookup_field = 'slug'


class ContestViewSet(viewsets.ModelViewSet):
    queryset = Contest.objects.prefetch_related(
        'contestants',
        'contestants__group',
        'contestants__performances',
        # 'contestants__group__contestants',
        'contestants__group__lead',
        'contestants__group__tenor',
        'contestants__group__baritone',
        'contestants__group__bass',
    )
    serializer_class = ContestSerializer
    # filter_class = ChorusFilter
    lookup_field = 'slug'


class ContestantViewSet(viewsets.ModelViewSet):
    queryset = Contestant.objects.select_related(
        'group',
        # 'group__contestants',
        'group__lead',
        'group__tenor',
        'group__baritone',
        'group__bass',
    ).prefetch_related(
        'performances',
        # 'group__contestants',
    )
    serializer_class = ContestantSerializer
    # filter_class = QuartetFilter
    lookup_field = 'slug'


class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.select_related(
        'lead', 'tenor', 'baritone', 'bass'
    ).all().prefetch_related('contestants')
    serializer_class = GroupSerializer
    filter_class = GroupFilter
    lookup_field = 'slug'


class PerformanceViewSet(viewsets.ModelViewSet):
    # queryset = Performance.objects.all()
    queryset = Performance.objects.select_related(
        'contestant__contest',
        'contestant__group',
    )
    # .filter(
    #     contestant__contest__convention__name='Pittsburgh 2015'<commit_msg>Database optimization<commit_after>import logging
log = logging.getLogger(__name__)

from rest_framework import (
    # mixins,
    viewsets,
    # filters,
)

from django.contrib.auth import get_user_model

from .models import (
    Convention,
    Contest,
    Group,
    Contestant,
    Performance,
    # Note,
)

from .filters import (
    PerformanceFilter,
    GroupFilter,
)


from .serializers import (
    ConventionSerializer,
    ContestSerializer,
    GroupSerializer,
    ContestantSerializer,
    PerformanceSerializer,
    NoteSerializer,
    UserSerializer,
)

User = get_user_model()


class ConventionViewSet(viewsets.ModelViewSet):
    queryset = Convention.objects.filter(
        name__in=[
            # 'Philadelphia 2010',
            # 'Kansas City 2011',
            # 'Portland 2012',
            # 'Toronto 2013',
            'International 2014',
            'International 2015',
        ]
    ).prefetch_related(
        'contests',
        # 'contests__contestants__group',
        # 'contests__contestants__group__contestants',
        # 'contests__contestants__performances',
        # 'contests__contestants__group__lead',
        # 'contests__contestants__group__tenor',
        # 'contests__contestants__group__baritone',
        # 'contests__contestants__group__bass',
    )
    serializer_class = ConventionSerializer
    lookup_field = 'slug'


class ContestViewSet(viewsets.ModelViewSet):
    queryset = Contest.objects.prefetch_related(
        'district',
        'contestants',
        'contestants__group',
        'contestants__performances',
        # 'contestants__group__contestants',
        'contestants__group__lead',
        'contestants__group__tenor',
        'contestants__group__baritone',
        'contestants__group__bass',
    )
    serializer_class = ContestSerializer
    # filter_class = ChorusFilter
    lookup_field = 'slug'


class ContestantViewSet(viewsets.ModelViewSet):
    queryset = Contestant.objects.select_related(
        'group',
        # 'group__contestants',
        'group__lead',
        'group__tenor',
        'group__baritone',
        'group__bass',
    ).prefetch_related(
        'performances',
        'contest',
        # 'group__contestants',
    )
    serializer_class = ContestantSerializer
    # filter_class = QuartetFilter
    lookup_field = 'slug'


class GroupViewSet(viewsets.ModelViewSet):
    queryset = Group.objects.select_related(
        'lead', 'tenor', 'baritone', 'bass'
    ).all().prefetch_related('contestants', 'district')
    serializer_class = GroupSerializer
    filter_class = GroupFilter
    lookup_field = 'slug'


class PerformanceViewSet(viewsets.ModelViewSet):
    # queryset = Performance.objects.all()
    queryset = Performance.objects.select_related(
        'contestant__contest',
        'contestant__group',
    )
    # .filter(
    #     contestant__contest__convention__name='Pittsburgh 2015'
{'fraction_non_alphanumeric': 0.09172568664708931, 'fraction_numerical': 0.009673518742442563, 'mean_word_length': 2.984858912594632, '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': '7428760', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1718, 'n_words': 271}