from functools import wraps
from django.shortcuts import get_object_or_404
-from django.contrib.auth.decorators import login_required, permission_required
-from piston.handler import BaseHandler
+from piston.handler import AnonymousBaseHandler, BaseHandler
from piston.utils import rc, validate
from catalogue.models import Book
from catalogue.forms import BookImportForm
-def method_decorator(function_decorator):
- """
- Turns a function(*args, **kwargs) decorator into an
- equivalent decorator for method(self, *args, **kwargs).
- """
- @wraps(function_decorator)
- def decorator(method):
- @wraps(method)
- def decorated_method(self, *args, **kwargs):
- def method_as_function(*fargs, **fkwargs):
- return method(self, *fargs, **fkwargs)
- return function_decorator(method_as_function)(*args, **kwargs)
- return decorated_method
- return decorator
-
-class BookHandler(BaseHandler):
+class AnonymousBooksHandler(AnonymousBaseHandler):
model = Book
fields = ('slug', 'title')
else:
return Book.objects.all()
- @method_decorator(permission_required('catalogue.add_book'))
+
+class BooksHandler(BaseHandler):
+ model = Book
+ fields = ('slug', 'title')
+ anonymous = AnonymousBooksHandler
+
def create(self, request):
+ if not request.user.has_perm('catalogue.add_book'):
+ return rc.FORBIDDEN
+
form = BookImportForm(request.POST, request.FILES)
if form.is_valid():
form.save()
from piston.resource import Resource
from piston.authentication import OAuthAuthentication
-from api.handlers import BookHandler
+from api.handlers import BooksHandler
auth = OAuthAuthentication(realm="Wolne Lektury")
-book_resource = Resource(handler=BookHandler, authentication=auth)
+book_resource = Resource(handler=BooksHandler, authentication=auth)
urlpatterns = patterns('',