Source

dotcloud_starter / www / api / handlers.py

import re

from piston.handler import BaseHandler
from piston.utils import rc, throttle

from www.myapp.models import Poll

class PollHandler(BaseHandler):
    allowed_methods = ('GET', 'PUT', 'DELETE')
    fields = ('question', ('choices', ('choice', 'votes')))
    exclude = ('id', re.compile(r'^private_'))
    model = Poll

    @classmethod
    def content_size(self, blogpost):
        return len(blogpost.content)

    def read(self, request, poll_slug=None):
        if poll_slug:
            return Poll.objects.get(slug=poll_slug)
        else:
            return Poll.objects.all()

    @throttle(5, 10*60) # allow 5 times in 10 minutes
    def update(self, request, poll_slug):
        poll = Poll.objects.get(slug=poll_slug)

        poll.question = request.PUT.get('question')
        poll.save()

        return post

    def delete(self, request, poll_slug):
        poll = Poll.objects.get(slug=poll_slug)

        #if not request.user == post.author:
        #    return rc.FORBIDDEN # returns HTTP 401

        poll.delete()

        return rc.DELETED # returns HTTP 204

class ArbitraryDataHandler(BaseHandler):
    methods_allowed = ('GET',)

    def read(self, request, username, data):
        user = User.objects.get(username=username)

        return { 'user': user, 'data_length': len(data) }