Commit 822034dc authored by Nicolas Joyard's avatar Nicolas Joyard

Add themes + theme scores to API, fixes #163

parent 13c1b7e1
Pipeline #162 passed with stages
in 24 seconds
......@@ -21,9 +21,14 @@ from representatives_recommendations.api import (
RecommendationViewSet
)
from memopol_themes.api import (
ThemeViewSet
)
from memopol_scores.api import (
DossierScoreViewSet,
RepresentativeScoreViewSet,
ThemeScoreViewSet,
VoteScoreViewSet
)
......@@ -54,5 +59,7 @@ router.register('recommendations', RecommendationViewSet, 'api-recommendation')
router.register('representatives', RepresentativeViewSet,
'api-representative')
router.register('scores', RepresentativeScoreViewSet, 'api-score')
router.register('theme_scores', ThemeScoreViewSet, 'api-themescore')
router.register('themes', ThemeViewSet, 'api-themes')
router.register('vote_scores', VoteScoreViewSet, 'api-votescore')
router.register('votes', RQLVoteViewSet, 'api-vote')
......@@ -10,12 +10,14 @@ from representatives.api import DefaultWebPagination
from .models import (
DossierScore,
RepresentativeScore,
ThemeScore,
VoteScore
)
from .serializers import (
DossierScoreSerializer,
RepresentativeScoreSerializer,
ThemeScoreSerializer,
VoteScoreSerializer
)
......@@ -63,6 +65,28 @@ class RepresentativeScoreViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = RepresentativeScoreSerializer
class ThemeScoreViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view theme scores
"""
queryset = ThemeScore.objects.select_related('representative', 'theme')
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter,
RQLFilterBackend
)
filter_fields = {
'representative': ['exact'],
'theme': ['exact'],
'score': ['exact', 'gte', 'lte']
}
search_fields = ('representative', 'theme', 'score')
ordering_fields = ('representative', 'theme', 'score')
pagination_class = DefaultWebPagination
serializer_class = ThemeScoreSerializer
class VoteScoreViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view votes with their score impact.
......
from rest_framework import serializers
from memopol_themes.serializers import ThemeSerializer
from representatives.serializers import RepresentativeSimpleSerializer
from .models import (
DossierScore,
RepresentativeScore,
ThemeScore,
VoteScore
)
......@@ -28,6 +32,15 @@ class RepresentativeScoreSerializer(serializers.HyperlinkedModelSerializer):
}
class ThemeScoreSerializer(serializers.HyperlinkedModelSerializer):
theme = ThemeSerializer()
representative = RepresentativeSimpleSerializer()
class Meta:
model = ThemeScore
fields = ('representative', 'theme', 'score')
class VoteScoreSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
......
from rest_framework import (
filters,
viewsets,
)
from representatives.api import DefaultWebPagination
from rql_filter.backend import RQLFilterBackend
from .models import Theme
from .serializers import ThemeSerializer
class ThemeViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view themes
"""
queryset = Theme.objects.all()
filter_backends = (
filters.SearchFilter,
filters.OrderingFilter,
RQLFilterBackend
)
search_fields = ('name',)
ordering_fields = ('name',)
pagination_class = DefaultWebPagination
serializer_class = ThemeSerializer
from rest_framework import serializers
from .models import Theme
class ThemeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Theme
fields = ('name',)
......@@ -153,6 +153,20 @@ class MandateDetailSerializer(MandateSerializer):
)
class RepresentativeSimpleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Representative
fields = (
'id',
'url',
'first_name',
'last_name'
)
extra_kwargs = {
'url': {'view_name': 'api-representative-detail'},
}
class RepresentativeSerializer(serializers.HyperlinkedModelSerializer):
contacts = ContactField()
......
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