Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make breadcrumbs show specific parents when they exist #7

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
long_description=long_description,
long_description_content_type='text/markdown',
license='CC0',
version='1.0.2',
version='1.0.3',
include_package_data=True,
packages=find_packages(),
package_data={
Expand Down
10 changes: 8 additions & 2 deletions treemodeladmin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def get_index_url_with_parent(self, parent_field, parent_pk):
parent_pk=quote(parent_pk)
)

def crumb(self, parent_field=None, parent_instance=None):
def crumb(self, parent_field=None, parent_instance=None,
specific_instance=None):
if parent_field is not None and parent_instance is not None:
index_url = self.get_index_url_with_parent(
parent_field,
Expand All @@ -29,9 +30,14 @@ def crumb(self, parent_field=None, parent_instance=None):
else:
index_url = self.index_url

if specific_instance is not None:
crumb_text = force_text(specific_instance)
else:
crumb_text = force_text(self.opts.verbose_name_plural)

return (
index_url,
force_text(self.opts.verbose_name_plural)
crumb_text
)


Expand Down
27 changes: 24 additions & 3 deletions treemodeladmin/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ def test_breadcrumbs(self):
)


class TestAuthorCreateView(TestCase, WagtailTestUtils):
fixtures = ['treemodeladmin_test.json']

def setUp(self):
self.user = self.login()

def post(self, post_data):
return self.client.post('/admin/treemodeladmintest/author/create/',
post_data)

def test_create_redirects_to_plain_index(self):
response = self.post({
'name': 'P. G. Wodehouse',
})

# Should redirect back to
self.assertRedirects(response,
'/admin/treemodeladmintest/author/')


class TestBookIndexView(TestCase, WagtailTestUtils):
fixtures = ['treemodeladmin_test.json']

Expand Down Expand Up @@ -95,7 +115,7 @@ def test_breadcrumbs_with_parent(self):
self.assertEqual(
list(resposne.context['view'].breadcrumbs),
[
('/admin/treemodeladmintest/author/', 'authors'),
('/admin/treemodeladmintest/author/', 'J. R. R. Tolkien'),
('/admin/treemodeladmintest/book/?author=1', 'books')
]
)
Expand Down Expand Up @@ -234,8 +254,9 @@ def test_breadcrumbs_with_parents(self):
self.assertEqual(
list(resposne.context['view'].breadcrumbs),
[
('/admin/treemodeladmintest/author/', 'authors'),
('/admin/treemodeladmintest/book/?author=1', 'books'),
('/admin/treemodeladmintest/author/', 'J. R. R. Tolkien'),
('/admin/treemodeladmintest/book/?author=1',
'The Lord of the Rings'),
('/admin/treemodeladmintest/volume/?book=1', 'volumes')
]
)
5 changes: 4 additions & 1 deletion treemodeladmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def parent_instance(self):
@property
def breadcrumbs(self):
parent_instance = self.parent_instance
specific_instance = None
model_admin = self.model_admin

breadcrumbs = []
Expand All @@ -65,12 +66,14 @@ def breadcrumbs(self):
breadcrumbs.append(
model_admin.url_helper.crumb(
parent_field=model_admin.parent_field,
parent_instance=parent_instance
parent_instance=parent_instance,
specific_instance=specific_instance
)
)

if model_admin.has_parent():
model_admin = model_admin.parent
specific_instance = parent_instance
parent_instance = getattr(
parent_instance,
model_admin.parent_field,
Expand Down