Add competence list to duck details
This commit is contained in:
parent
6aa0e50193
commit
2bfedffad4
@ -2,7 +2,7 @@
|
|||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from booking.models import Duck
|
from booking.models import Duck, Competence, DuckCompetence
|
||||||
|
|
||||||
class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@ -11,6 +11,9 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
|||||||
|
|
||||||
self.url_namespace = self.Meta.url_namespace
|
self.url_namespace = self.Meta.url_namespace
|
||||||
|
|
||||||
|
self.url_namespace = kwargs.pop('url_namespace',
|
||||||
|
self.url_namespace)
|
||||||
|
|
||||||
if not self.url_namespace.endswith(':'):
|
if not self.url_namespace.endswith(':'):
|
||||||
self.url_namespace += ':'
|
self.url_namespace += ':'
|
||||||
|
|
||||||
@ -28,8 +31,24 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
|||||||
|
|
||||||
return field_class, field_kwargs
|
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):
|
class DuckSerializer(NamespacedSerializer):
|
||||||
|
competences = DuckCompetenceSerializer(many=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
url_namespace = 'api'
|
url_namespace = 'api'
|
||||||
model = Duck
|
model = Duck
|
||||||
fields = ('url', 'name', 'color',)
|
fields = ('url', 'name', 'color', 'competences',)
|
||||||
|
28
api/tests.py
28
api/tests.py
@ -18,36 +18,31 @@ class DuckClassTest(WebTest):
|
|||||||
self.user = User.objects.create_user(username='test',
|
self.user = User.objects.create_user(username='test',
|
||||||
password='test')
|
password='test')
|
||||||
|
|
||||||
spec = Species(name = 'duck')
|
spec = Species.objects.create(name = 'duck')
|
||||||
spec.save()
|
|
||||||
|
|
||||||
loc = Location(name = 'temp')
|
loc = Location.objects.create(name = 'temp')
|
||||||
loc.save()
|
|
||||||
|
|
||||||
self.comp_bad = Competence(name = 'test1', added_by = self.user)
|
self.comp_bad = Competence.objects.create(name = 'test1',
|
||||||
self.comp_bad.save()
|
|
||||||
|
|
||||||
self.comp_good = Competence(name = 'test2',
|
|
||||||
added_by = self.user)
|
added_by = self.user)
|
||||||
self.comp_good.save()
|
|
||||||
|
|
||||||
self.duck = Duck(species = spec,
|
self.comp_good = Competence.objects.create(name = 'test2',
|
||||||
|
added_by = self.user)
|
||||||
|
|
||||||
|
self.duck = Duck.objects.create(species = spec,
|
||||||
|
name='test duck',
|
||||||
location = loc,
|
location = loc,
|
||||||
donated_by = self.user,
|
donated_by = self.user,
|
||||||
color='123456')
|
color='123456')
|
||||||
self.duck.save()
|
|
||||||
|
|
||||||
dcomp = DuckCompetence(duck = self.duck,
|
DuckCompetence.objects.create(duck = self.duck,
|
||||||
comp = self.comp_bad,
|
comp = self.comp_bad,
|
||||||
up_minutes = bad_minutes,
|
up_minutes = bad_minutes,
|
||||||
down_minutes = 0)
|
down_minutes = 0)
|
||||||
dcomp.save()
|
|
||||||
|
|
||||||
dcomp = DuckCompetence(duck = self.duck,
|
DuckCompetence.objects.create(duck = self.duck,
|
||||||
comp = self.comp_good,
|
comp = self.comp_good,
|
||||||
up_minutes = good_minutes,
|
up_minutes = good_minutes,
|
||||||
down_minutes = 0)
|
down_minutes = 0)
|
||||||
dcomp.save()
|
|
||||||
|
|
||||||
def test_book_nonlogged(self):
|
def test_book_nonlogged(self):
|
||||||
page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True)
|
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)
|
self.assertEqual(200, page.status_code)
|
||||||
|
|
||||||
page_json = json.loads(page.content)
|
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.assertEquals('123456', page_json['color'])
|
||||||
|
self.assertEqual(2, len(page_json['competences']))
|
||||||
|
@ -6,5 +6,6 @@ from . import views
|
|||||||
|
|
||||||
rest_router = routers.DefaultRouter()
|
rest_router = routers.DefaultRouter()
|
||||||
rest_router.register(r'ducks', views.DuckViewSet)
|
rest_router.register(r'ducks', views.DuckViewSet)
|
||||||
|
rest_router.register(r'competences', views.CompetenceViewSet)
|
||||||
|
|
||||||
urlpatterns = rest_router.urls
|
urlpatterns = rest_router.urls
|
||||||
|
11
api/views.py
11
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.permissions import IsAuthenticated
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from .serializers import DuckSerializer, CompetenceSerializer
|
from .serializers import DuckSerializer, CompetenceSerializer, \
|
||||||
from booking.models import Duck, Competence, Booking
|
DuckCompetenceSerializer
|
||||||
|
from booking.models import Duck, Competence, Booking, DuckCompetence
|
||||||
|
|
||||||
class DuckViewSet(viewsets.ModelViewSet):
|
class DuckViewSet(viewsets.ModelViewSet):
|
||||||
serializer_class = DuckSerializer
|
serializer_class = DuckSerializer
|
||||||
@ -30,7 +31,7 @@ class DuckViewSet(viewsets.ModelViewSet):
|
|||||||
comp_level = 0
|
comp_level = 0
|
||||||
|
|
||||||
# Check if the duck has the requested competence
|
# 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:
|
if len(dcomp_list) < 1:
|
||||||
comp_level = 0
|
comp_level = 0
|
||||||
@ -54,3 +55,7 @@ class DuckViewSet(viewsets.ModelViewSet):
|
|||||||
@list_route(methods=['post'], permission_classes=[IsAuthenticated])
|
@list_route(methods=['post'], permission_classes=[IsAuthenticated])
|
||||||
def donate(self, request):
|
def donate(self, request):
|
||||||
return Response({'Woot!'})
|
return Response({'Woot!'})
|
||||||
|
|
||||||
|
class CompetenceViewSet(viewsets.ModelViewSet):
|
||||||
|
serializer_class = CompetenceSerializer
|
||||||
|
queryset = Competence.objects.all()
|
||||||
|
Loading…
Reference in New Issue
Block a user