Pylint happiness!
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Serializers for the Duck Booking Tool API
|
||||
"""
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from rest_framework import serializers
|
||||
|
||||
from booking.models import Duck, Competence, DuckCompetence
|
||||
|
||||
class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
||||
"""
|
||||
HyperlinkedModelSerializer with URL namespace support
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if not hasattr(self.Meta, 'url_namespace') or self.Meta.url_namespace is None:
|
||||
raise ImproperlyConfigured("namespace must be set!")
|
||||
@@ -17,7 +25,7 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
||||
if not self.url_namespace.endswith(':'):
|
||||
self.url_namespace += ':'
|
||||
|
||||
return super(NamespacedSerializer, self).__init__(*args, **kwargs)
|
||||
super(NamespacedSerializer, self).__init__(*args, **kwargs)
|
||||
|
||||
def build_url_field(self, field_name, model_class):
|
||||
field_class, field_kwargs = super(NamespacedSerializer, self) \
|
||||
@@ -32,12 +40,20 @@ class NamespacedSerializer(serializers.HyperlinkedModelSerializer):
|
||||
return field_class, field_kwargs
|
||||
|
||||
class CompetenceSerializer(NamespacedSerializer):
|
||||
"""
|
||||
Serializer for Competence objects
|
||||
"""
|
||||
|
||||
class Meta:
|
||||
url_namespace = 'api'
|
||||
model = Competence
|
||||
fields = ('url', 'name',)
|
||||
|
||||
class DuckCompetenceSerializer(NamespacedSerializer):
|
||||
"""
|
||||
Serializer for DuckCompetence objects
|
||||
"""
|
||||
|
||||
comp = CompetenceSerializer()
|
||||
|
||||
class Meta:
|
||||
@@ -46,6 +62,10 @@ class DuckCompetenceSerializer(NamespacedSerializer):
|
||||
fields = ('comp', 'up_minutes', 'down_minutes',)
|
||||
|
||||
class DuckSerializer(NamespacedSerializer):
|
||||
"""
|
||||
Serializer for Duck objects
|
||||
"""
|
||||
|
||||
competences = DuckCompetenceSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
|
36
api/tests.py
36
api/tests.py
@@ -1,5 +1,8 @@
|
||||
# -*- coding: utf-8
|
||||
from django.test import TestCase, Client
|
||||
"""
|
||||
Test cases for API calls
|
||||
"""
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.conf import settings
|
||||
from django_webtest import WebTest
|
||||
@@ -10,6 +13,10 @@ from booking.ducklevel import level_to_up_minutes
|
||||
from booking.models import Species, Location, Duck, Competence, DuckCompetence
|
||||
|
||||
class DuckClassTest(WebTest):
|
||||
"""
|
||||
Test case for duck related API calls
|
||||
"""
|
||||
|
||||
csrf_checks = False
|
||||
|
||||
def setUp(self):
|
||||
@@ -46,10 +53,18 @@ class DuckClassTest(WebTest):
|
||||
down_minutes=0)
|
||||
|
||||
def test_book_nonlogged(self):
|
||||
"""
|
||||
Test booking without logging in
|
||||
"""
|
||||
|
||||
page = self.app.post('/api/v1/ducks/1/book/', expect_errors=True)
|
||||
self.assertEqual(page.status_code, 403)
|
||||
|
||||
def test_book_nonexist(self):
|
||||
"""
|
||||
Test booking a non-existing duck
|
||||
"""
|
||||
|
||||
# Try to book a non-existing duck
|
||||
page = self.app.post(
|
||||
'/api/v1/ducks/9999/book/',
|
||||
@@ -71,6 +86,10 @@ class DuckClassTest(WebTest):
|
||||
self.assertEqual(404, page.status_code)
|
||||
|
||||
def test_book_warn(self):
|
||||
"""
|
||||
Test duck booking for a competence the duck is not good at
|
||||
"""
|
||||
|
||||
url = '/api/v1/ducks/%d/book/' % self.duck.pk
|
||||
comp_none = Competence.objects.create(name='test3',
|
||||
added_by=self.user)
|
||||
@@ -107,6 +126,10 @@ class DuckClassTest(WebTest):
|
||||
self.assertEquals(page_json['status'], 'ok')
|
||||
|
||||
def test_book_good(self):
|
||||
"""
|
||||
Test duck booking for a competence the duck is good at
|
||||
"""
|
||||
|
||||
test_data = {
|
||||
"competence": self.comp_good.pk
|
||||
}
|
||||
@@ -127,6 +150,10 @@ class DuckClassTest(WebTest):
|
||||
self.assertEqual('already-booked', page_json['status'])
|
||||
|
||||
def test_duck_donation(self):
|
||||
"""
|
||||
Test duck donating functionality
|
||||
"""
|
||||
|
||||
# Duck donation should not be allowed without logging in
|
||||
page = self.app.get('/api/v1/ducks/donate/', expect_errors=True)
|
||||
self.assertEquals(page.status_code, 403)
|
||||
@@ -135,16 +162,19 @@ class DuckClassTest(WebTest):
|
||||
page = self.app.post('/api/v1/ducks/donate/', expect_errors=True)
|
||||
self.assertEquals(page.status_code, 403)
|
||||
|
||||
page = self.app.post(
|
||||
self.app.post(
|
||||
'/api/v1/ducks/donate/',
|
||||
params={
|
||||
'species': 1,
|
||||
'color': '123456',
|
||||
},
|
||||
user=self.user)
|
||||
page_json = json.loads(page.content)
|
||||
|
||||
def test_duck_details(self):
|
||||
"""
|
||||
Test duck details view
|
||||
"""
|
||||
|
||||
url = '/api/v1/ducks/%d/' % self.duck.pk
|
||||
page = self.app.get(url)
|
||||
self.assertEqual(200, page.status_code)
|
||||
|
@@ -1,4 +1,7 @@
|
||||
from django.conf.urls import patterns, url, include
|
||||
# -*- coding: utf-8
|
||||
"""
|
||||
URL definitions for version 1 of the Duck Booking Tool API
|
||||
"""
|
||||
|
||||
from rest_framework import routers
|
||||
|
||||
|
26
api/views.py
26
api/views.py
@@ -1,22 +1,32 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Views for the Duck Booking Tool API
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework import viewsets
|
||||
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, \
|
||||
DuckCompetenceSerializer
|
||||
from booking.models import Duck, Competence, Booking, DuckCompetence
|
||||
from .serializers import DuckSerializer, CompetenceSerializer
|
||||
from booking.models import Duck, Competence, Booking
|
||||
|
||||
class DuckViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
View set for duck handling
|
||||
"""
|
||||
|
||||
serializer_class = DuckSerializer
|
||||
queryset = Duck.objects.all()
|
||||
|
||||
@detail_route(methods=['post'], permission_classes=[IsAuthenticated])
|
||||
def book(self, request, pk=None):
|
||||
"""
|
||||
API call to book a duck
|
||||
"""
|
||||
|
||||
duck = self.get_object()
|
||||
competence = get_object_or_404(Competence, pk=request.data['competence'])
|
||||
force = request.data.get('force', False)
|
||||
@@ -54,8 +64,16 @@ class DuckViewSet(viewsets.ModelViewSet):
|
||||
|
||||
@list_route(methods=['post'], permission_classes=[IsAuthenticated])
|
||||
def donate(self, request):
|
||||
"""
|
||||
API call to donate a new duck
|
||||
"""
|
||||
|
||||
return Response({'Woot!'})
|
||||
|
||||
class CompetenceViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
View set for competence handling
|
||||
"""
|
||||
|
||||
serializer_class = CompetenceSerializer
|
||||
queryset = Competence.objects.all()
|
||||
|
Reference in New Issue
Block a user