Skip to content

Commit

Permalink
fix wrong breadcrumb path issue (ciur/papermerge#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur authored Jan 23, 2023
1 parent ad5a09d commit e359046
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog.d/509.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wrong breadcrumb path when openening document/folder
13 changes: 7 additions & 6 deletions papermerge/core/serializers/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions papermerge/core/serializers/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions papermerge/test/baker_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 16 additions & 17 deletions tests/core/serializers/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit e359046

Please # to comment.