Skip to content

Commit

Permalink
budgeting/serializers: add labels to ProposalSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
rine authored and fuzzylogic2000 committed Sep 27, 2022
1 parent a1367da commit 5535dd6
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions meinberlin/apps/budgeting/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import serializers

from adhocracy4.categories.models import Category
from adhocracy4.labels.models import Label
from meinberlin.apps.votes.models import TokenVote
from meinberlin.apps.votes.models import VotingToken

Expand All @@ -20,13 +21,23 @@ def to_representation(self, category):
return {'id': category.pk, 'name': category.name}


class LabelListingField(serializers.StringRelatedField):

def to_internal_value(self, label):
return Label.objects.get(pk=label)

def to_representation(self, label):
return {'id': label.pk, 'name': label.name}


class ProposalSerializer(serializers.ModelSerializer):

creator = serializers.SerializerMethodField()
comment_count = serializers.SerializerMethodField()
positive_rating_count = serializers.SerializerMethodField()
negative_rating_count = serializers.SerializerMethodField()
category = CategoryField()
labels = LabelListingField(many=True)
url = serializers.SerializerMethodField()
moderator_feedback = serializers.SerializerMethodField()
session_token_voted = serializers.SerializerMethodField()
Expand All @@ -35,15 +46,16 @@ class ProposalSerializer(serializers.ModelSerializer):
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',
'point_label', 'session_token_voted', 'vote_allowed')
'creator', 'is_archived', 'labels', 'name',
'negative_rating_count', 'positive_rating_count', 'url',
'pk', 'moderator_feedback', 'point_label',
'session_token_voted', 'vote_allowed')
read_only_fields = ('budget', 'category', 'comment_count', 'created',
'modified', 'creator', 'is_archived', 'name',
'negative_rating_count', 'positive_rating_count',
'url', 'pk', 'moderator_feedback',
'point_label', 'session_token_voted',
'vote_allowed')
'modified', 'creator', 'is_archived', 'labels',
'name', 'negative_rating_count',
'positive_rating_count', 'url', 'pk',
'moderator_feedback', 'point_label',
'session_token_voted', 'vote_allowed')

def get_creator(self, proposal):
return proposal.creator.username
Expand Down

0 comments on commit 5535dd6

Please sign in to comment.