Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apps/budgeting/api: more info in API and serializer #4063

Merged
merged 7 commits into from
Dec 16, 2021
40 changes: 29 additions & 11 deletions meinberlin/apps/budgeting/api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import mixins
Expand All @@ -7,6 +8,8 @@

from adhocracy4.api.mixins import ModuleMixin
from adhocracy4.api.permissions import ViewSetRulesPermission
from adhocracy4.categories import get_category_icon_url
from adhocracy4.categories import has_icons
from adhocracy4.categories.models import Category
from meinberlin.apps.contrib.filters import IdeaCategoryFilterBackend

Expand All @@ -16,17 +19,24 @@

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

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


class BudgetFilterInfoMixin(ModuleMixin):
class LocaleInfoMixin:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

def list(self, request, *args, **kwargs):
response = super().list(request, args, kwargs)
response.data['locale'] = get_language()
return response


class ProposalFilterInfoMixin(ModuleMixin):
def list(self, request, *args, **kwargs):
"""Add the filter information to the data of the Proposal API.

Expand All @@ -41,7 +51,8 @@ def list(self, request, *args, **kwargs):

filters['ordering'] = {
'label': _('Ordering'),
'choices': ordering_choices
'choices': ordering_choices,
'default': '-created',
}

filters['is_archived'] = {
Expand All @@ -50,36 +61,43 @@ def list(self, request, *args, **kwargs):
('', _('All')),
('false', _('No')),
('true', _('Yes')),
]
],
'default': 'false',
}

categories = Category.objects.filter(
module=self.module
)
if categories:
category_choices = [('', _('All')), ]
category_icons = []
if has_icons(self.module):
category_icons = []
for category in categories:
category_choices += (str(category.pk), category.name),
category_icons += (str(category.pk), category.icon),
if has_icons(self.module):
icon_name = getattr(category, 'icon', None)
icon_url = get_category_icon_url(icon_name)
category_icons += (str(category.pk), icon_url),

filters['category'] = {
'label': _('Category'),
'choices': category_choices,
'icons': category_icons,
}
if has_icons(self.module):
filters['category']['icons'] = category_icons

response = super().list(request, args, kwargs)
response.data['filters'] = filters
return response


class ProposalViewSet(BudgetFilterInfoMixin,
class ProposalViewSet(ProposalFilterInfoMixin,
LocaleInfoMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet,
):

pagination_class = BudgetPagination
pagination_class = ProposalPagination
serializer_class = ProposalSerializer
permission_classes = (ViewSetRulesPermission,)
filter_backends = (DjangoFilterBackend,
Expand All @@ -99,5 +117,5 @@ def get_queryset(self):
.annotate_comment_count() \
.annotate_positive_rating_count() \
.annotate_negative_rating_count() \
.order_by('created')
.order_by('-created')
return proposals
21 changes: 5 additions & 16 deletions meinberlin/apps/budgeting/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.utils import translation
from rest_framework import serializers

from adhocracy4.categories.models import Category
Expand Down Expand Up @@ -26,19 +25,17 @@ class ProposalSerializer(serializers.ModelSerializer):
negative_rating_count = serializers.SerializerMethodField()
category = CategoryField()
url = serializers.SerializerMethodField()
locale = serializers.SerializerMethodField()
moderator_feedback = serializers.SerializerMethodField()

class Meta:
model = Proposal
fields = ('budget', 'category', 'comment_count', 'created', 'modified',
'creator', 'is_archived', 'name', 'negative_rating_count',
'positive_rating_count', 'url', 'pk', 'moderator_feedback',
'moderator_feedback_choices', 'locale')
'positive_rating_count', 'url', 'pk', 'moderator_feedback')
read_only_fields = ('budget', 'category', 'comment_count', 'created',
'modified', 'creator', 'is_archived', 'name',
'negative_rating_count', 'positive_rating_count',
'url', 'pk', 'moderator_feedback',
'moderator_feedback_choices', 'locale')
'url', 'pk', 'moderator_feedback')

def get_creator(self, proposal):
return proposal.creator.username
Expand Down Expand Up @@ -66,15 +63,7 @@ def get_url(self, proposal):

def get_moderator_feedback(self, proposal):
if hasattr(proposal, 'moderator_feedback'):
return proposal.moderator_feedback
return (proposal.moderator_feedback,
proposal.get_moderator_feedback_display())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better now :)

else:
return None

def get_moderator_feedback_choices(self, proposal):
if hasattr(proposal, 'moderator_feedback_choices'):
return proposal.moderator_feedback_choices
else:
return None

def get_locale(self, proposal):
return translation.get_language()
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</a>
{% endif %}
{% comment %}
Fixme: differentiating 2 and 3 phase buergerhaushalt by just counting the phases
FIXME: differentiating 2 and 3 phase buergerhaushalt by just counting the phases
should be changed once we have module types
{% endcomment %}
{% if module.phases.count == 3 %}
Expand Down