diff --git a/api/templates/api/duck_comp_list.json b/api/templates/api/duck_comp_list.json new file mode 100644 index 0000000..cc57934 --- /dev/null +++ b/api/templates/api/duck_comp_list.json @@ -0,0 +1,8 @@ +[ + {% for comp in duck.duckcompetence_set.all %} + { + "name": "{{ comp.comp.name }}", + "level": {{ comp.level }} + }{% if not forloop.last %},{% endif %} + {% endfor %} +] diff --git a/api/tests.py b/api/tests.py index 66d1f29..bbafa6a 100644 --- a/api/tests.py +++ b/api/tests.py @@ -1,5 +1,8 @@ from django.test import TestCase, Client from django.core.urlresolvers import reverse +from django.contrib.auth.models import User + +from booking.models import Species, Location, Duck, Competence, DuckCompetence class ReverseTest(TestCase): def setUp(self): @@ -8,3 +11,31 @@ class ReverseTest(TestCase): def test_reverse_js(self): response = self.client.get(reverse('api:js_reverse')) self.assertEqual(response.status_code, 200) + +class ApiTest(TestCase): + def setUp(self): + self.client = Client() + + species = Species(name = 'duck') + species.save() + + loc = Location(name = 'test') + loc.save() + + user = User() + user.save() + + self.duck = Duck(pk = 1, species = species, location = loc, donated_by = user) + self.duck.save() + + comp = Competence(name = 'test', added_by = user) + comp.save() + + duckcomp = DuckCompetence(duck = self.duck, comp = comp) + duckcomp.save() + + def test_duck_comp_list(self): + response = self.client.get('/api/duck/1/competence.json') + self.assertEqual(response.status_code, 200) + + self.assertEqual(len(response.context['duck'].duckcompetence_set.all()), 1) diff --git a/api/urls.py b/api/urls.py index b6b6367..c0cf827 100644 --- a/api/urls.py +++ b/api/urls.py @@ -2,7 +2,10 @@ from django.conf.urls import patterns, url from django.views.decorators.cache import cache_page from django_js_reverse.views import urls_js +from . import views + urlpatterns = patterns( '', url(r'^reverse.js$', cache_page(3600)(urls_js), name = 'js_reverse'), + url(r'^duck/(?P\d+)/competence.json$', views.duck_comp_list) ) diff --git a/api/views.py b/api/views.py new file mode 100644 index 0000000..16632a1 --- /dev/null +++ b/api/views.py @@ -0,0 +1,12 @@ +from django.shortcuts import render, get_object_or_404 + +from booking.models import Duck + +def duck_comp_list(request, duck_id): + duck = get_object_or_404(Duck, pk = duck_id) + + context = { + 'duck': duck + } + + return render(request, 'api/duck_comp_list.json', context) diff --git a/duckbook/settings.py b/duckbook/settings.py index e60867e..7216f42 100644 --- a/duckbook/settings.py +++ b/duckbook/settings.py @@ -39,6 +39,7 @@ INSTALLED_APPS = ( 'django_js_reverse', 'accounts', 'booking', + 'api', ) MIDDLEWARE_CLASSES = (