forked from hasadna/Open-Knesset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hasadna#50 from danzat/master
Add tastypie wrappers to the Event model for APIv2
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters