Commit 394fbec7 authored by Nicolas Joyard's avatar Nicolas Joyard

Add scores, scoredvotes, recommendations to api

parent 7d05cdc8
...@@ -13,6 +13,13 @@ from representatives_votes.api import ( ...@@ -13,6 +13,13 @@ from representatives_votes.api import (
VoteViewSet, VoteViewSet,
) )
from representatives_recommendations.api import (
RecommendationViewSet,
RepresentativeScoreViewSet,
ScoredVoteViewSet
)
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r'constituencies', ConstituencyViewSet) router.register(r'constituencies', ConstituencyViewSet)
...@@ -20,5 +27,8 @@ router.register(r'dossiers', DossierViewSet) ...@@ -20,5 +27,8 @@ router.register(r'dossiers', DossierViewSet)
router.register(r'groups', GroupViewSet) router.register(r'groups', GroupViewSet)
router.register(r'mandates', MandateViewSet) router.register(r'mandates', MandateViewSet)
router.register(r'proposals', ProposalViewSet) router.register(r'proposals', ProposalViewSet)
router.register(r'recommendations', RecommendationViewSet)
router.register(r'representatives', RepresentativeViewSet) router.register(r'representatives', RepresentativeViewSet)
router.register(r'scores', RepresentativeScoreViewSet)
router.register(r'scored_votes', ScoredVoteViewSet)
router.register(r'votes', VoteViewSet) router.register(r'votes', VoteViewSet)
from rest_framework import (
filters,
viewsets,
)
from representatives.api import DefaultWebPagination
from .models import (
Recommendation,
RepresentativeScore,
ScoredVote
)
from .serializers import (
RecommendationSerializer,
RepresentativeScoreSerializer,
ScoredVoteSerializer
)
class RecommendationViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows recommendations to be viewed.
"""
queryset = Recommendation.objects.select_related('proposal')
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
'id': ['exact'],
'recommendation': ['exact'],
'title': ['exact', 'icontains'],
'description': ['exact', 'icontains'],
'weight': ['exact', 'gte', 'lte']
}
search_fields = ('title', 'description')
ordering_fields = ('id', 'weight', 'title')
pagination_class = DefaultWebPagination
serializer_class = RecommendationSerializer
class RepresentativeScoreViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view representative scores
"""
queryset = RepresentativeScore.objects.select_related('representative')
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
'representative': ['exact'],
'score': ['exact', 'gte', 'lte']
}
search_fields = ('representative', 'score')
ordering_fields = ('representative', 'score')
pagination_class = DefaultWebPagination
serializer_class = RepresentativeScoreSerializer
class ScoredVoteViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view votes with their score impact.
This endpoint only shows votes that have a matching recommendation.
"""
queryset = ScoredVote.objects.select_related(
'representative',
'proposal',
'proposal__recommendation'
).filter(
proposal__recommendation__isnull=False
)
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
}
pagination_class = DefaultWebPagination
serializer_class = ScoredVoteSerializer
from rest_framework import serializers
from .models import (
Recommendation,
RepresentativeScore,
ScoredVote
)
class RecommendationSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Recommendation
fields = ('recommendation', 'title', 'description', 'weight',
'proposal')
class RepresentativeScoreSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = RepresentativeScore
fields = ('representative', 'score')
class ScoredVoteSerializer(serializers.HyperlinkedModelSerializer):
"""
Scored Vote serializer
"""
class Meta:
model = ScoredVote
fields = (
'proposal',
'representative',
'position',
'absolute_score'
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment