From 82934c50e7fb933e86c331215ed240df1b21b5a4 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 10 Jul 2014 01:48:09 -0400 Subject: [PATCH] Move sources app API views to their own module --- mayan/apps/sources/api_views.py | 90 +++++++++++++++++++++++++++++++++ mayan/apps/sources/urls.py | 8 +-- mayan/apps/sources/views.py | 72 -------------------------- 3 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 mayan/apps/sources/api_views.py diff --git a/mayan/apps/sources/api_views.py b/mayan/apps/sources/api_views.py new file mode 100644 index 0000000000..a1cc60adc5 --- /dev/null +++ b/mayan/apps/sources/api_views.py @@ -0,0 +1,90 @@ +from __future__ import absolute_import + +from django.core.exceptions import PermissionDenied +from django.shortcuts import get_object_or_404 + +from converter.exceptions import UnkownConvertError, UnknownFileFormat +from converter.literals import (DEFAULT_FILE_FORMAT_MIMETYPE, DEFAULT_PAGE_NUMBER, + DEFAULT_ROTATION, DEFAULT_ZOOM_LEVEL) +from permissions.models import Permission +from rest_framework import generics +from rest_framework.response import Response + +from documents.conf.settings import (DISPLAY_SIZE, PREVIEW_SIZE, RECENT_COUNT, + ROTATION_STEP, ZOOM_PERCENT_STEP, ZOOM_MAX_LEVEL, + ZOOM_MIN_LEVEL) +from rest_api.filters import MayanObjectPermissionsFilter +from rest_api.permissions import MayanPermission + +from .models import StagingFolder +from .serializers import (StagingFolderFileSerializer, StagingFolderSerializer, + StagingSourceFileImageSerializer) + + +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 434a46d48e..d492eb30e6 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 .literals import (SOURCE_CHOICE_WEB_FORM, SOURCE_CHOICE_STAGING, - SOURCE_CHOICE_WATCH) -from .views import (APIStagingSourceListView, APIStagingSourceView, - APIStagingSourceFileView, APIStagingSourceFileImageView) +from .api_views import (APIStagingSourceFileView, APIStagingSourceFileImageView, + APIStagingSourceListView, APIStagingSourceView) +from .literals import (SOURCE_CHOICE_STAGING, SOURCE_CHOICE_WATCH, + SOURCE_CHOICE_WEB_FORM) from .wizards import DocumentCreateWizard urlpatterns = patterns('sources.views', diff --git a/mayan/apps/sources/views.py b/mayan/apps/sources/views.py index 6089346c44..d09ce7ad44 100644 --- a/mayan/apps/sources/views.py +++ b/mayan/apps/sources/views.py @@ -683,75 +683,3 @@ 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)})