Commits

Jesper Nøhr committed ad85279

Automated commit message

  • Participants
  • Parent commits 53a1a6d

Comments (0)

Files changed (1)

 <</float>>
 
 <<clear>>
+
+Fully functional example
+
+urls.py:
+
+{{{
+#!python
+
+from django.conf.urls.defaults import *
+from piston.resource import Resource
+from piston.authentication import HttpBasicAuthentication
+
+from myapp.handlers import BlogPostHandler, ArbitraryDataHandler
+
+auth = HttpBasicAuthentication(realm="My Realm")
+ad = { 'authentication': auth }
+
+blogpost_resource = Resource(handler=BlogPostHandler, **ad)
+arbitrary_resource = Resource(handler=ArbitraryDataHandler, **ad)
+
+urlpatterns += patterns('',
+    url(r'^posts/(?P<post_slug>[^/]+)/$', blogpost_resource), 
+    url(r'^other/(?P<username>[^/]+)/(?P<data>.+)/$', arbitrary_resource), 
+)
+}}}
+
+And as for handlers.py:
+
+{{{
+#!python
+
+import re
+
+from piston.handler import BaseHandler
+from piston.utils import rc, throttle
+
+from myapp.models import Blogpost
+
+class BlogPostHandler(BaseHandler):
+    allowed_methods = ('GET', 'PUT', 'DELETE')
+    include = ('title', 'content', ('author', ('username', 'first_name')))
+    exclude = ('id', re.compile(r'^private_'))
+    model = Blogpost
+
+    def read(self, request, post_slug):
+        post = Blogpost.objects.get(slug=post_slug)
+        return post
+
+    @throttle(5, 10) # allow 5 times in 10 minutes
+    def update(self, request, post_slug):
+        post = Blogpost.objects.get(slug=post_slug)
+
+        post.title = request.PUT.get('title')
+        post.save()
+
+        return post
+
+    def delete(self, request, post_slug):
+        post = Blogpost.objects.get(slug=post_slug)
+
+        if not request.user == post.author:
+            return rc.FORBIDDEN # returns HTTP 401
+
+        post.delete()
+
+        return rc.DELETED # returns HTTP 204
+
+class ArbitraryDataHandler(BaseHandler):
+
+
+}}}