Skip to content

Commit

Permalink
Merge pull request hasadna#50 from danzat/master
Browse files Browse the repository at this point in the history
Add tastypie wrappers to the Event model for APIv2
  • Loading branch information
habeanf committed Jan 3, 2013
2 parents 4b076ba + 93de269 commit 7bae645
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apis/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from agendas.api import AgendaResource, AgendaTodoResource
from committees.api import CommitteeResource, CommitteeMeetingResource, ProtocolPartResource
from auxiliary.api import PostResource, TagResource
from events.api import EventResource

v2_api = Api(api_name='v2')

Expand All @@ -29,3 +30,4 @@
v2_api.register(ProtocolPartResource())
v2_api.register(PostResource())
v2_api.register(TagResource())
v2_api.register(EventResource())
33 changes: 33 additions & 0 deletions events/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'''
Api for the events app
'''
import datetime
from models import Event
from tastypie.utils import trailing_slash
from apis.resources.base import BaseResource
from django.conf.urls.defaults import url

class EventResource(BaseResource):
class Meta:
queryset = Event.objects.all()
allowed_methods = ['get']

def override_urls(self):
return [
url(r'^(?P<resource_name>%s)/?$' % self._meta.resource_name,
self.wrap_view('get_future_events'),
name = 'api-get-future-events'),
url(r'^(?P<resource_name>%s)/(?P<event_id>\d+)/?$' % self._meta.resource_name,
self.wrap_view('get_specific_event'),
name = 'api-get-specific-event')
]

def get_future_events(self, request, **kwargs):
events = Event.objects.filter(when__gte = datetime.datetime.now())
bundles = [self.build_bundle(obj = event, request = request) for event in events]
return self.create_response(request, map(self.full_dehydrate, bundles))

def get_specific_event(self, request, **kwargs):
events = Event.objects.filter(id = kwargs['event_id'])
bundles = [self.build_bundle(obj = event, request = request) for event in events]
return self.create_response(request, self.full_dehydrate(bundles[0]))
31 changes: 30 additions & 1 deletion events/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import vobject

from django.test import TestCase
from django.core.urlresolvers import reverse
from django.utils import translation
from django.utils import simplejson as json
from django.core.urlresolvers import reverse

from models import Event

Expand Down Expand Up @@ -64,6 +65,34 @@ def testIcalenderGuessedEndWarning(self):
'ev3\n\noknesset warnings:\nno end date data - guessed it to be 2 hours after start')
translation.deactivate()

def testAPIv2FutureEventsConsistency(self):
"""
Test that APIv2 and APIv1 fetch the same future events.
"""
res_v1 = self.client.get('/api/event/')
self.assertEqual(res_v1.status_code, 200)
res_v2 = self.client.get('/api/v2/event/', format = 'json')
self.assertEqual(res_v2.status_code, 200)
ids_v1 = set(x['what'] for x in json.loads(res_v1.content))
ids_v2 = set(x['what'] for x in json.loads(res_v2.content))
self.assertEqual(ids_v1, ids_v2)

def testAPIv2Identity(self):
"""
Test that APIv2 and APIv1 return the same data for each event.
"""
for event_id in [self.ev1.id, self.ev2.id, self.ev3.id]:
res_v1 = self.client.get('/api/event/%d/' % event_id)
self.assertEqual(res_v1.status_code, 200)
res_v2 = self.client.get('/api/v2/event/%d/' % event_id, format = 'json')
self.assertEqual(res_v2.status_code, 200)
event_v1 = json.loads(res_v1.content)
event_v2 = json.loads(res_v2.content)
self.assertEqual(event_v1['what'], event_v2['what'])
# APIv2 return a more "accurate" result, so I need to trunk it
self.assertEqual(event_v1['when'], event_v2['when'][:-3])
self.assertEqual(event_v1['where'], event_v2['where'])

def tearDown(self):
self.ev1.delete()
self.ev2.delete()
Expand Down

0 comments on commit 7bae645

Please sign in to comment.