diff --git a/HISTORY.rst b/HISTORY.rst index c199dc10d8..11813952c1 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,3 +1,9 @@ +3.1.6 (2018-10-XX) +================== +* Improve index mounting value clean up code to remove the spaces at the + starts and at the end of directories. Closes again GitLab issue #520 + Thanks to TheOneValen @ for the report. + 3.1.5 (2018-10-08) ================== * Consolidate some document indexing test code into a new mixin. diff --git a/mayan/apps/mirroring/filesystems.py b/mayan/apps/mirroring/filesystems.py index 07582ce411..51b7d45e81 100644 --- a/mayan/apps/mirroring/filesystems.py +++ b/mayan/apps/mirroring/filesystems.py @@ -9,7 +9,7 @@ from time import time from fuse import FuseOSError, Operations from django.core.exceptions import MultipleObjectsReturned -from django.db.models import Count, F, Func, Value +from django.db.models import Count, F, Func, Transform, Value from document_indexing.models import Index, IndexInstanceNode from documents.models import Document @@ -23,14 +23,22 @@ from .runtime import cache logger = logging.getLogger(__name__) +class Trim(Transform): + function = 'TRIM' + lookup_name = 'trim' + + class IndexFilesystem(Operations): @staticmethod def _clean_queryset(queryset): - # Remove newline carriage return to make multiline indexes + # Remove newline carriage returns and the first and last space + # to make multiline indexes # valid directoy names return queryset.annotate( - clean_value=Func( - F('value'), Value('\r\n'), Value(' '), function='replace' + clean_value=Trim( + Func( + F('value'), Value('\r\n'), Value(' '), function='replace' + ), ) ) diff --git a/mayan/apps/mirroring/tests/literals.py b/mayan/apps/mirroring/tests/literals.py index 6231941d5e..19708b1341 100644 --- a/mayan/apps/mirroring/tests/literals.py +++ b/mayan/apps/mirroring/tests/literals.py @@ -3,5 +3,8 @@ from __future__ import absolute_import, unicode_literals TEST_DOCUMENT_PK = 99 TEST_NODE_EXPRESSION = 'level_1' TEST_NODE_EXPRESSION_MULTILINE = 'first\r\nsecond\r\nthird' +TEST_NODE_EXPRESSION_MULTILINE_EXPECTED = 'first second third' +TEST_NODE_EXPRESSION_MULTILINE_2 = '\r\n\r\nfirst\r\nsecond\r\nthird\r\n' +TEST_NODE_EXPRESSION_MULTILINE_2_EXPECTED = 'first second third' TEST_NODE_PK = 88 TEST_PATH = '/test/path' diff --git a/mayan/apps/mirroring/tests/test_filesystems.py b/mayan/apps/mirroring/tests/test_filesystems.py index 6a0409e9d4..7ccafec4c3 100644 --- a/mayan/apps/mirroring/tests/test_filesystems.py +++ b/mayan/apps/mirroring/tests/test_filesystems.py @@ -14,7 +14,9 @@ from document_indexing.tests import DocumentIndexingTestMixin from ..filesystems import IndexFilesystem from .literals import ( - TEST_NODE_EXPRESSION, TEST_NODE_EXPRESSION_MULTILINE + TEST_NODE_EXPRESSION, TEST_NODE_EXPRESSION_MULTILINE, + TEST_NODE_EXPRESSION_MULTILINE_EXPECTED, TEST_NODE_EXPRESSION_MULTILINE_2, + TEST_NODE_EXPRESSION_MULTILINE_2_EXPECTED ) @@ -93,7 +95,24 @@ class IndexFilesystemTestCase(DocumentIndexingTestMixin, DocumentTestMixin, Base self.assertEqual( list(index_filesystem.readdir('/', ''))[2:], - [TEST_NODE_EXPRESSION_MULTILINE.replace('\r\n', ' ')] + [TEST_NODE_EXPRESSION_MULTILINE_EXPECTED] + ) + + def test_multiline_indexes_first_and_last(self): + self._create_index() + + self.index.node_templates.create( + parent=self.index.template_root, + expression=TEST_NODE_EXPRESSION_MULTILINE_2, + link_documents=True + ) + + self.upload_document() + index_filesystem = IndexFilesystem(index_slug=self.index.slug) + + self.assertEqual( + list(index_filesystem.readdir('/', ''))[2:], + [TEST_NODE_EXPRESSION_MULTILINE_2_EXPECTED] ) def test_duplicated_indexes(self):