From 358a8e3549309089aef75fdec5d7fed568d6ec81 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 9 Jul 2014 18:13:16 -0400 Subject: [PATCH] Move API code to the views modules --- mayan/apps/documents/api.py | 131 ---------------------------------- mayan/apps/documents/urls.py | 3 +- mayan/apps/documents/views.py | 114 ++++++++++++++++++++++++++++- mayan/apps/sources/api.py | 91 ----------------------- mayan/apps/sources/urls.py | 4 +- mayan/apps/sources/views.py | 81 +++++++++++++++++++++ 6 files changed, 198 insertions(+), 226 deletions(-) delete mode 100644 mayan/apps/documents/api.py delete mode 100644 mayan/apps/sources/api.py diff --git a/mayan/apps/documents/api.py b/mayan/apps/documents/api.py deleted file mode 100644 index cbd1d61cfc..0000000000 --- a/mayan/apps/documents/api.py +++ /dev/null @@ -1,131 +0,0 @@ -from __future__ import absolute_import - -import logging - -from django.core.exceptions import PermissionDenied -from django.shortcuts import get_object_or_404 - -from rest_framework import generics -from rest_framework.response import Response - -from acls.models import AccessEntry -from permissions.models import Permission - -from converter.exceptions import UnknownFileFormat, UnkownConvertError -from converter.literals import (DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, - DEFAULT_PAGE_NUMBER) -from permissions.models import Permission -from rest_api.filters import MayanObjectPermissionsFilter -from rest_api.permissions import MayanPermission - -from .conf.settings import DISPLAY_SIZE, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL -from .models import Document, DocumentVersion, DocumentPage -from .permissions import PERMISSION_DOCUMENT_VIEW -from .serializers import (DocumentImageSerializer, DocumentPageSerializer, - DocumentSerializer, DocumentVersionSerializer) - -logger = logging.getLogger(__name__) - -# API Views - - -class APIDocumentListView(generics.ListAPIView): - """ - Returns a list of all the documents. - """ - - serializer_class = DocumentSerializer - queryset = Document.objects.all() - - filter_backends = (MayanObjectPermissionsFilter,) - mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] - - -class APIDocumentPageView(generics.RetrieveAPIView): - """ - Returns the selected document page details. - """ - - allowed_methods = ['GET'] - serializer_class = DocumentPageSerializer - queryset = DocumentPage.objects.all() - - permission_classes = (MayanPermission,) - mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] - mayan_permission_attribute_check = 'document' - - -class APIDocumentView(generics.RetrieveAPIView): - """ - Returns the selected document details. - """ - - allowed_methods = ['GET'] - serializer_class = DocumentSerializer - queryset = Document.objects.all() - - permission_classes = (MayanPermission,) - mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] - - -class APIDocumentVersionView(generics.RetrieveAPIView): - """ - Returns the selected document version details. - """ - - allowed_methods = ['GET'] - serializer_class = DocumentVersionSerializer - queryset = DocumentVersion.objects.all() - - permission_classes = (MayanPermission,) - mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] - mayan_permission_attribute_check = 'document' - - -class APIDocumentImageView(generics.GenericAPIView): - """ - Returns an image representation of the selected document. - size -- 'x' seprated width and height of the desired image representation. - page -- Page number of the document to be imaged. - zoom -- Zoom level of the image to be generated, numeric value only. - version -- Version number of the document to be imaged. - """ - serializer_class = DocumentImageSerializer - - def get(self, request, pk): - document = get_object_or_404(Document, pk=pk) - - logger.debug('document: %s' % document) - - try: - Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_VIEW]) - except PermissionDenied: - AccessEntry.objects.check_access(PERMISSION_DOCUMENT_VIEW, request.user, document) - - size = request.GET.get('size', DISPLAY_SIZE) - - page = int(request.GET.get('page', DEFAULT_PAGE_NUMBER)) - - zoom = int(request.GET.get('zoom', DEFAULT_ZOOM_LEVEL)) - - version = int(request.GET.get('version', document.latest_version.pk)) - - if request.GET.get('as_base64', False): - base64_version = True - - if zoom < ZOOM_MIN_LEVEL: - zoom = ZOOM_MIN_LEVEL - - if zoom > ZOOM_MAX_LEVEL: - zoom = ZOOM_MAX_LEVEL - - rotation = int(request.GET.get('rotation', DEFAULT_ROTATION)) % 360 - - try: - return Response({'status': 'success', - 'data': document.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True, version=version) - }) - except UnknownFileFormat as exception: - return Response({'status': 'error', 'detail': 'unknown_file_format', 'message': unicode(exception)}) - except UnkownConvertError as exception: - return Response({'status': 'error', 'detail': 'converter_error', 'message': unicode(exception)}) diff --git a/mayan/apps/documents/urls.py b/mayan/apps/documents/urls.py index 9e0848f401..7bb365b542 100644 --- a/mayan/apps/documents/urls.py +++ b/mayan/apps/documents/urls.py @@ -2,9 +2,10 @@ from __future__ import absolute_import from django.conf.urls import patterns, url -from .api import APIDocumentView, APIDocumentListView, APIDocumentVersionView, APIDocumentImageView, APIDocumentPageView from .conf.settings import (PREVIEW_SIZE, PRINT_SIZE, DISPLAY_SIZE, MULTIPAGE_PREVIEW_SIZE) +from .views import (APIDocumentView, APIDocumentImageView, APIDocumentListView, + APIDocumentPageView, APIDocumentVersionView) urlpatterns = patterns('documents.views', url(r'^list/$', 'document_list', (), 'document_list'), diff --git a/mayan/apps/documents/views.py b/mayan/apps/documents/views.py index e96d15cde7..1e0c3d0b6b 100644 --- a/mayan/apps/documents/views.py +++ b/mayan/apps/documents/views.py @@ -14,6 +14,8 @@ from django.template import RequestContext from django.utils.http import urlencode from django.utils.translation import ugettext_lazy as _ +from rest_framework import generics +from rest_framework.response import Response import sendfile from acls.models import AccessEntry @@ -24,6 +26,7 @@ from common.utils import (pretty_size, parse_range, urlquote, return_diff, encapsulate) from common.widgets import two_state_template from common.conf.settings import DEFAULT_PAPER_SIZE +from converter.exceptions import UnknownFileFormat, UnkownConvertError from converter.literals import (DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, DEFAULT_PAGE_NUMBER, DEFAULT_FILE_FORMAT_MIMETYPE) from converter.office_converter import OfficeConverter @@ -31,9 +34,11 @@ from filetransfers.api import serve_file from history.api import create_history from navigation.utils import resolve_to_name from permissions.models import Permission +from rest_api.filters import MayanObjectPermissionsFilter +from rest_api.permissions import MayanPermission from .events import HISTORY_DOCUMENT_EDITED -from .conf.settings import (PREVIEW_SIZE, ZOOM_PERCENT_STEP, +from .conf.settings import (DISPLAY_SIZE, PREVIEW_SIZE, ZOOM_PERCENT_STEP, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL, ROTATION_STEP, RECENT_COUNT) from .forms import (DocumentForm_edit, DocumentPropertiesForm, DocumentPreviewForm, DocumentPageForm, @@ -52,6 +57,8 @@ from .permissions import (PERMISSION_DOCUMENT_PROPERTIES_EDIT, PERMISSION_DOCUMENT_TYPE_DELETE, PERMISSION_DOCUMENT_TYPE_CREATE, PERMISSION_DOCUMENT_TYPE_VIEW) from .runtime import storage_backend +from .serializers import (DocumentImageSerializer, DocumentPageSerializer, + DocumentSerializer, DocumentVersionSerializer) logger = logging.getLogger(__name__) @@ -1331,3 +1338,108 @@ def document_page_transformation_delete(request, document_page_transformation_id 'previous': previous, 'form_icon': u'pencil_delete.png', }, context_instance=RequestContext(request)) + + +# API views + + +class APIDocumentListView(generics.ListAPIView): + """ + Returns a list of all the documents. + """ + + serializer_class = DocumentSerializer + queryset = Document.objects.all() + + filter_backends = (MayanObjectPermissionsFilter,) + mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] + + +class APIDocumentPageView(generics.RetrieveAPIView): + """ + Returns the selected document page details. + """ + + allowed_methods = ['GET'] + serializer_class = DocumentPageSerializer + queryset = DocumentPage.objects.all() + + permission_classes = (MayanPermission,) + mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] + mayan_permission_attribute_check = 'document' + + +class APIDocumentView(generics.RetrieveAPIView): + """ + Returns the selected document details. + """ + + allowed_methods = ['GET'] + serializer_class = DocumentSerializer + queryset = Document.objects.all() + + permission_classes = (MayanPermission,) + mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] + + +class APIDocumentVersionView(generics.RetrieveAPIView): + """ + Returns the selected document version details. + """ + + allowed_methods = ['GET'] + serializer_class = DocumentVersionSerializer + queryset = DocumentVersion.objects.all() + + permission_classes = (MayanPermission,) + mayan_object_permissions = [PERMISSION_DOCUMENT_VIEW] + mayan_permission_attribute_check = 'document' + + +class APIDocumentImageView(generics.GenericAPIView): + """ + Returns an image representation of the selected document. + size -- 'x' seprated width and height of the desired image representation. + page -- Page number of the document to be imaged. + zoom -- Zoom level of the image to be generated, numeric value only. + version -- Version number of the document to be imaged. + """ + serializer_class = DocumentImageSerializer + + def get(self, request, pk): + document = get_object_or_404(Document, pk=pk) + + logger.debug('document: %s' % document) + + try: + Permission.objects.check_permissions(request.user, [PERMISSION_DOCUMENT_VIEW]) + except PermissionDenied: + AccessEntry.objects.check_access(PERMISSION_DOCUMENT_VIEW, request.user, document) + + size = request.GET.get('size', DISPLAY_SIZE) + + page = int(request.GET.get('page', DEFAULT_PAGE_NUMBER)) + + zoom = int(request.GET.get('zoom', DEFAULT_ZOOM_LEVEL)) + + version = int(request.GET.get('version', document.latest_version.pk)) + + if request.GET.get('as_base64', False): + base64_version = True + + if zoom < ZOOM_MIN_LEVEL: + zoom = ZOOM_MIN_LEVEL + + if zoom > ZOOM_MAX_LEVEL: + zoom = ZOOM_MAX_LEVEL + + rotation = int(request.GET.get('rotation', DEFAULT_ROTATION)) % 360 + + try: + return Response({'status': 'success', + 'data': document.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True, version=version) + }) + except UnknownFileFormat as exception: + return Response({'status': 'error', 'detail': 'unknown_file_format', 'message': unicode(exception)}) + except UnkownConvertError as exception: + return Response({'status': 'error', 'detail': 'converter_error', 'message': unicode(exception)}) diff --git a/mayan/apps/sources/api.py b/mayan/apps/sources/api.py deleted file mode 100644 index f82405d116..0000000000 --- a/mayan/apps/sources/api.py +++ /dev/null @@ -1,91 +0,0 @@ -from __future__ import absolute_import - -import logging - -from django.shortcuts import get_object_or_404 - -from rest_framework import generics -from rest_framework.response import Response - -from converter.exceptions import UnkownConvertError, UnknownFileFormat -from converter.literals import (DEFAULT_PAGE_NUMBER, DEFAULT_ROTATION, - DEFAULT_ZOOM_LEVEL) -from documents.conf.settings import DISPLAY_SIZE, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL - -from .models import StagingFolder -from .serializers import (StagingFolderSerializer, StagingFolderFileSerializer, - StagingSourceFileImageSerializer) - -logger = logging.getLogger(__name__) - -# API Views - - -class APIStagingSourceFileView(generics.GenericAPIView): - """ - Details of the selected staging file. - """ - serializer_class = StagingFolderFileSerializer - - def get(self, request, staging_folder_pk, encoded_filename): - staging_folder = get_object_or_404(StagingFolder, pk=staging_folder_pk) - return Response(StagingFolderFileSerializer(staging_folder.get_file(encoded_filename=encoded_filename), context={'request': request}).data) - - -class APIStagingSourceListView(generics.ListAPIView): - """ - Returns a list of all the staging folders and the files they contain. - """ - - serializer_class = StagingFolderSerializer - queryset = StagingFolder.objects.all() - - -class APIStagingSourceView(generics.RetrieveAPIView): - """ - Details of the selected staging folders and the files it contains. - """ - serializer_class = StagingFolderSerializer - queryset = StagingFolder.objects.all() - - -class APIStagingSourceFileImageView(generics.GenericAPIView): - """ - Image of the selected staging file. - size -- 'x' seprated width and height of the desired image representation. - page -- Page number of the staging file to be imaged. - zoom -- Zoom level of the image to be generated, numeric value only. - """ - - serializer_class = StagingSourceFileImageSerializer - - def get(self, request, staging_folder_pk, encoded_filename): - staging_folder = get_object_or_404(StagingFolder, pk=staging_folder_pk) - staging_file = staging_folder.get_file(encoded_filename=encoded_filename) - - size = request.GET.get('size', DISPLAY_SIZE) - - page = int(request.GET.get('page', DEFAULT_PAGE_NUMBER)) - - zoom = int(request.GET.get('zoom', DEFAULT_ZOOM_LEVEL)) - - if request.GET.get('as_base64', False): - base64_version = True - - if zoom < ZOOM_MIN_LEVEL: - zoom = ZOOM_MIN_LEVEL - - if zoom > ZOOM_MAX_LEVEL: - zoom = ZOOM_MAX_LEVEL - - rotation = int(request.GET.get('rotation', DEFAULT_ROTATION)) % 360 - - try: - return Response({'status': 'success', - 'data': staging_file.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True) - }) - except UnknownFileFormat as exception: - return Response({'status': 'error', 'detail': 'unknown_file_format', 'message': unicode(exception)}) - except UnkownConvertError as exception: - return Response({'status': 'error', 'detail': 'converter_error', 'message': unicode(exception)}) - diff --git a/mayan/apps/sources/urls.py b/mayan/apps/sources/urls.py index 784a27c254..434a46d48e 100644 --- a/mayan/apps/sources/urls.py +++ b/mayan/apps/sources/urls.py @@ -2,10 +2,10 @@ from __future__ import absolute_import from django.conf.urls import patterns, url -from .api import (APIStagingSourceListView, APIStagingSourceView, - APIStagingSourceFileView, APIStagingSourceFileImageView) from .literals import (SOURCE_CHOICE_WEB_FORM, SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH) +from .views import (APIStagingSourceListView, APIStagingSourceView, + APIStagingSourceFileView, APIStagingSourceFileImageView) from .wizards import DocumentCreateWizard urlpatterns = patterns('sources.views', diff --git a/mayan/apps/sources/views.py b/mayan/apps/sources/views.py index e2f6a4c692..6089346c44 100644 --- a/mayan/apps/sources/views.py +++ b/mayan/apps/sources/views.py @@ -12,8 +12,15 @@ from django.utils.safestring import mark_safe from django.utils.translation import ugettext from django.utils.translation import ugettext_lazy as _ +from rest_framework import generics +from rest_framework.response import Response + from acls.models import AccessEntry from common.utils import encapsulate +from converter.exceptions import UnkownConvertError, UnknownFileFormat +from converter.literals import (DEFAULT_PAGE_NUMBER, DEFAULT_ROTATION, + DEFAULT_ZOOM_LEVEL) +from documents.conf.settings import DISPLAY_SIZE, ZOOM_MAX_LEVEL, ZOOM_MIN_LEVEL from documents.exceptions import NewDocumentVersionNotAllowed from documents.models import DocumentType, Document from documents.permissions import (PERMISSION_DOCUMENT_CREATE, @@ -31,6 +38,8 @@ from .models import (WebForm, StagingFolder, SourceTransformation, from .permissions import (PERMISSION_SOURCES_SETUP_VIEW, PERMISSION_SOURCES_SETUP_EDIT, PERMISSION_SOURCES_SETUP_DELETE, PERMISSION_SOURCES_SETUP_CREATE) +from .serializers import (StagingFolderSerializer, StagingFolderFileSerializer, + StagingSourceFileImageSerializer) def document_create_siblings(request, document_id): @@ -674,3 +683,75 @@ def setup_source_transformation_create(request, source_type, source_id): 'navigation_object_name': 'source', 'title': _(u'Create new transformation for source: %s') % source, }, context_instance=RequestContext(request)) + + +# API Views + + +class APIStagingSourceFileView(generics.GenericAPIView): + """ + Details of the selected staging file. + """ + serializer_class = StagingFolderFileSerializer + + def get(self, request, staging_folder_pk, encoded_filename): + staging_folder = get_object_or_404(StagingFolder, pk=staging_folder_pk) + return Response(StagingFolderFileSerializer(staging_folder.get_file(encoded_filename=encoded_filename), context={'request': request}).data) + + +class APIStagingSourceListView(generics.ListAPIView): + """ + Returns a list of all the staging folders and the files they contain. + """ + + serializer_class = StagingFolderSerializer + queryset = StagingFolder.objects.all() + + +class APIStagingSourceView(generics.RetrieveAPIView): + """ + Details of the selected staging folders and the files it contains. + """ + serializer_class = StagingFolderSerializer + queryset = StagingFolder.objects.all() + + +class APIStagingSourceFileImageView(generics.GenericAPIView): + """ + Image of the selected staging file. + size -- 'x' seprated width and height of the desired image representation. + page -- Page number of the staging file to be imaged. + zoom -- Zoom level of the image to be generated, numeric value only. + """ + + serializer_class = StagingSourceFileImageSerializer + + def get(self, request, staging_folder_pk, encoded_filename): + staging_folder = get_object_or_404(StagingFolder, pk=staging_folder_pk) + staging_file = staging_folder.get_file(encoded_filename=encoded_filename) + + size = request.GET.get('size', DISPLAY_SIZE) + + page = int(request.GET.get('page', DEFAULT_PAGE_NUMBER)) + + zoom = int(request.GET.get('zoom', DEFAULT_ZOOM_LEVEL)) + + if request.GET.get('as_base64', False): + base64_version = True + + if zoom < ZOOM_MIN_LEVEL: + zoom = ZOOM_MIN_LEVEL + + if zoom > ZOOM_MAX_LEVEL: + zoom = ZOOM_MAX_LEVEL + + rotation = int(request.GET.get('rotation', DEFAULT_ROTATION)) % 360 + + try: + return Response({'status': 'success', + 'data': staging_file.get_image(size=size, page=page, zoom=zoom, rotation=rotation, as_base64=True) + }) + except UnknownFileFormat as exception: + return Response({'status': 'error', 'detail': 'unknown_file_format', 'message': unicode(exception)}) + except UnkownConvertError as exception: + return Response({'status': 'error', 'detail': 'converter_error', 'message': unicode(exception)})