Permalink
Browse files

WIP, waiting for rf2 to support FileFields to be used with the ModelS…

  • Loading branch information...
1 parent ff15a40 commit a0f8cf6d096252990c2bb0d9f757a6656896f3c5 @markotibold markotibold committed Nov 11, 2012
Showing with 86 additions and 54 deletions.
  1. +6 −0 fiber/models.py
  2. +31 −2 fiber/rest_api/serializers.py
  3. +9 −49 fiber/rest_api/urls.py
  4. +40 −3 fiber/rest_api/views.py
View
@@ -309,6 +309,12 @@ def delete(self, *args, **kwargs):
def get_image_information(self):
self.width, self.height = get_image_dimensions(self.image) or (0, 0)
+ def get_filename(self):
+ return os.path.basename(self.image.name)
+
+ def get_size(self):
+ return '%s x %d' % (self.width, self.height)
+
class File(models.Model):
created = models.DateTimeField(_('created'), auto_now_add=True)
@@ -1,10 +1,39 @@
+from django.db import models
+
from rest_framework import serializers
-from fiber.models import Page
+from fiber.models import Page, PageContentItem, ContentItem, Image
-class PageSerializer(serializers.HyperlinkedModelSerializer):
+class PageSerializer(serializers.ModelSerializer):
move_url = serializers.HyperlinkedIdentityField(view_name='page-resource-instance-move')
page_url = serializers.Field(source='get_absolute_url')
class Meta:
model = Page
+
+
+class PageContentItemSerializer(serializers.ModelSerializer):
+ move_url = serializers.HyperlinkedIdentityField(view_name='page-content-item-resource-instance-move')
+ class Meta:
+ model = PageContentItem
+
+
+class ContentItemSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = ContentItem
+
+
+class ImageSerializer(serializers.ModelSerializer):
+ image_url = serializers.Field(source='image.url')
+ filename = serializers.Field(source='get_filename')
+ size = serializers.Field(source='get_size')
+ class Meta:
+ model = Image
+ read_only_fields = ('created', 'updated')
+
+#class ImageResource(FileResource):
+# model = Image
+#
+# def can_edit(self, instance):
+# return PERMISSIONS.can_edit(self.view.user, instance)
+
View
@@ -6,41 +6,20 @@
import os
from django.conf.urls.defaults import patterns, url
-from django.core.urlresolvers import reverse
from djangorestframework.resources import ModelResource
-from fiber.models import PageContentItem, ContentItem, Image, File
+from fiber.models import File
from fiber.utils.date import friendly_datetime
from fiber.app_settings import PERMISSION_CLASS
from fiber.utils import class_loader
-from .views import ApiRoot, MovePageView, MovePageContentItemView, ListView, TreeListView, FileListView, ImageListView, InstanceView
+from .views import ApiRoot, MovePageView, MovePageContentItemView, TreeListView, FileListView, InstanceView
PERMISSIONS = class_loader.load_class(PERMISSION_CLASS)
-from .views import PageList, PageDetail
-
-
-class PageContentItemResource(ModelResource):
- model = PageContentItem
- depth = 1
-
- def move_url(self, instance):
- """
- Provide a url on this resource that points to the
- `move_page_content_item` method on the PageContentItem model.
- """
- return reverse('page-content-item-resource-instance-move',
- kwargs={'pk': instance.id})
-
- include = ('move_url', )
-
-
-class ContentItemResource(ModelResource):
- model = ContentItem
- depth = 1
+from .views import PageList, PageDetail, PageContentItemList, PageContentItemDetail, ContentItemList, ContentItemDetail, ImageList, ImageDetail
class FileResource(ModelResource):
@@ -61,38 +40,19 @@ def can_edit(self, instance):
include = ('url', 'file_url', 'filename', 'updated', 'can_edit')
-class ImageResource(FileResource):
- model = Image
-
- def image_url(self, instance):
- return instance.image.url
-
- def filename(self, instance):
- return os.path.basename(instance.image.name)
-
- def size(self, instance):
- return '%s x %d' % (instance.width, instance.height)
-
- def can_edit(self, instance):
- return PERMISSIONS.can_edit(self.view.user, instance)
-
- include = ('url', 'image_url', 'filename', 'size', 'updated', 'can_edit')
-
-
urlpatterns = patterns('',
(r'^$', ApiRoot.as_view()),
url(r'^pages/$', PageList.as_view(), name='page-list'),
url(r'^pages/(?P<pk>[^/]+)/$', PageDetail.as_view(), name='page-detail'),
-# url(r'^pages/(?P<pk>[^/]+)/$', InstanceView.as_view(resource=PageResource), name='page-resource-instance'),
url(r'^pages/(?P<pk>[^/]+)/move_page/$', MovePageView.as_view(), name='page-resource-instance-move'),
url(r'^pagetree/$', TreeListView.as_view(), name='pagetree-resource'),
- url(r'^page_content_items/$', ListView.as_view(resource=PageContentItemResource), name='page-content-item-resource-root'),
- url(r'^page_content_items/(?P<pk>[^/]+)/$', InstanceView.as_view(resource=PageContentItemResource), name='page-content-item-resource-instance'),
+ url(r'^page_content_items/$', PageContentItemList.as_view(), name='pagecontentitem-list'),
+ url(r'^page_content_items/(?P<pk>[^/]+)/$', PageContentItemDetail.as_view(), name='pagecontentitem-detail'),
url(r'^page_content_items/(?P<pk>[^/]+)/move/$', MovePageContentItemView.as_view(), name='page-content-item-resource-instance-move'),
- url(r'^content_items/$', ListView.as_view(resource=ContentItemResource), name='content-item-resource-root'),
- url(r'^content_items/(?P<pk>[^/]+)/$', InstanceView.as_view(resource=ContentItemResource), name='contentitem-detail'),
- url(r'^images/$', ImageListView.as_view(resource=ImageResource), name='image-resource-root'),
- url(r'^images/(?P<pk>[^/]+)/$', InstanceView.as_view(resource=ImageResource), name='image-resource-instance'),
+ url(r'^content_items/$', ContentItemList.as_view(), name='contentitem-list'),
+ url(r'^content_items/(?P<pk>[^/]+)/$', ContentItemDetail.as_view(), name='contentitem-detail'),
+ url(r'^images/$', ImageList.as_view(), name='image-list'),
+ url(r'^images/(?P<pk>[^/]+)/$', ImageDetail.as_view(), name='image-detail'),
url(r'^files/$', FileListView.as_view(resource=FileResource), name='file-resource-root'),
url(r'^files/(?P<pk>[^/]+)/$', InstanceView.as_view(resource=FileResource), name='file-resource-instance'),
)
View
@@ -9,7 +9,7 @@
from djangorestframework.response import ErrorResponse
from djangorestframework.renderers import JSONRenderer, DocumentingHTMLRenderer
-from fiber.models import Page, PageContentItem
+from fiber.models import Page, PageContentItem, ContentItem, Image
from fiber.app_settings import API_RENDER_HTML, PERMISSION_CLASS
from fiber.utils import class_loader
from .forms import MovePageForm, MovePageContentItemForm
@@ -26,8 +26,8 @@
'You may need to login or otherwise authenticate the request.'})
-from rest_framework import generics, renderers
-from .serializers import PageSerializer
+from rest_framework import generics, renderers, parsers
+from .serializers import PageSerializer, PageContentItemSerializer, ContentItemSerializer, ImageSerializer
class PageList(generics.ListCreateAPIView):
@@ -42,6 +42,43 @@ class PageDetail(generics.RetrieveUpdateDestroyAPIView):
renderer_classes = (renderers.JSONRenderer, )
+class PageContentItemList(generics.ListCreateAPIView):
+ model = PageContentItem
+ serializer_class = PageContentItemSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
+class PageContentItemDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = PageContentItem
+ serializer_class = PageContentItemSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
+class ContentItemList(generics.ListCreateAPIView):
+ model = ContentItem
+ serializer_class = ContentItemSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
+class ContentItemDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = ContentItem
+ serializer_class = ContentItemSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
+class ImageList(generics.ListCreateAPIView):
+ model = Image
+ serializer_class = ImageSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+ #parser_classes = (parsers.MultiPartParser, )
+
+
+class ImageDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = Image
+ serializer_class = ImageSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
class ApiRoot(View):
"""
The root view for the rest api.

0 comments on commit a0f8cf6

Please sign in to comment.