From f10cc898479b12f2a9edea4dfcc358cde6a1ec6f Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 11 Oct 2019 10:10:04 -0400 Subject: [PATCH 1/3] Add document page append view tests Signed-off-by: Roberto Rosario --- mayan/apps/sources/tests/mixins.py | 7 ++++ mayan/apps/sources/tests/test_models.py | 1 - mayan/apps/sources/tests/test_views.py | 50 ++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/mayan/apps/sources/tests/mixins.py b/mayan/apps/sources/tests/mixins.py index d849788514..62959d5cfc 100644 --- a/mayan/apps/sources/tests/mixins.py +++ b/mayan/apps/sources/tests/mixins.py @@ -7,6 +7,13 @@ from .literals import TEST_SOURCE_LABEL, TEST_SOURCE_UNCOMPRESS_N class SourceTestMixin(object): + auto_create_test_source = True + + def setUp(self): + super(SourceTestMixin, self).setUp() + if self.auto_create_test_source: + self._create_test_source() + def _create_test_source(self): self.test_source = WebFormSource.objects.create( enabled=True, label=TEST_SOURCE_LABEL, diff --git a/mayan/apps/sources/tests/test_models.py b/mayan/apps/sources/tests/test_models.py index 8d142f1e9b..188b086931 100644 --- a/mayan/apps/sources/tests/test_models.py +++ b/mayan/apps/sources/tests/test_models.py @@ -39,7 +39,6 @@ class CompressedUploadsTestCase(SourceTestMixin, GenericDocumentTestCase): auto_upload_document = False def test_upload_compressed_file(self): - self._create_test_source() self.test_source.uncompress = SOURCE_UNCOMPRESS_CHOICE_Y self.test_source.save() diff --git a/mayan/apps/sources/tests/test_views.py b/mayan/apps/sources/tests/test_views.py index a5484201f1..cba5067210 100644 --- a/mayan/apps/sources/tests/test_views.py +++ b/mayan/apps/sources/tests/test_views.py @@ -6,7 +6,9 @@ import shutil from mayan.apps.checkouts.models import NewVersionBlock from mayan.apps.common.tests.base import GenericViewTestCase from mayan.apps.documents.models import Document -from mayan.apps.documents.permissions import permission_document_create +from mayan.apps.documents.permissions import ( + permission_document_create, permission_document_new_version +) from mayan.apps.documents.tests.base import GenericDocumentViewTestCase from mayan.apps.documents.tests.literals import ( TEST_COMPRESSED_DOCUMENT_PATH, TEST_DOCUMENT_DESCRIPTION, @@ -28,6 +30,44 @@ from .literals import ( from .mixins import SourceTestMixin, SourceViewTestMixin +class DocumentViewTestMixin(object): + def _request_test_document_append_pages_view(self): + with open(TEST_SMALL_DOCUMENT_PATH, mode='rb') as file_object: + return self.post( + viewname='sources:document_pages_append', kwargs={ + 'document_pk': self.test_document.pk + }, data={ + 'source-file': file_object, + 'document-append_pages': True, # Needs to be explicit + }, + ) + + +class DocumentViewTestCase( + SourceTestMixin, DocumentViewTestMixin, GenericDocumentViewTestCase +): + def test_document_append_pages_view_no_permission(self): + page_count = self.test_document.pages.count() + + response = self._request_test_document_append_pages_view() + self.assertEqual(response.status_code, 403) + + self.assertEqual(self.test_document.pages.count(), page_count) + + def test_document_append_pages_view_with_access(self): + page_count = self.test_document.pages.count() + + self.grant_access( + obj=self.test_document, + permission=permission_document_new_version + ) + + response = self._request_test_document_append_pages_view() + self.assertEqual(response.status_code, 302) + + self.assertTrue(self.test_document.pages.count() > page_count) + + class DocumentUploadWizardViewTestMixin(object): def _request_upload_wizard_view(self, document_path=TEST_SMALL_DOCUMENT_PATH): with open(document_path, mode='rb') as file_object: @@ -54,10 +94,6 @@ class DocumentUploadWizardViewTestCase( ): auto_upload_document = False - def setUp(self): - super(DocumentUploadWizardViewTestCase, self).setUp() - self._create_test_source() - def test_upload_compressed_file(self): self.test_source.uncompress = SOURCE_UNCOMPRESS_CHOICE_Y self.test_source.save() @@ -188,7 +224,7 @@ class DocumentUploadIssueTestCase(GenericDocumentViewTestCase): self.assertEqual(document.description, TEST_DOCUMENT_DESCRIPTION) -class NewDocumentVersionViewTestCase(GenericDocumentViewTestCase): +class DocumentVersionUploadViewTestCase(GenericDocumentViewTestCase): auto_login_superuser = True auto_login_user = False create_test_case_superuser = True @@ -300,6 +336,8 @@ class StagingFolderViewTestCase( class SourcesViewTestCase( SourceTestMixin, SourceViewTestMixin, GenericViewTestCase ): + auto_create_test_source = False + def test_source_create_view_no_permission(self): response = self._request_setup_source_create_view() self.assertEqual(response.status_code, 403) From beb3b936a6571455ef34d404a4babd513cb1d48f Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 11 Oct 2019 10:22:09 -0400 Subject: [PATCH 2/3] Remove the documents app settings Remove DOCUMENTS_DISABLE_BASE_IMAGE_CACHE, DOCUMENTS_DISABLE_TRANSFORMED_IMAGE_CACHE, and DOCUMENTS_FIX_ORIENTATION settings. Signed-off-by: Roberto Rosario --- HISTORY.rst | 3 ++ mayan/apps/documents/api_views.py | 3 +- .../migrations/0054_reset_document_pages.py | 6 ---- .../apps/documents/models/document_models.py | 4 +-- .../documents/models/document_page_models.py | 1 - .../models/document_version_models.py | 17 ++-------- .../models/document_version_page_models.py | 31 ++++++------------- mayan/apps/documents/settings.py | 24 -------------- .../documents/views/document_page_views.py | 2 +- 9 files changed, 20 insertions(+), 71 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index a61fdd6a1e..f44b715dec 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -80,6 +80,9 @@ Deploy a Redis container. - Improve document version upload form. - Use dropzone for document version upload form. +- Remove the DOCUMENTS_DISABLE_BASE_IMAGE_CACHE, + DOCUMENTS_DISABLE_TRANSFORMED_IMAGE_CACHE, and + DOCUMENTS_FIX_ORIENTATION settings. 3.2.8 (2019-10-01) ================== diff --git a/mayan/apps/documents/api_views.py b/mayan/apps/documents/api_views.py index 87c4c9bd3d..302ad9a701 100644 --- a/mayan/apps/documents/api_views.py +++ b/mayan/apps/documents/api_views.py @@ -404,7 +404,8 @@ class APIDocumentTypeView(generics.RetrieveUpdateDestroyAPIView): 'GET': (permission_document_type_view,), 'PUT': (permission_document_type_edit,), 'PATCH': (permission_document_type_edit,), - 'DELETE': (permission_document_type_delete,) } + 'DELETE': (permission_document_type_delete,) + } permission_classes = (MayanPermission,) queryset = DocumentType.objects.all() diff --git a/mayan/apps/documents/migrations/0054_reset_document_pages.py b/mayan/apps/documents/migrations/0054_reset_document_pages.py index 413f423e3d..83e6b27410 100644 --- a/mayan/apps/documents/migrations/0054_reset_document_pages.py +++ b/mayan/apps/documents/migrations/0054_reset_document_pages.py @@ -9,9 +9,6 @@ def get_latest_version(document): def operation_reset_document_pages(apps, schema_editor): Document = apps.get_model(app_label='documents', model_name='Document') - DocumentPage = apps.get_model( - app_label='documents', model_name='DocumentPage' - ) # Define inside the function to use the migration's apps instance def pages_reset(document): @@ -40,9 +37,6 @@ def operation_reset_document_pages(apps, schema_editor): def operation_reset_document_pages_reverse(apps, schema_editor): Document = apps.get_model(app_label='documents', model_name='Document') - DocumentPage = apps.get_model( - app_label='documents', model_name='DocumentPage' - ) for document in Document.objects.using(schema_editor.connection.alias).all(): for document_page in document.pages.all(): diff --git a/mayan/apps/documents/models/document_models.py b/mayan/apps/documents/models/document_models.py index 95cbba506d..7cc761fb90 100644 --- a/mayan/apps/documents/models/document_models.py +++ b/mayan/apps/documents/models/document_models.py @@ -224,8 +224,8 @@ class Document(models.Model): self.latest_version.update_page_count() for version_page in self.latest_version.pages.all(): - document_page = self.pages.create( - content_object = version_page + self.pages.create( + content_object=version_page ) def restore(self): diff --git a/mayan/apps/documents/models/document_page_models.py b/mayan/apps/documents/models/document_page_models.py index b6784a1ff8..adc3391ea6 100644 --- a/mayan/apps/documents/models/document_page_models.py +++ b/mayan/apps/documents/models/document_page_models.py @@ -23,7 +23,6 @@ from mayan.apps.converter.utils import get_converter_class from ..managers import DocumentPageManager from ..settings import ( - setting_disable_base_image_cache, setting_disable_transformed_image_cache, setting_display_width, setting_display_height, setting_zoom_max_level, setting_zoom_min_level ) diff --git a/mayan/apps/documents/models/document_version_models.py b/mayan/apps/documents/models/document_version_models.py index d491ca5bd8..2537a9e800 100644 --- a/mayan/apps/documents/models/document_version_models.py +++ b/mayan/apps/documents/models/document_version_models.py @@ -15,15 +15,13 @@ from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ from mayan.apps.converter.exceptions import InvalidOfficeFormat, PageCountError -from mayan.apps.converter.layers import layer_saved_transformations -from mayan.apps.converter.transformations import TransformationRotate from mayan.apps.converter.utils import get_converter_class from mayan.apps.mimetype.api import get_mimetype from ..events import event_document_new_version, event_document_version_revert from ..literals import DOCUMENT_IMAGES_CACHE_NAME from ..managers import DocumentVersionManager -from ..settings import setting_fix_orientation, setting_hash_block_size +from ..settings import setting_hash_block_size from ..signals import post_document_created, post_version_upload from ..storages import storage_documentversion @@ -152,15 +150,6 @@ class DocumentVersion(models.Model): """ return self.file.storage.exists(self.file.name) - def fix_orientation(self): - for page in self.pages.all(): - degrees = page.detect_orientation() - if degrees: - layer_saved_transformations.add_to_object( - obj=page, transformation=TransformationRotate, - arguments='{{"degrees": {}}}'.format(360 - degrees) - ) - def get_absolute_url(self): return reverse( viewname='documents:document_version_view', kwargs={ @@ -296,8 +285,6 @@ class DocumentVersion(models.Model): self.update_mimetype(save=False) self.save(append_pages=append_pages, _user=user) self.update_page_count(save=False) - if setting_fix_orientation.value: - self.fix_orientation() logger.info( 'New document version "%s" created for document: %s', @@ -330,7 +317,7 @@ class DocumentVersion(models.Model): if append_pages: for version_page in self.pages.all(): self.document.pages.create( - content_object = version_page + content_object=version_page ) else: self.document.pages_reset(update_page_count=False) diff --git a/mayan/apps/documents/models/document_version_page_models.py b/mayan/apps/documents/models/document_version_page_models.py index c0c5b7f8f3..f5255ce383 100644 --- a/mayan/apps/documents/models/document_version_page_models.py +++ b/mayan/apps/documents/models/document_version_page_models.py @@ -21,7 +21,6 @@ from mayan.apps.converter.utils import get_converter_class from ..managers import DocumentVersionPageManager from ..settings import ( - setting_disable_base_image_cache, setting_disable_transformed_image_cache, setting_display_width, setting_display_height, setting_zoom_max_level, setting_zoom_min_level ) @@ -67,16 +66,6 @@ class DocumentVersionPage(models.Model): self.cache_partition.delete() super(DocumentVersionPage, self).delete(*args, **kwargs) - #def detect_orientation(self): - # with self.document_version.open() as file_object: - # converter = get_converter_class()( - # file_object=file_object, - # mime_type=self.document_version.mimetype - # ) - # return converter.detect_orientation( - # page_number=self.page_number - # ) - @property def document(self): return self.document_version.document @@ -102,12 +91,12 @@ class DocumentVersionPage(models.Model): return combined_cache_filename - #def get_absolute_url(self): - # return reverse( - # viewname='documents:document_version_page_view', kwargs={ - # 'pk': self.pk - # } - # ) + def get_absolute_url(self): + return reverse( + viewname='documents:document_version_page_view', kwargs={ + 'pk': self.pk + } + ) def get_api_image_url(self, *args, **kwargs): """ @@ -241,10 +230,6 @@ class DocumentVersionPage(models.Model): ) raise - #@property - #def is_in_trash(self): - # return self.document.is_in_trash - def get_label(self): return _( 'Version page %(page_number)d out of %(total_pages)d of %(document)s' @@ -255,6 +240,10 @@ class DocumentVersionPage(models.Model): } get_label.short_description = _('Label') + @property + def is_in_trash(self): + return self.document_version.document.is_in_trash + def natural_key(self): return (self.page_number, self.document_version.natural_key()) natural_key.dependencies = ['documents.DocumentVersion'] diff --git a/mayan/apps/documents/settings.py b/mayan/apps/documents/settings.py index 8a666c8f11..f0ef66fe26 100644 --- a/mayan/apps/documents/settings.py +++ b/mayan/apps/documents/settings.py @@ -38,21 +38,6 @@ setting_documentimagecache_storage_arguments = namespace.add_setting( 'Arguments to pass to the DOCUMENT_CACHE_STORAGE_BACKEND.' ), ) -setting_disable_base_image_cache = namespace.add_setting( - global_name='DOCUMENTS_DISABLE_BASE_IMAGE_CACHE', default=False, - help_text=_( - 'Disables the first cache tier which stores high resolution, ' - 'non transformed versions of documents\'s pages.' - ) -) -setting_disable_transformed_image_cache = namespace.add_setting( - global_name='DOCUMENTS_DISABLE_TRANSFORMED_IMAGE_CACHE', default=False, - help_text=_( - 'Disables the second cache tier which stores medium to low ' - 'resolution, transformed (rotated, zoomed, etc) versions ' - 'of documents\' pages.' - ) -) setting_display_height = namespace.add_setting( global_name='DOCUMENTS_DISPLAY_HEIGHT', default='' ) @@ -65,15 +50,6 @@ setting_favorite_count = namespace.add_setting( 'Maximum number of favorite documents to remember per user.' ) ) -setting_fix_orientation = namespace.add_setting( - global_name='DOCUMENTS_FIX_ORIENTATION', default=False, - help_text=_( - 'Detect the orientation of each of the document\'s pages ' - 'and create a corresponding rotation transformation to ' - 'display it rightside up. This is an experimental ' - 'feature and it is disabled by default.' - ) -) setting_hash_block_size = namespace.add_setting( global_name='DOCUMENTS_HASH_BLOCK_SIZE', default=DEFAULT_DOCUMENTS_HASH_BLOCK_SIZE, help_text=_( diff --git a/mayan/apps/documents/views/document_page_views.py b/mayan/apps/documents/views/document_page_views.py index 46fcacd9e7..7668b98dcb 100644 --- a/mayan/apps/documents/views/document_page_views.py +++ b/mayan/apps/documents/views/document_page_views.py @@ -21,7 +21,7 @@ from mayan.apps.converter.literals import DEFAULT_ROTATION, DEFAULT_ZOOM_LEVEL from ..forms import DocumentPageForm from ..icons import icon_document_pages from ..links import link_document_pages_reset -from ..models import Document, DocumentPage, DocumentVersionPage +from ..models import Document, DocumentPage from ..permissions import permission_document_edit, permission_document_view from ..settings import ( setting_rotation_step, setting_zoom_percent_step, setting_zoom_max_level, From acd8fd2a3e654551f7ad8d402f59d3eaf7f82217 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 11 Oct 2019 10:22:42 -0400 Subject: [PATCH 3/3] PEP8 cleanup Signed-off-by: Roberto Rosario --- mayan/apps/ocr/migrations/0009_rename_page_content.py | 3 --- mayan/apps/ocr/models.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/mayan/apps/ocr/migrations/0009_rename_page_content.py b/mayan/apps/ocr/migrations/0009_rename_page_content.py index f98ee91251..aef45e2982 100644 --- a/mayan/apps/ocr/migrations/0009_rename_page_content.py +++ b/mayan/apps/ocr/migrations/0009_rename_page_content.py @@ -19,7 +19,6 @@ class Migration(migrations.Migration): name='document_page', field=models.OneToOneField( on_delete=django.db.models.deletion.CASCADE, - #name='document_version_page', related_name='ocr_content', to='documents.DocumentVersionPage', verbose_name='Document version page' @@ -38,5 +37,3 @@ class Migration(migrations.Migration): }, ), ] - - diff --git a/mayan/apps/ocr/models.py b/mayan/apps/ocr/models.py index adaf142a0c..c4b735f57b 100644 --- a/mayan/apps/ocr/models.py +++ b/mayan/apps/ocr/models.py @@ -5,7 +5,7 @@ from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ from mayan.apps.documents.models import ( - DocumentPage, DocumentType, DocumentVersion, DocumentVersionPage + DocumentType, DocumentVersion, DocumentVersionPage ) from .managers import (