From 2bfedffad4fc3884ecb45905ae3b33be00bbb45d Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 19 Oct 2015 17:08:44 +0200 Subject: [PATCH] Add competence list to duck details --- api/serializers.py | 23 +++++++++++++++++++++-- api/tests.py | 46 +++++++++++++++++++++------------------------- api/urls.py | 1 + api/views.py | 11 ++++++++--- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 3764d05..77a4846 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -2,7 +2,7 @@ from django.core.exceptions import ImproperlyConfigured from rest_framework import serializers -from booking.models import Duck +from booking.models import Duck, Competence, DuckCompetence class NamespacedSerializer(serializers.HyperlinkedModelSerializer): def __init__(self, *args, **kwargs): @@ -11,6 +11,9 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer): self.url_namespace = self.Meta.url_namespace + self.url_namespace = kwargs.pop('url_namespace', + self.url_namespace) + if not self.url_namespace.endswith(':'): self.url_namespace += ':' @@ -28,8 +31,24 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer): return field_class, field_kwargs +class CompetenceSerializer(NamespacedSerializer): + class Meta: + url_namespace = 'api' + model = Competence + fields = ('url', 'name',) + +class DuckCompetenceSerializer(NamespacedSerializer): + comp = CompetenceSerializer() + + class Meta: + url_namespace = 'api' + model = DuckCompetence + fields = ('comp', 'up_minutes', 'down_minutes',) + class DuckSerializer(NamespacedSerializer): + competences = DuckCompetenceSerializer(many=True) + class Meta: url_namespace = 'api' model = Duck - fields = ('url', 'name', 'color',) + fields = ('url', 'name', 'color', 'competences',) diff --git a/api/tests.py b/api/tests.py index 3c46b59..1f98c4a 100644 --- a/api/tests.py +++ b/api/tests.py @@ -18,36 +18,31 @@ class DuckClassTest(WebTest): self.user = User.objects.create_user(username='test', password='test') - spec = Species(name = 'duck') - spec.save() + spec = Species.objects.create(name = 'duck') - loc = Location(name = 'temp') - loc.save() + loc = Location.objects.create(name = 'temp') - self.comp_bad = Competence(name = 'test1', added_by = self.user) - self.comp_bad.save() + self.comp_bad = Competence.objects.create(name = 'test1', + added_by = self.user) - self.comp_good = Competence(name = 'test2', - added_by = self.user) - self.comp_good.save() + self.comp_good = Competence.objects.create(name = 'test2', + added_by = self.user) - self.duck = Duck(species = spec, - location = loc, - donated_by = self.user, - color='123456') - self.duck.save() + self.duck = Duck.objects.create(species = spec, + name='test duck', + location = loc, + donated_by = self.user, + color='123456') - dcomp = DuckCompetence(duck = self.duck, - comp = self.comp_bad, - up_minutes = bad_minutes, - down_minutes = 0) - dcomp.save() + DuckCompetence.objects.create(duck = self.duck, + comp = self.comp_bad, + up_minutes = bad_minutes, + down_minutes = 0) - dcomp = DuckCompetence(duck = self.duck, - comp = self.comp_good, - up_minutes = good_minutes, - down_minutes = 0) - dcomp.save() + DuckCompetence.objects.create(duck = self.duck, + comp = self.comp_good, + up_minutes = good_minutes, + down_minutes = 0) def test_book_nonlogged(self): page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True) @@ -138,6 +133,7 @@ class DuckClassTest(WebTest): self.assertEqual(200, page.status_code) page_json = json.loads(page.content) - self.assertEqual(len(page_json), 3) + self.assertEquals('test duck', page_json['name']) self.assertEquals('123456', page_json['color']) + self.assertEqual(2, len(page_json['competences'])) diff --git a/api/urls.py b/api/urls.py index d6d03fd..daa482a 100644 --- a/api/urls.py +++ b/api/urls.py @@ -6,5 +6,6 @@ from . import views rest_router = routers.DefaultRouter() rest_router.register(r'ducks', views.DuckViewSet) +rest_router.register(r'competences', views.CompetenceViewSet) urlpatterns = rest_router.urls diff --git a/api/views.py b/api/views.py index 4072c2a..c4dfe7f 100644 --- a/api/views.py +++ b/api/views.py @@ -7,8 +7,9 @@ from rest_framework.decorators import detail_route, list_route from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response -from .serializers import DuckSerializer, CompetenceSerializer -from booking.models import Duck, Competence, Booking +from .serializers import DuckSerializer, CompetenceSerializer, \ + DuckCompetenceSerializer +from booking.models import Duck, Competence, Booking, DuckCompetence class DuckViewSet(viewsets.ModelViewSet): serializer_class = DuckSerializer @@ -30,7 +31,7 @@ class DuckViewSet(viewsets.ModelViewSet): comp_level = 0 # Check if the duck has the requested competence - dcomp_list = duck.duckcompetence_set.filter(comp=competence) + dcomp_list = duck.competences.filter(comp=competence) if len(dcomp_list) < 1: comp_level = 0 @@ -54,3 +55,7 @@ class DuckViewSet(viewsets.ModelViewSet): @list_route(methods=['post'], permission_classes=[IsAuthenticated]) def donate(self, request): return Response({'Woot!'}) + +class CompetenceViewSet(viewsets.ModelViewSet): + serializer_class = CompetenceSerializer + queryset = Competence.objects.all()