diff --git a/changelog.d/509.fixed.md b/changelog.d/509.fixed.md new file mode 100644 index 000000000..ec5cdd8dd --- /dev/null +++ b/changelog.d/509.fixed.md @@ -0,0 +1 @@ +Wrong breadcrumb path when openening document/folder \ No newline at end of file diff --git a/papermerge/core/serializers/document.py b/papermerge/core/serializers/document.py index 353947dfa..9699e49e6 100644 --- a/papermerge/core/serializers/document.py +++ b/papermerge/core/serializers/document.py @@ -98,12 +98,13 @@ class Meta: 'updated_at' ) - def get_breadcrumb(self, obj: Document) -> str: - titles = [ - item.title - for item in obj.get_ancestors() - ] - return '/'.join(titles) + def get_breadcrumb(self, obj: Document): + return [(item.title, item.id) for item in obj.get_ancestors()] + + def to_representation(self, instance): + result = super().to_representation(instance) + result['parent']['type'] = 'node' + return result class Data_DocumentDetailsSerializer(Serializer): diff --git a/papermerge/core/serializers/folder.py b/papermerge/core/serializers/folder.py index 230308433..2b682f1fb 100644 --- a/papermerge/core/serializers/folder.py +++ b/papermerge/core/serializers/folder.py @@ -57,5 +57,11 @@ class Meta: ) ] - def get_breadcrumb(self, obj: Folder) -> str: - return obj.breadcrumb + def get_breadcrumb(self, obj): + return [(item.title, item.id) for item in obj.get_ancestors()] + + def to_representation(self, instance): + result = super().to_representation(instance) + if result['parent']: + result['parent']['type'] = 'node' + return result diff --git a/papermerge/test/baker_recipes.py b/papermerge/test/baker_recipes.py index 1e741be9f..25bb4932b 100644 --- a/papermerge/test/baker_recipes.py +++ b/papermerge/test/baker_recipes.py @@ -15,6 +15,10 @@ def make_folders(breadcrumb: str, user=None): """Creates folders from specified breadcrumb. Hierarchy (i.e. parent/child) is respected. + Breadcrumb must include the root folder i.e. .home or .inbox. + Example of usage: + + >>> lidl_folder = make_folders('.home/My Documents/My Invoices/Lidl') """ if user is None: user = user_recipe.make() diff --git a/tests/core/serializers/test_folder.py b/tests/core/serializers/test_folder.py index cda052259..9025f8334 100644 --- a/tests/core/serializers/test_folder.py +++ b/tests/core/serializers/test_folder.py @@ -4,7 +4,7 @@ from papermerge.core.serializers import FolderSerializer from papermerge.test import TestCase -from papermerge.test.baker_recipes import folder_recipe, user_recipe +from papermerge.test.baker_recipes import make_folders class TestFolderSerializer(TestCase): @@ -50,22 +50,21 @@ def test_basic_folder_serialization_from_instance(self): @pytest.mark.django_db def test_folder_serializer_for_correct_breadcrumb(): - user = user_recipe.make() - my_documents = folder_recipe.make( - title="My Documents", - parent=user.home_folder - ) - sub1 = folder_recipe.make( - title="My Invoices", - user=user, - parent=my_documents - ) - lidl_folder = folder_recipe.make( - title="Lidl", - user=user, - parent=sub1 - ) + lidl_folder = make_folders(".home/My Documents/My Invoices/Lidl") ser = FolderSerializer(lidl_folder) - assert ser.data['breadcrumb'] == '.home/My Documents/My Invoices/Lidl/' + # breadcrumb returns a list of tuples (title, id) + # where ``id`` is the id of the node from the breadcrumb + # and ``title`` is the title of the node from the breadcrumb + # The most distanced ancestor is returned first + # i.e .home (or .inbox) title will be first in the list + actual_breadcrumb_titles = set([ + item[0] for item in ser.data['breadcrumb'] + ]) + + expected_breadcrumb_titles = { + '.home', 'My Documents', 'My Invoices', 'Lidl' + } + + assert actual_breadcrumb_titles == expected_breadcrumb_titles