Created
August 27, 2009 13:43
-
-
Save maxp/176290 to your computer and use it in GitHub Desktop.
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
""" | |
Example usage: | |
class ArticleView(RestView): | |
def GET(request, article_id): | |
return render_to_response("article.html", { | |
'article': get_object_or_404(Article, pk = article_id), | |
}) | |
def POST(request, article_id): | |
# Example logic only; should be using django.forms instead | |
article = get_object_or_404(Article, pk = article_id) | |
article.headline = request.POST['new_headline'] | |
article.body = request.POST['new_body'] | |
article.save() | |
return HttpResponseRedirect(request.path) | |
Then in your urls.py: | |
from my_views import ArticleView | |
urlpatterns = patterns('', | |
... | |
(r'^article/(\d+)/$', ArticleView()), | |
... | |
) | |
""" | |
from django.http import HttpResponse | |
import re | |
nonalpha_re = re.compile('[^A-Z]') | |
class RestView(object): | |
""" | |
Subclass this and add GET / POST / etc methods. | |
""" | |
allowed_methods = ('GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS') | |
def __call__(self, request, *args, **kwargs): | |
method = nonalpha_re.sub('', request.method.upper()) | |
if not method in self.allowed_methods or not hasattr(self, method): | |
return self.method_not_allowed(method) | |
return getattr(self, method)(request, *args, **kwargs) | |
def method_not_allowed(self, method): | |
response = HttpResponse('Method not allowed: %s' % method) | |
response.status_code = 405 | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment