<commit_before># -*- coding: utf-8 -*-
from django.http import Http404
from django.template.defaultfilters import date

from rest_framework import viewsets
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from server.models import Instruction
from server.serializers.frontend.instructions import InstructionListSerializer, InstructionSerializer


class InstructionViewSet(viewsets.ModelViewSet):

    queryset = Instruction.objects.filter(deprecated=False, topic__seasons__current=True)

    def get_serializer_class(self):
        if self.action == 'list':
            return InstructionListSerializer
        return InstructionSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True, context={'request': request})

        response = Response(serializer.data)
        response['Cache-Control'] = "public, max-age=86400"
        if queryset.exists():
            latest = queryset.latest()
            response['ETag'] = '"{}"'.format(latest.get_etag())
            response['Last-Modified'] = "{} GMT".format(date(latest.updated, "D, d M Y H:i:s"))
        return response
<commit_msg>length of list for instruction list view optimized<commit_after># -*- coding: utf-8 -*-
from django.http import Http404
from django.template.defaultfilters import date

from rest_framework import viewsets
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response

from server.models import Instruction, State
from server.serializers.frontend.instructions import InstructionListSerializer, InstructionSerializer


class InstructionViewSet(viewsets.ModelViewSet):

    queryset = (
        Instruction.objects
        .filter(deprecated=False, topic__seasons__current=True)
        .exclude(state_id=State.objects.get(done=True).pk)
    )

    def get_serializer_class(self):
        if self.action == 'list':
            return InstructionListSerializer
        return InstructionSerializer

    def list(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True, context={'request': request})

        response = Response(serializer.data)
        response['Cache-Control'] = "public, max-age=86400"
        if queryset.exists():
            latest = queryset.latest()
            response['ETag'] = '"{}"'.format(latest.get_etag())
            response['Last-Modified'] = "{} GMT".format(date(latest.updated, "D, d M Y H:i:s"))
        return response

{'fraction_non_alphanumeric': 0.10544478527607362, 'fraction_numerical': 0.009202453987730062, 'mean_word_length': 3.7522768670309654, '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': '6193331', 'n_tokens_mistral': 729, 'n_tokens_neox': 691, 'n_words': 166}