From 4ee6add201a96e9f972de38a8f77973950f92365 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 16 Nov 2018 21:43:49 -0400 Subject: [PATCH] Documents: Document image API transformations Add transformations support to the document image API. Signed-off-by: Roberto Rosario --- HISTORY.rst | 2 ++ mayan/apps/documents/api_views.py | 33 ++++++++++++++++++++++++++++++- mayan/apps/documents/models.py | 13 ++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 18b6ae0d35..8bbabcf120 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -12,6 +12,8 @@ as a dependency. - Appearance: Remove unused form_empty_label flag. - Appearance: Allow subclassing the text area widget. +- Documents: Add transformation support to document image API with serialized + transformations. 3.1.9 (2018-11-01) ================== diff --git a/mayan/apps/documents/api_views.py b/mayan/apps/documents/api_views.py index 1fc320cc82..6a77a358b0 100644 --- a/mayan/apps/documents/api_views.py +++ b/mayan/apps/documents/api_views.py @@ -2,8 +2,11 @@ from __future__ import absolute_import, unicode_literals import logging +from furl import furl + from django.http import HttpResponse from django.shortcuts import get_object_or_404 +from django.utils.encoding import force_bytes from django.views.decorators.cache import cache_control, patch_cache_control from django_downloadview import DownloadMixin, VirtualFile @@ -184,6 +187,33 @@ class APIDocumentPageImageView(generics.RetrieveAPIView): @cache_control(private=True) def retrieve(self, request, *args, **kwargs): + transformation_dict = { + 'kwargs': {}, + 'name': {} + } + transformation_list = [] + + querystring = furl() + querystring.args.update(self.request.GET) + querystring.args.update(self.request.POST) + + for key, value in querystring.args.items(): + if key.startswith('transformation_'): + literal, index, element = key.split('_') + transformation_dict[element][index] = value + + for order, identifier in transformation_dict['name'].items(): + if order in transformation_dict['kwargs'].keys(): + kwargs = {} + for kwargs_entry in transformation_dict['kwargs'][order].split(','): + key, value = kwargs_entry.split(':') + kwargs[key] = float(value) + + transformation_list.append({ + 'name': identifier, + 'kwargs': kwargs + }) + width = request.GET.get('width') height = request.GET.get('height') zoom = request.GET.get('zoom') @@ -199,7 +229,8 @@ class APIDocumentPageImageView(generics.RetrieveAPIView): task = task_generate_document_page_image.apply_async( kwargs=dict( document_page_id=self.get_object().pk, width=width, - height=height, zoom=zoom, rotation=rotation + height=height, zoom=zoom, rotation=rotation, + transformation_list=transformation_list ) ) diff --git a/mayan/apps/documents/models.py b/mayan/apps/documents/models.py index 71e035f3b5..f21f464b70 100644 --- a/mayan/apps/documents/models.py +++ b/mayan/apps/documents/models.py @@ -865,10 +865,12 @@ class DocumentPage(models.Model): if document page images. """ transformations_hash = BaseTransformation.combine( - self.get_combined_transformation_list(*args, **kwargs) + transformations=self.get_combined_transformation_list( + *args, **kwargs + ) ) - kwargs.pop('transformations', None) + transformations = kwargs.pop('transformations', None) final_url = furl() final_url.args = kwargs @@ -878,6 +880,13 @@ class DocumentPage(models.Model): ) ) final_url.args['_hash'] = transformations_hash + count = 1 + for transformation in transformations or []: + name, kwargs = transformation.serialize().split(';') + + final_url.args['transformation_{}_name'.format(count)] = name + final_url.args['transformation_{}_kwargs'.format(count)] = kwargs + count = count +1 return final_url.tostr()