Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ported Files.

  • Loading branch information...
commit 2c1dce31a75866274774cef45767fc4760b48368 1 parent dd121c9
Marko Tibold markotibold authored
3  fiber/models.py
View
@@ -341,3 +341,6 @@ def save(self, *args, **kwargs):
def delete(self, *args, **kwargs):
os.remove(os.path.join(settings.MEDIA_ROOT, str(self.file)))
super(File, self).delete(*args, **kwargs)
+
+ def get_filename(self):
+ return os.path.basename(self.file.name)
12 fiber/rest_api/serializers.py
View
@@ -1,6 +1,6 @@
from rest_framework import serializers
-from fiber.models import Page, PageContentItem, ContentItem, Image
+from fiber.models import Page, PageContentItem, ContentItem, File, Image
from .fields import CanEditField
@@ -26,6 +26,16 @@ class Meta:
model = ContentItem
+class FileSerializer(serializers.ModelSerializer):
+ file_url = serializers.Field(source='file.url')
+ filename = serializers.Field(source='get_filename')
+ can_edit = CanEditField()
+
+ class Meta:
+ model = File
+ read_only_fields = ('created', 'updated')
+
+
class ImageSerializer(serializers.ModelSerializer):
image_url = serializers.Field(source='image.url')
filename = serializers.Field(source='get_filename')
32 fiber/rest_api/urls.py
View
@@ -3,41 +3,17 @@
http://django-rest-framework.readthedocs.org/.
"""
-import os
-
from django.conf.urls.defaults import patterns, url
-from djangorestframework.resources import ModelResource
-
-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, TreeListView, FileListView, InstanceView
+from .views import ApiRoot, MovePageView, MovePageContentItemView, TreeListView
PERMISSIONS = class_loader.load_class(PERMISSION_CLASS)
-from .views import PageList, PageDetail, PageContentItemList, PageContentItemDetail, ContentItemList, ContentItemDetail, ImageList, ImageDetail
-
-
-class FileResource(ModelResource):
- model = File
-
- def file_url(self, instance):
- return instance.file.url
-
- def filename(self, instance):
- return os.path.basename(instance.file.name)
-
- def updated(self, instance):
- return friendly_datetime(instance.updated)
-
- def can_edit(self, instance):
- return PERMISSIONS.can_edit(self.view.user, instance)
-
- include = ('url', 'file_url', 'filename', 'updated', 'can_edit')
+from .views import PageList, PageDetail, PageContentItemList, PageContentItemDetail, ContentItemList, ContentItemDetail, ImageList, ImageDetail, FileList, FileDetail
urlpatterns = patterns('',
@@ -53,6 +29,6 @@ def can_edit(self, instance):
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'),
+ url(r'^files/$', FileList.as_view(), name='file-list'),
+ url(r'^files/(?P<pk>[^/]+)/$', FileDetail.as_view(), name='file-detail'),
)
19 fiber/rest_api/views.py
View
@@ -9,7 +9,7 @@
from djangorestframework.response import ErrorResponse
from djangorestframework.renderers import JSONRenderer, DocumentingHTMLRenderer
-from fiber.models import Page, PageContentItem, ContentItem, Image
+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 .forms import MovePageForm, MovePageContentItemForm
@@ -26,8 +26,8 @@
'You may need to login or otherwise authenticate the request.'})
-from rest_framework import generics, renderers, parsers
-from .serializers import PageSerializer, PageContentItemSerializer, ContentItemSerializer, ImageSerializer
+from rest_framework import generics, renderers
+from .serializers import PageSerializer, PageContentItemSerializer, ContentItemSerializer, FileSerializer, ImageSerializer
class PageList(generics.ListCreateAPIView):
@@ -66,11 +66,22 @@ class ContentItemDetail(generics.RetrieveUpdateDestroyAPIView):
renderer_classes = (renderers.JSONRenderer, )
+class FileList(generics.ListCreateAPIView):
+ model = File
+ serializer_class = FileSerializer
+ renderer_classes = (renderers.JSONRenderer, )
+
+
+class FileDetail(generics.RetrieveUpdateDestroyAPIView):
+ model = File
+ serializer_class = FileSerializer
+ 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):
Please sign in to comment.
Something went wrong with that request. Please try again.