diff --git a/apps/rest_api/resources.py b/apps/rest_api/resources.py index d127096f3a..971161133e 100644 --- a/apps/rest_api/resources.py +++ b/apps/rest_api/resources.py @@ -1,15 +1,12 @@ from django.core.urlresolvers import reverse -from djangorestframework.resources import ModelResource +from rest_framework import serializers -from documents.models import Document from converter.exceptions import UnknownFileFormat, UnkownConvertError +from documents.models import Document, DocumentType -class DocumentResourceSimple(ModelResource): - model = Document - fields = ('url', 'pk', 'document_type', 'uuid', 'date_added', 'description', 'tags', 'comments', 'expensive_methods', 'versions') - +class DocumentResourceSimple(serializers.HyperlinkedModelSerializer): def versions(self, instance): return [ { @@ -36,10 +33,11 @@ class DocumentResourceSimple(ModelResource): } for page in version.pages.all() ] - + } for version in instance.versions.all() ] - def expensive_methods(self, instance): - return [] + class Meta: + model = Document + fields = ('url', 'uuid', 'date_added', 'description') diff --git a/apps/rest_api/urls.py b/apps/rest_api/urls.py index 315bd5f1bc..d2a80ebaf7 100644 --- a/apps/rest_api/urls.py +++ b/apps/rest_api/urls.py @@ -2,17 +2,13 @@ from __future__ import absolute_import from django.conf.urls.defaults import patterns, url -from djangorestframework.views import ListModelView -from djangorestframework.views import ListOrCreateModelView, InstanceModelView - -from .views import APIBase, Version_0, ReadOnlyInstanceModelView, IsZoomable -from .resources import DocumentResourceSimple +from .views import APIBase, Version_1, DocumentDetailView, IsZoomable urlpatterns = patterns('', url(r'^$', APIBase.as_view(), name='api-root'), - url(r'^v0/$', Version_0.as_view(), name='api-version-0'), - - # Version 0 alpha API calls - url(r'^v0/document/(?P[0-9]+)/$', ReadOnlyInstanceModelView.as_view(resource=DocumentResourceSimple), name='documents-simple'), - url(r'^v0/document/(?P[0-9]+)/version/(?P[0-9]+)/page/(?P[0-9]+)/expensive/is_zoomable/$', IsZoomable.as_view(), name='documents-expensive-is_zoomable'), + url(r'^v1/$', Version_1.as_view(), name='api-version-1'), + + # Version 1 API calls + url(r'^v1/document/(?P[0-9]+)/$', DocumentDetailView.as_view(), name='document-detail'), + url(r'^v1/document/(?P[0-9]+)/version/(?P[0-9]+)/page/(?P[0-9]+)/expensive/is_zoomable/$', IsZoomable.as_view(), name='documents-expensive-is_zoomable'), ) diff --git a/apps/rest_api/views.py b/apps/rest_api/views.py index 6624e41510..c6f48d72b7 100644 --- a/apps/rest_api/views.py +++ b/apps/rest_api/views.py @@ -1,28 +1,27 @@ -'''Views file for the rest_api app''' +"""Views file for the rest_api app""" import logging -from django.utils.translation import ugettext_lazy as _ -from django.shortcuts import get_object_or_404 from django.core.urlresolvers import reverse +from django.shortcuts import get_object_or_404 +from django.utils.translation import ugettext_lazy as _ -from documents.models import Document, DocumentVersion, DocumentPage from converter.exceptions import UnknownFileFormat, UnkownConvertError +from documents.models import Document, DocumentVersion, DocumentPage -from djangorestframework.views import View, ModelView, ListModelView, InstanceModelView -from djangorestframework.mixins import InstanceMixin, ReadModelMixin -from djangorestframework.response import Response -from djangorestframework import status +from rest_framework import generics +from rest_framework import permissions +from rest_framework import serializers +from rest_framework.response import Response +from rest_framework.reverse import reverse + +from .resources import DocumentResourceSimple logger = logging.getLogger(__name__) -class ReadOnlyInstanceModelView(InstanceModelView): - allowed_methods = ['GET'] - - -class APIBase(View): - """This is the REST API for Mayan EDMS (https://github.com/rosarior/mayan/). +class APIBase(generics.GenericAPIView): + """This is the REST API for Mayan EDMS (https://github.com/mayan-edms/mayan-edms). All the API calls can be navigated either through the browser or from the command line... @@ -31,27 +30,37 @@ class APIBase(View): """ - def get(self, request): - return [ - {'name': 'Version 0 Alpha', 'url': reverse('api-version-0')} - ] + def get(self, request, format=None): + return Response( + {'versions': [ + {'name': 'Version 1', 'url': reverse('api-version-1', request=request, format=format), 'number': 1} + ]} + ) -class Version_0(View): - def get(self, request): - return [ - {'name': 'Resources', 'resources': ['document/']} - ] +class Version_1(generics.GenericAPIView): + def get(self, request, format=None): + return Response( + [ + {'name': 'Resources', 'resources': ['document/']} + ] + ) -class IsZoomable(View): +class DocumentDetailView(generics.RetrieveAPIView): + allowed_methods = ['GET'] + serializer_class = DocumentResourceSimple + queryset = Document.objects.all() + + +class IsZoomable(generics.GenericAPIView): def get(self, request, pk, page_number, version_pk): logger.info('received is_zoomable call from: %s' % (request.META['REMOTE_ADDR'])) document_version = get_object_or_404(DocumentVersion, pk=version_pk) try: document_version.document.get_image_cache_name(int(page_number), version_pk) - return {'result': True} + return Response({'result': True}) except (UnknownFileFormat, UnkownConvertError, DocumentPage.DoesNotExist, Document.DoesNotExist, DocumentVersion.DoesNotExist): - return {'result': False} + return Response({'result': False}) diff --git a/requirements/production.txt b/requirements/production.txt index ba7a339143..4962bcb799 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -1,4 +1,4 @@ -Django==1.3.5 +Django==1.4.13 django-pagination==1.0.7 wsgiref==0.1.2 django-taggit==0.9.3 @@ -12,7 +12,7 @@ Pillow==1.7.8 cssmin==0.1.4 django-compressor==1.1.1 -e git+https://github.com/rosarior/django-sendfile.git#egg=django-sendfile -djangorestframework==0.2.3 +djangorestframework==2.3.13 South==0.7.6 https://github.com/rosarior/python-gnupg/zipball/0.2.8 python-hkp==0.1.3 diff --git a/settings.py b/settings.py index bb795dd01d..c1ee5dba73 100644 --- a/settings.py +++ b/settings.py @@ -148,7 +148,7 @@ INSTALLED_APPS = ( 'taggit', 'mptt', 'compressor', - 'djangorestframework', + 'rest_framework', # Base generic 'permissions', 'project_setup', @@ -274,7 +274,7 @@ if DEVELOPMENT: import debug_toolbar #INSTALLED_APPS +=('debug_toolbar',) except ImportError: - pass + pass TEMPLATE_CONTEXT_PROCESSORS += ('django.core.context_processors.debug',)