Skip to content

Commit

Permalink
apps/budgeting: add API filters for categories and is_archived
Browse files Browse the repository at this point in the history
add category=<category_pk> and / or is_archived=true or false as url parameter
to get all propsals, either remove the parameter or add it as empty parameter "category="
  • Loading branch information
fuzzylogic2000 committed Dec 1, 2021
1 parent 1ff20cb commit 7a80cea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions meinberlin/apps/budgeting/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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
Expand All @@ -32,8 +33,9 @@ class ProposalViewSet(ModuleMixin,
serializer_class = ProposalSerializer
permission_classes = (ViewSetRulesPermission,)
filter_backends = (DjangoFilterBackend,
OrderingFilter,)
# filter_fields = ('is_archived',)
OrderingFilter,
IdeaCategoryFilterBackend,)
filter_fields = ('is_archived', 'category',)
ordering_fields = ('created',
'comment_count',
'positive_rating_count',)
Expand Down
13 changes: 13 additions & 0 deletions meinberlin/apps/contrib/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.utils.translation import gettext_lazy as _
from rest_framework.filters import BaseFilterBackend

from adhocracy4.filters.filters import DistinctOrderingFilter
from adhocracy4.filters.widgets import DropdownLinkWidget
Expand All @@ -19,3 +20,15 @@ def __init__(self, *args, **kwargs):
kwargs['widget'] = OrderingWidget
kwargs['empty_label'] = None
super().__init__(*args, **kwargs)


class IdeaCategoryFilterBackend(BaseFilterBackend):
"""Filter ideas for the categories in API."""

def filter_queryset(self, request, queryset, view):

if 'category' in request.GET:
category = request.GET['category']
return queryset.filter(category=category)

return queryset

0 comments on commit 7a80cea

Please sign in to comment.