<commit_before>from django_filters import rest_framework as filters
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.pagination import CursorPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from karrot.conversations.api import RetrieveConversationMixin
from karrot.history.models import History, HistoryTypus
from karrot.pickups.filters import (PickupDatesFilter, PickupDateSeriesFilter, FeedbackFilter)
from karrot.pickups.models import (
    PickupDate as PickupDateModel, PickupDateSeries as PickupDateSeriesModel, Feedback as FeedbackModel
)
from karrot.pickups.permissions import (
    IsUpcoming, HasNotJoinedPickupDate, HasJoinedPickupDate, IsEmptyPickupDate, IsNotFull, IsSameCollector,
    IsRecentPickupDate, IsGroupEditor
)
from karrot.pickups.serializers import (
    PickupDateSerializer, PickupDateSeriesSerializer, PickupDateJoinSerializer, PickupDateLeaveSerializer,
    FeedbackSerializer, PickupDateUpdateSerializer, PickupDateSeriesUpdateSerializer,
    PickupDateSeriesHistorySerializer, FeedbackExportSerializer, FeedbackExportRenderer
)
from karrot.utils.mixins import PartialUpdateModelMixin


class FeedbackPagination(CursorPagination):
    page_size = 10
    ordering = '-id'


class FeedbackViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        PartialUpdateModelMixin,
        GenericViewSet,
):
    """
    Feedback

    # Query parameters
    - `?given_by` - filter by user id
    - `?about` - filter by pickup id
    - `?place` - filter by place id
    - `?group` - filter by group id
    - `?created_at_before` and `?created_at_after` - filter by creation date
    """
    serializer_class = FeedbackSerializer
    queryset = FeedbackModel.objects.all()
    filter_backends = (filters.DjangoFilterBackend, )
    filterset_class = FeedbackFilter
    permission_classes = (IsAuthenticated, IsSameCollector, IsRecentPickupDate)
    pagination_class = FeedbackPagination

    def get_queryset(self):
        return self.queryset.filter(about__place__group__members=self.request.user)

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset()) \
            .select_related('about') \
            .prefetch_related('about__pickupdatecollector_set', 'about__feedback_given_by')
        feedback = self.paginate_queryset(queryset)

        pickups = set(f.about for f in feedback)

        serializer = self.get_serializer(feedback, many=True)
        context = self.get_serializer_context()
        pickups_serializer = PickupDateSerializer(pickups, many=True, context=context)
        return self.get_paginated_response({
            'feedback': serializer.data,
            'pickups': pickups_serializer.data,
        })

    @action(
        detail=False,
        methods=['GET'],
        renderer_classes=(FeedbackExportRenderer, ),
        pagination_class=None,
        serializer_class=FeedbackExportSerializer,
    )
    def export(self, request):
        queryset = self.filter_queryset(self.get_queryset()) \
            .select_related('about', 'about__place', 'about__place__group') \
            .order_by('-about__date')
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


class PickupDateSeriesViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        PartialUpdateModelMixin,
        mixins.ListModelMixin,
        mixins.DestroyModelMixin,
        viewsets.GenericViewSet,
):

    serializer_class = PickupDateSeriesSerializer
    queryset = PickupDateSeriesModel.objects
    filter_backends = (filters.DjangoFilterBackend, )
    filterset_class = PickupDateSeriesFilter
    permission_classes = (IsAuthenticated, IsGroupEditor)

    def get_queryset(self):
        return self.queryset.filter(place__group__members=self.request.user)

    def get_serializer_class(self):
        if self.action == 'partial_update':<commit_msg>allow format parameter for feedback export<commit_after>from django_filters import rest_framework as filters
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.pagination import CursorPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

from karrot.conversations.api import RetrieveConversationMixin
from karrot.history.models import History, HistoryTypus
from karrot.pickups.filters import (PickupDatesFilter, PickupDateSeriesFilter, FeedbackFilter)
from karrot.pickups.models import (
    PickupDate as PickupDateModel, PickupDateSeries as PickupDateSeriesModel, Feedback as FeedbackModel
)
from karrot.pickups.permissions import (
    IsUpcoming, HasNotJoinedPickupDate, HasJoinedPickupDate, IsEmptyPickupDate, IsNotFull, IsSameCollector,
    IsRecentPickupDate, IsGroupEditor
)
from karrot.pickups.serializers import (
    PickupDateSerializer, PickupDateSeriesSerializer, PickupDateJoinSerializer, PickupDateLeaveSerializer,
    FeedbackSerializer, PickupDateUpdateSerializer, PickupDateSeriesUpdateSerializer,
    PickupDateSeriesHistorySerializer, FeedbackExportSerializer, FeedbackExportRenderer
)
from karrot.utils.mixins import PartialUpdateModelMixin


class FeedbackPagination(CursorPagination):
    page_size = 10
    ordering = '-id'


class FeedbackViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        PartialUpdateModelMixin,
        GenericViewSet,
):
    """
    Feedback

    # Query parameters
    - `?given_by` - filter by user id
    - `?about` - filter by pickup id
    - `?place` - filter by place id
    - `?group` - filter by group id
    - `?created_at_before` and `?created_at_after` - filter by creation date
    """
    serializer_class = FeedbackSerializer
    queryset = FeedbackModel.objects.all()
    filter_backends = (filters.DjangoFilterBackend, )
    filterset_class = FeedbackFilter
    permission_classes = (IsAuthenticated, IsSameCollector, IsRecentPickupDate)
    pagination_class = FeedbackPagination

    def get_queryset(self):
        return self.queryset.filter(about__place__group__members=self.request.user)

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset()) \
            .select_related('about') \
            .prefetch_related('about__pickupdatecollector_set', 'about__feedback_given_by')
        feedback = self.paginate_queryset(queryset)

        pickups = set(f.about for f in feedback)

        serializer = self.get_serializer(feedback, many=True)
        context = self.get_serializer_context()
        pickups_serializer = PickupDateSerializer(pickups, many=True, context=context)
        return self.get_paginated_response({
            'feedback': serializer.data,
            'pickups': pickups_serializer.data,
        })

    @action(
        detail=False,
        methods=['GET'],
        renderer_classes=(FeedbackExportRenderer, ),
        pagination_class=None,
        serializer_class=FeedbackExportSerializer,
    )
    def export(self, request, format=None):
        queryset = self.filter_queryset(self.get_queryset()) \
            .select_related('about', 'about__place', 'about__place__group') \
            .order_by('-about__date')
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)


class PickupDateSeriesViewSet(
        mixins.CreateModelMixin,
        mixins.RetrieveModelMixin,
        PartialUpdateModelMixin,
        mixins.ListModelMixin,
        mixins.DestroyModelMixin,
        viewsets.GenericViewSet,
):

    serializer_class = PickupDateSeriesSerializer
    queryset = PickupDateSeriesModel.objects
    filter_backends = (filters.DjangoFilterBackend, )
    filterset_class = PickupDateSeriesFilter
    permission_classes = (IsAuthenticated, IsGroupEditor)

    def get_queryset(self):
        return self.queryset.filter(place__group__members=self.request.user)

    def get_serializer_class(self):
        if self.action == 'partial_update':
{'fraction_non_alphanumeric': 0.07421686746987952, 'fraction_numerical': 0.00048192771084337347, 'mean_word_length': 4.077064220183487, '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': '2842991', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2335, 'n_words': 465}