<commit_before>import bisect

from django.db.models import Prefetch
from django.db.models import Q
from django.core.cache import cache
from django.utils import timezone
from django.utils.translation import ugettext as _

from rest_framework.generics import GenericAPIView
from rest_framework.generics import ListAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.mixins import ListModelMixin
from rest_framework.parsers import FileUploadParser
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED
from rest_framework.status import HTTP_400_BAD_REQUEST
from rest_framework.status import HTTP_200_OK

from author.utils import process_uploaded_task
from author.models import UploadedTask
from author.models import TaskUploadProgress
from author.models import UploadedTaskDeployStatus
from author.tasks import deploy_uploaded_task
from reg.models import Team, Member
from game.models import Task
from game.models import Answer
from game.tasks import recalc_data
from api.serializers import TeamListSerializer
from api.serializers import TeamCreateSerializer
from api.serializers import MemberSerializer
from api.serializers import TaskUploadProgressSerializer
from api.serializers import UploadedTaskSerializer
from api.serializers import UploadedTaskDeployStatusSerializer
from api.serializers import TaskSerializer


class TeamAPIView(ListModelMixin,
                  CreateModelMixin,
                  GenericAPIView):

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return TeamCreateSerializer
        return TeamListSerializer

    def get_queryset(self):
        queryset = Team.objects.all()
        return queryset.exclude(is_hidden=True)

    def get(self, req, *args, **kwargs):
        return self.list(req, *args, **kwargs)

    def post(self, req, *args, **kwargs):
        return self.create(req, *args, **kwargs)


class MemberAPIView(CreateModelMixin,
                    GenericAPIView):

    queryset = Member.objects.all()
    serializer_class = MemberSerializer

    def post(self, req, *args, **kwargs):
        return self.create(req, *args, **kwargs)


# TODO: only for authors
class TaskUploadAPIView(APIView):

    parser_classes = (FileUploadParser,)

    def put(self, req, filename=None, format=None):
        file_obj = req.data['file']
        uploaded_task = UploadedTask.objects.get(
            pk=req.QUERY_PARAMS.get('uploaded_task_pk')
        )
        process_uploaded_task(file_obj, uploaded_task)
        deploy_uploaded_task.delay(uploaded_task)
        return Response({}, status=HTTP_201_CREATED)


# TODO: only for the author who initiated upload
class TaskUploadStartAPIView(APIView):

    def post(self, req, *args, **kwargs):
        uploaded_task = UploadedTask(author=self.request.user)
        uploaded_task.save()
        return Response({'uploaded_task_pk': uploaded_task.pk},
                        status=HTTP_201_CREATED)


# TODO: only for author of the task
class TaskUploadProgressAPIView(ListAPIView):

    serializer_class = TaskUploadProgressSerializer

    def get_queryset(self):
        queryset = TaskUploadProgress.objects.all()
        uploaded_task_pk = self.request.QUERY_PARAMS.get('uploaded_task_pk')
        if uploaded_task_pk is not None:
            queryset = queryset.filter(uploaded_task__pk=uploaded_task_pk)
        return queryset.order_by('-progress')


# TODO: only for author of the tasks
class UploadedTaskAPIView(ListAPIView):

    serializer_class = UploadedTaskSerializer

    def get_queryset(self):
        queryset = UploadedTask.objects.filter(
            Q(author=self.request.user) & ~Q(path='')
        )
        queryset = queryset.prefetch_related(Prefetch(
            'progress',
            queryset=TaskUploadProgress.objects.filter(progress=0),
            to_attr='progress0',
        ))
        queryset = queryset.prefetch_related(Prefetch(
            'progress',
            queryset=TaskUploadProgress.objects.filter(progress=100),
            to_attr='progress100',
        ))
        return queryset


# TODO: only for author of the tasks
class UploadedTaskDeployStatusAPIView(ListAPIView):

    serializer_class = UploadedTaskDeployStatusSerializer

    def get_queryset(self):
        return UploadedTaskDeployStatus.objects.filter(
            uploaded_task__pk=self.kwargs['uploaded_task_pk'],
        )


# TODO: only for author of the tasks
class AllUploadedTaskDeployStatusAPIView(ListAPIView):

    serializer_class = UploadedTaskDeployStatusSerializer

    def get_queryset(self):
        return UploadedTaskDeployStatus.objects.filter(
            uploaded_task__author=self.request.user
        )


class TaskListAPIView(ListAPIView):

    serializer_class = TaskSerializer

    def get_queryset(self):
        selected = cache.get('published')
        if selected is None:
            queryset = Task.objects.all()
            published_pks = [task.pk for task in queryset if task.is_published()]
            queryset = Task.objects.filter(pk__in=published_pks)
            cache.set('published', queryset, timeout=None)
            return queryset
        return selected


class SolvedTaskAPIView(ListAPIView):

    serializer_class = TaskSerializer

    def get_queryset(self):
        if hasattr(self.request.user, 'member'):
            team_pk = self.request.user.member.team.pk
            result = cache.get('solved%d' % team_pk)
            if result is None:
                result = []
            return Task.objects.filter(pk__in=result)
        return Task.objects.filter(pk=-1)


class FlagAPIView(APIView):

    def post(self, req, *args, **kwargs):
        task_pk = req.POST.get('task')
        flag = req.POST.get('flag')

        tasks = Task.objects.filter(pk=task_pk)
        if not tasks:
            return Response({'error': _('No such task.')},
                            status=HTTP_400_BAD_REQUEST)
        task = tasks[0]

        game = task.get_game()
        if game is None or game.ends_at <= timezone.now():
            return Respone({'result': _('Game over!')},<commit_msg>Fix game ending error<commit_after>import bisect

from django.db.models import Prefetch
from django.db.models import Q
from django.core.cache import cache
from django.utils import timezone
from django.utils.translation import ugettext as _

from rest_framework.generics import GenericAPIView
from rest_framework.generics import ListAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.mixins import ListModelMixin
from rest_framework.parsers import FileUploadParser
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED
from rest_framework.status import HTTP_400_BAD_REQUEST
from rest_framework.status import HTTP_200_OK

from author.utils import process_uploaded_task
from author.models import UploadedTask
from author.models import TaskUploadProgress
from author.models import UploadedTaskDeployStatus
from author.tasks import deploy_uploaded_task
from reg.models import Team, Member
from game.models import Task
from game.models import Answer
from game.tasks import recalc_data
from api.serializers import TeamListSerializer
from api.serializers import TeamCreateSerializer
from api.serializers import MemberSerializer
from api.serializers import TaskUploadProgressSerializer
from api.serializers import UploadedTaskSerializer
from api.serializers import UploadedTaskDeployStatusSerializer
from api.serializers import TaskSerializer


class TeamAPIView(ListModelMixin,
                  CreateModelMixin,
                  GenericAPIView):

    def get_serializer_class(self):
        if self.request.method == 'POST':
            return TeamCreateSerializer
        return TeamListSerializer

    def get_queryset(self):
        queryset = Team.objects.all()
        return queryset.exclude(is_hidden=True)

    def get(self, req, *args, **kwargs):
        return self.list(req, *args, **kwargs)

    def post(self, req, *args, **kwargs):
        return self.create(req, *args, **kwargs)


class MemberAPIView(CreateModelMixin,
                    GenericAPIView):

    queryset = Member.objects.all()
    serializer_class = MemberSerializer

    def post(self, req, *args, **kwargs):
        return self.create(req, *args, **kwargs)


# TODO: only for authors
class TaskUploadAPIView(APIView):

    parser_classes = (FileUploadParser,)

    def put(self, req, filename=None, format=None):
        file_obj = req.data['file']
        uploaded_task = UploadedTask.objects.get(
            pk=req.QUERY_PARAMS.get('uploaded_task_pk')
        )
        process_uploaded_task(file_obj, uploaded_task)
        deploy_uploaded_task.delay(uploaded_task)
        return Response({}, status=HTTP_201_CREATED)


# TODO: only for the author who initiated upload
class TaskUploadStartAPIView(APIView):

    def post(self, req, *args, **kwargs):
        uploaded_task = UploadedTask(author=self.request.user)
        uploaded_task.save()
        return Response({'uploaded_task_pk': uploaded_task.pk},
                        status=HTTP_201_CREATED)


# TODO: only for author of the task
class TaskUploadProgressAPIView(ListAPIView):

    serializer_class = TaskUploadProgressSerializer

    def get_queryset(self):
        queryset = TaskUploadProgress.objects.all()
        uploaded_task_pk = self.request.QUERY_PARAMS.get('uploaded_task_pk')
        if uploaded_task_pk is not None:
            queryset = queryset.filter(uploaded_task__pk=uploaded_task_pk)
        return queryset.order_by('-progress')


# TODO: only for author of the tasks
class UploadedTaskAPIView(ListAPIView):

    serializer_class = UploadedTaskSerializer

    def get_queryset(self):
        queryset = UploadedTask.objects.filter(
            Q(author=self.request.user) & ~Q(path='')
        )
        queryset = queryset.prefetch_related(Prefetch(
            'progress',
            queryset=TaskUploadProgress.objects.filter(progress=0),
            to_attr='progress0',
        ))
        queryset = queryset.prefetch_related(Prefetch(
            'progress',
            queryset=TaskUploadProgress.objects.filter(progress=100),
            to_attr='progress100',
        ))
        return queryset


# TODO: only for author of the tasks
class UploadedTaskDeployStatusAPIView(ListAPIView):

    serializer_class = UploadedTaskDeployStatusSerializer

    def get_queryset(self):
        return UploadedTaskDeployStatus.objects.filter(
            uploaded_task__pk=self.kwargs['uploaded_task_pk'],
        )


# TODO: only for author of the tasks
class AllUploadedTaskDeployStatusAPIView(ListAPIView):

    serializer_class = UploadedTaskDeployStatusSerializer

    def get_queryset(self):
        return UploadedTaskDeployStatus.objects.filter(
            uploaded_task__author=self.request.user
        )


class TaskListAPIView(ListAPIView):

    serializer_class = TaskSerializer

    def get_queryset(self):
        selected = cache.get('published')
        if selected is None:
            queryset = Task.objects.all()
            published_pks = [task.pk for task in queryset if task.is_published()]
            queryset = Task.objects.filter(pk__in=published_pks)
            cache.set('published', queryset, timeout=None)
            return queryset
        return selected


class SolvedTaskAPIView(ListAPIView):

    serializer_class = TaskSerializer

    def get_queryset(self):
        if hasattr(self.request.user, 'member'):
            team_pk = self.request.user.member.team.pk
            result = cache.get('solved%d' % team_pk)
            if result is None:
                result = []
            return Task.objects.filter(pk__in=result)
        return Task.objects.filter(pk=-1)


class FlagAPIView(APIView):

    def post(self, req, *args, **kwargs):
        task_pk = req.POST.get('task')
        flag = req.POST.get('flag')

        tasks = Task.objects.filter(pk=task_pk)
        if not tasks:
            return Response({'error': _('No such task.')},
                            status=HTTP_400_BAD_REQUEST)
        task = tasks[0]

        game = task.get_game()
        if game is None or game.ends_at <= timezone.now():
            return Response({'error': _('Game over!')},
{'fraction_non_alphanumeric': 0.08076239702794379, 'fraction_numerical': 0.004522694233564852, 'mean_word_length': 3.2670572019297035, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1487450', 'n_tokens_mistral': 3562, 'n_tokens_neox': 3432, 'n_words': 812}