Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv2 for events #144

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
from events.api import EventResource

v2_api = Api(api_name='v2')

Expand All @@ -28,3 +29,4 @@
v2_api.register(CommitteeMeetingResource())
v2_api.register(ProtocolPartResource())
v2_api.register(PostResource())
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