Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[Django] RESTful API

[Django] RESTful API

Sébastien Fievet

February 11, 2011
Tweet

More Decks by Sébastien Fievet

Other Decks in Programming

Transcript

  1. 1

  2. from django.contrib.auth.models import User from tastypie import fields from tastypie.resources

    import ModelResource from blog.models import Entry class UserResource(ModelResource): class Meta: queryset = User.objects.all() resource_name = 'user' class EntryResource(ModelResource): user = fields.ForeignKey(UserResource, 'user') class Meta: queryset = Entry.objects.all() resource_name = 'entry'
  3. 2

  4. from webmachine import Resource from webmachine.helpers.serialize import JSONSerializer class EntryResource(Resource):

    fields = None exclude = None def __init__(self): self.json = JSONSerializer(fields=self.fields, exclude=self.exclude) super(BaseResource, self).__init__() def content_types_provided(self, request, response): return ( ('application/json', self.to_json), ) def allowed_methods(self, request, response): return ('GET', ) def get_query_set(self): return Entry.objects.all() def to_json(self, request, response): return self.json.serialize(self.get_query_set())
  5. 3

  6. from django.conf.urls.defaults import url from tastypie.resources import ModelResource class SuffixedResource(ModelResource):

    class Meta: allowed_methods = ('get', ) def determine_format(self, request): url_parts = request.path.rsplit(".", 1) try: format = url_parts[1] if format in self._meta.serializer.formats: return self._meta.serializer.get_mime_for_format(format) except IndexError: pass return super(SuffixedResource, self).determine_format(request) def format_suffix(self): return r'\.(%s)' % '|'.join(self._meta.serializer.formats) def override_urls(self): # def base_urls(self): return [ url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, self.format_suffix()), self.wrap_view('dispatch_list'), name="api_dispatch_list"), url(r"^(?P<resource_name>%s)/schema%s$" % (self._meta.resource_name, self.format_suffix()), self.wrap_view('get_schema'), name="api_get_schema"), url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)%s$" % (self._meta.resource_name, self.format_suffix()), self.wrap_view('dispatch_detail'), name="api_dispatch_detail"), ]
  7. from django.conf.urls.defaults import patterns, url from webmachine import Resource class

    SuffixedResource(Resource): def content_types_provided(self, request, response): return ( ('application/json', self.to_json), ) def format_suffix_accepted(self, request, response): return ( ("json", "application/json"), ) def allowed_methods(self, request, response): return ('GET', ) def get_urls(self): regexp = getattr(self, "url_regexp") or r'^$' # Add format suffix formats = dict(self.format_suffix_accepted(None, None)) regexp = regexp[:-1] + r'(.%s)?' % '|'.join(formats.keys()) + regexp[-1] # -- urlpatterns = patterns('', url(regexp, self, name="%s_index" % self.__class__.__name__), ) return urlpatterns
  8. Recap: You have the choice Choosing a backend is a

    trade-off Fork it, toy it, contribute it