Permalink
Browse files

Ported filtering behaviour for Files and Images.

  • Loading branch information...
1 parent b83e991 commit 81d5cdd822045bb7fb9eb28f392882c5c18882ab @markotibold markotibold committed Nov 12, 2012
Showing with 74 additions and 95 deletions.
  1. +74 −95 fiber/rest_api/views.py
View
@@ -1,31 +1,23 @@
from django.db.models import Q
-from djangorestframework.permissions import IsAuthenticated
-from djangorestframework.views import ListOrCreateModelView
-from djangorestframework.mixins import PaginatorMixin
-from djangorestframework.status import HTTP_400_BAD_REQUEST
-from djangorestframework.response import ErrorResponse
-from djangorestframework.renderers import JSONRenderer, DocumentingHTMLRenderer
-
-from fiber.models import Page, PageContentItem, ContentItem, File, Image
-from fiber.app_settings import API_RENDER_HTML, PERMISSION_CLASS
-from fiber.utils import class_loader
-
-PERMISSIONS = class_loader.load_class(PERMISSION_CLASS)
-
-API_RENDERERS = (JSONRenderer, )
-if API_RENDER_HTML:
- API_RENDERERS = (JSONRenderer, DocumentingHTMLRenderer)
-
from rest_framework import generics, renderers
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework import views
from rest_framework import status
+from fiber.models import Page, PageContentItem, ContentItem, File, Image
+from fiber.app_settings import API_RENDER_HTML, PERMISSION_CLASS
+from fiber.utils import class_loader
+
from .serializers import PageSerializer, MovePageSerializer, PageContentItemSerializer, MovePageContentItemSerializer, ContentItemSerializer, FileSerializer, ImageSerializer, FiberPaginationSerializer
+PERMISSIONS = class_loader.load_class(PERMISSION_CLASS)
+
+API_RENDERERS = (renderers.JSONRenderer, )
+if API_RENDER_HTML:
+ API_RENDERERS = (renderers.DocumentingHTMLRenderer, renderers.JSONRenderer)
_403_FORBIDDEN_RESPONSE = Response(
{
@@ -45,18 +37,18 @@ def create(self, request, *args, **kwargs):
class PageList(FiberListCreateAPIView):
model = Page
serializer_class = PageSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class PageDetail(generics.RetrieveUpdateDestroyAPIView):
model = Page
serializer_class = PageSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class MovePageView(views.APIView):
-
serializer_class = MovePageSerializer
+ renderer_classes = API_RENDERERS
def get(self, request, pk, format=None):
if not PERMISSIONS.can_move_page(request.user, Page.objects.get(id=pk)):
@@ -76,18 +68,18 @@ def put(self, request, pk, format=None):
class PageContentItemList(FiberListCreateAPIView):
model = PageContentItem
serializer_class = PageContentItemSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class PageContentItemDetail(generics.RetrieveUpdateDestroyAPIView):
model = PageContentItem
serializer_class = PageContentItemSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class MovePageContentItemView(views.APIView):
-
serializer_class = MovePageContentItemSerializer
+ renderer_classes = API_RENDERERS
def get(self, request, pk, format=None):
if not PERMISSIONS.can_edit(request.user, Page.objects.get(page_content_items__id=pk)):
@@ -107,129 +99,116 @@ def put(self, request, pk, format=None):
class ContentItemList(FiberListCreateAPIView):
model = ContentItem
serializer_class = ContentItemSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class ContentItemDetail(generics.RetrieveUpdateDestroyAPIView):
model = ContentItem
serializer_class = ContentItemSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
class FileList(FiberListCreateAPIView):
model = File
serializer_class = FileSerializer
- renderer_classes = (renderers.JSONRenderer, )
+ renderer_classes = API_RENDERERS
pagination_serializer_class = FiberPaginationSerializer
paginate_by = 5
-
-class FileDetail(generics.RetrieveUpdateDestroyAPIView):
- model = File
- serializer_class = FileSerializer
- renderer_classes = (renderers.JSONRenderer, )
-
-
-class ImageList(FiberListCreateAPIView):
- model = Image
- serializer_class = ImageSerializer
- renderer_classes = (renderers.JSONRenderer, )
- pagination_serializer_class = FiberPaginationSerializer
- paginate_by = 5
-
-
-class ImageDetail(generics.RetrieveUpdateDestroyAPIView):
- model = Image
- serializer_class = ImageSerializer
- renderer_classes = (renderers.JSONRenderer, )
-
-
-@api_view(('GET',))
-def api_root(request, format='None'):
- """
- This is the entry point for the API.
- """
- return Response({
- 'pages': reverse('page-list', request=request),
- 'pagetree': reverse('pagetree', request=request),
- 'page content items': reverse('pagecontentitem-list', request=request),
- 'content items': reverse('contentitem-list', request=request),
- 'images': reverse('image-list', request=request),
- 'files': reverse('file-list', request=request),
- })
-
-
-class PageTree(views.APIView):
-
- def get(self, request, format=None):
- """
- Provide jqTree data for the PageSelect dialog.
- """
- return Response(Page.objects.create_jqtree_data(request.user))
-
-
-class ListView(ListOrCreateModelView):
-
- permissions = (IsAuthenticated, )
- renderers = API_RENDERERS
-
-
-class PaginatedListView(PaginatorMixin, ListView):
-
- limit = 5
+ orderable_fields = ('filename', 'updated')
def check_fields(self, order_by):
if order_by not in self.orderable_fields:
- raise ErrorResponse(status=HTTP_400_BAD_REQUEST, content="Can not order by the passed value.")
-
-
-class FileListView(PaginatedListView):
-
- orderable_fields = ('filename', 'updated')
+ return Response("Can not order by the passed value.", status=status.HTTP_400_BAD_REQUEST)
def get_queryset(self, *args, **kwargs):
- qs = super(FileListView, self).get_queryset(*args, **kwargs)
+ qs = super(FileList, self).get_queryset(*args, **kwargs)
qs = PERMISSIONS.filter_files(self.request.user, qs)
- search = self.request.GET.get('search', None)
+ search = self.request.QUERY_PARAMS.get('search')
+
if search:
qs = qs.filter(file__icontains=search)
- order_by = self.request.GET.get('order_by', 'updated')
+ order_by = self.request.QUERY_PARAMS.get('order_by', 'updated')
self.check_fields(order_by)
if order_by == 'filename':
order_by = 'file'
- sort_order = self.request.GET.get('sortorder', 'asc')
+ sort_order = self.request.QUERY_PARAMS.get('sortorder', 'asc')
qs = qs.order_by('%s%s' % ('-' if sort_order != 'asc' else '', order_by))
return qs
-class ImageListView(PaginatedListView):
+class FileDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = File
+ serializer_class = FileSerializer
+ renderer_classes = API_RENDERERS
+
+class ImageList(FiberListCreateAPIView):
+ model = Image
+ serializer_class = ImageSerializer
+ renderer_classes = API_RENDERERS
+ pagination_serializer_class = FiberPaginationSerializer
+ paginate_by = 5
orderable_fields = ('filename', 'size', 'updated')
+ def check_fields(self, order_by):
+ if order_by not in self.orderable_fields:
+ return Response("Can not order by the passed value.", status=status.HTTP_400_BAD_REQUEST)
+
def get_queryset(self, *args, **kwargs):
- qs = super(ImageListView, self).get_queryset(*args, **kwargs)
+ qs = super(ImageList, self).get_queryset(*args, **kwargs)
qs = PERMISSIONS.filter_images(self.request.user, qs)
- search = self.request.GET.get('search', None)
+ search = self.request.QUERY_PARAMS.get('search')
if search:
# TODO: image_icontains searches in the entire path, it should only search in the filename (use iregex for this?)
qs = qs.filter(Q(image__icontains=search) | Q(title__icontains=search) | Q(width__icontains=search) | Q(height__icontains=search))
- order_by = self.request.GET.get('order_by', 'updated')
+ order_by = self.request.QUERY_PARAMS.get('order_by', 'updated')
self.check_fields(order_by)
if order_by == 'filename':
order_by = 'image'
elif order_by == 'size':
order_by = 'width'
- sort_order = self.request.GET.get('sortorder', 'asc')
+ sort_order = self.request.QUERY_PARAMS.get('sortorder', 'asc')
qs = qs.order_by('%s%s' % ('-' if sort_order != 'asc' else '', order_by))
return qs
+
+class ImageDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = Image
+ serializer_class = ImageSerializer
+ renderer_classes = API_RENDERERS
+
+
+@api_view(('GET',))
+def api_root(request, format='None'):
+ """
+ This is the entry point for the API.
+ """
+ return Response({
+ 'pages': reverse('page-list', request=request),
+ 'pagetree': reverse('pagetree', request=request),
+ 'page content items': reverse('pagecontentitem-list', request=request),
+ 'content items': reverse('contentitem-list', request=request),
+ 'images': reverse('image-list', request=request),
+ 'files': reverse('file-list', request=request),
+ })
+
+
+class PageTree(views.APIView):
+ renderer_classes = API_RENDERERS
+
+ def get(self, request, format=None):
+ """
+ Provide jqTree data for the PageSelect dialog.
+ """
+ return Response(Page.objects.create_jqtree_data(request.user))

0 comments on commit 81d5cdd

Please sign in to comment.