from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination

from adhocracy4.api.mixins import ModuleMixin
from adhocracy4.api.permissions import ViewSetRulesPermission
from meinberlin.apps.contrib.filters import IdeaCategoryFilterBackend

from .models import Proposal
from .serializers import ProposalSerializer


# To be changed to a more general IdeaPagination, when using
# pagination via rest api for more idea lists
class BudgetPagination(PageNumberPagination):
    page_size = 15

    def get_paginated_response(self, data):
        response = super(BudgetPagination, self).get_paginated_response(data)
        response.data['page_size'] = self.page_size
        response.data['page_count'] = self.page.paginator.num_pages
        return response


class ProposalViewSet(ModuleMixin,
                      mixins.ListModelMixin,
                      viewsets.GenericViewSet,
                      ):

    pagination_class = BudgetPagination
    serializer_class = ProposalSerializer
    permission_classes = (ViewSetRulesPermission,)
    filter_backends = (DjangoFilterBackend,
                       OrderingFilter,
                       IdeaCategoryFilterBackend,)
    filter_fields = ('is_archived', 'category',)
    ordering_fields = ('created',
                       'comment_count',
                       'positive_rating_count',)

    def get_permission_object(self):
        return self.module

    def get_queryset(self):
        proposals = Proposal.objects\
            .filter(module=self.module) \
            .annotate_comment_count() \
            .annotate_positive_rating_count() \
            .annotate_negative_rating_count() \
            .order_by('created')
        return proposals

@@ -1,12 +1,67 @@
+from django.utils.translation import gettext_lazy as _%0A
 from django_
@@ -110,16 +110,16 @@
 Backend%0A
-
 from res
@@ -398,16 +398,66 @@
 mission%0A
+from adhocracy4.categories.models import Category%0A
 from mei
@@ -1045,30 +1045,1584 @@
 ass 
-ProposalViewSet(Module
+BudgetFilterInfoMixin(ModuleMixin):%0A    def list(self, request, *args, **kwargs):%0A        %22%22%22Add the filter information to the data of the Proposal API.%0A%0A        Needs to be used with rest_framework.mixins.ListModelMixin%0A        %22%22%22%0A        filters = %7B%7D%0A%0A        ordering_choices = %5B('-created', _('Most recent')), %5D%0A        if self.module.has_feature('rate', Proposal):%0A            ordering_choices += ('-positive_rating_count', _('Most popular')),%0A        ordering_choices += ('-comment_count', _('Most commented')),%0A%0A        filters%5B'ordering'%5D = %7B%0A            'label': _('Ordering'),%0A            'choices': ordering_choices%0A        %7D%0A%0A        filters%5B'is_archived'%5D = %7B%0A            'label': _('Archived'),%0A            'choices': %5B%0A                ('', _('All')),%0A                ('false', _('No')),%0A                ('true', _('Yes')),%0A            %5D%0A        %7D%0A%0A        categories = Category.objects.filter(%0A            module=self.module%0A        )%0A        if categories:%0A            category_choices = %5B('', _('All')), %5D%0A            category_icons = %5B%5D%0A            for category in categories:%0A                category_choices += (str(category.pk), category.name),%0A                category_icons += (str(category.pk), category.icon),%0A%0A            filters%5B'category'%5D = %7B%0A                'label': _('Category'),%0A                'choices': category_choices,%0A                'icons': category_icons,%0A            %7D%0A%0A        response = super().list(request, args, kwargs)%0A        response.data%5B'filters'%5D = filters%0A        return response%0A%0A%0Aclass ProposalViewSet(BudgetFilterInfo
 Mixi

add filter info and choices to proposal API