Add duck_comp_list API call

This commit is contained in:
Gergely Polonkai 2014-12-23 09:56:04 +01:00 committed by Gergely Polonkai
parent 13c09bb856
commit 2a53041702
5 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,8 @@
[
{% for comp in duck.duckcompetence_set.all %}
{
"name": "{{ comp.comp.name }}",
"level": {{ comp.level }}
}{% if not forloop.last %},{% endif %}
{% endfor %}
]

View File

@ -1,5 +1,8 @@
from django.test import TestCase, Client from django.test import TestCase, Client
from django.core.urlresolvers import reverse 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): class ReverseTest(TestCase):
def setUp(self): def setUp(self):
@ -8,3 +11,31 @@ class ReverseTest(TestCase):
def test_reverse_js(self): def test_reverse_js(self):
response = self.client.get(reverse('api:js_reverse')) response = self.client.get(reverse('api:js_reverse'))
self.assertEqual(response.status_code, 200) 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)

View File

@ -2,7 +2,10 @@ from django.conf.urls import patterns, url
from django.views.decorators.cache import cache_page from django.views.decorators.cache import cache_page
from django_js_reverse.views import urls_js from django_js_reverse.views import urls_js
from . import views
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^reverse.js$', cache_page(3600)(urls_js), name = 'js_reverse'), url(r'^reverse.js$', cache_page(3600)(urls_js), name = 'js_reverse'),
url(r'^duck/(?P<duck_id>\d+)/competence.json$', views.duck_comp_list)
) )

12
api/views.py Normal file
View File

@ -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)

View File

@ -39,6 +39,7 @@ INSTALLED_APPS = (
'django_js_reverse', 'django_js_reverse',
'accounts', 'accounts',
'booking', 'booking',
'api',
) )
MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (