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

feat: Removing slug field from list display when versioning is enabled #98

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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
Unreleased
==========

* fix: Slug field on list display for admin should only be displayed when versioning is not available

4.0.0.dev2 (2021-12-22)
=======================
Expand Down
37 changes: 32 additions & 5 deletions djangocms_snippet/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@

try:
from djangocms_versioning.admin import ExtendedVersionAdminMixin

if djangocms_versioning_enabled:
snippet_admin_classes.insert(0, ExtendedVersionAdminMixin)
except ImportError:
pass
djangocms_versioning_enabled = False


class SnippetAdmin(*snippet_admin_classes):
list_display = ('slug', 'name')
search_fields = ['slug', 'name']
prepopulated_fields = {'slug': ('name',)}
list_display = ('name',)
search_fields = ['name']
change_form_template = 'djangocms_snippet/admin/change_form.html'
text_area_attrs = {
'rows': 20,
Expand All @@ -46,6 +44,35 @@ class SnippetAdmin(*snippet_admin_classes):
class Meta:
model = Snippet

def get_list_display(self, request):
list_display = super().get_list_display(request)
list_display = list(list_display)

if not djangocms_versioning_enabled:
Bernardvdv marked this conversation as resolved.
Show resolved Hide resolved
list_display.insert(0, 'slug')

list_display = tuple(list_display)
return list_display

def get_search_fields(self, request):
search_fields = super().get_search_fields(request)
if not djangocms_versioning_enabled:
Bernardvdv marked this conversation as resolved.
Show resolved Hide resolved
search_fields.append('slug')
return search_fields

def get_prepopulated_fields(self, obj, request):
prepopulated_fields = super().get_prepopulated_fields(request)
if not djangocms_versioning_enabled:
prepopulated_fields = {'slug': ('name',)}
Bernardvdv marked this conversation as resolved.
Show resolved Hide resolved
return prepopulated_fields

def get_list_display_links(self, request, list_display):
if not djangocms_versioning_enabled:
return list(list_display)[:1]
else:
self.list_display_links = (None,)
return self.list_display_links

def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
Expand Down
32 changes: 31 additions & 1 deletion tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_admin_list_display_with_versioning(self):
self.snippet_admin.__class__.__bases__, (ExtendedVersionAdminMixin, admin.ModelAdmin)
)
self.assertEqual(
list_display[:-1], ('slug', 'name', 'get_author', 'get_modified_date', 'get_versioning_state')
list_display[:-1], ('name', 'get_author', 'get_modified_date', 'get_versioning_state')
)
self.assertEqual(list_display[-1].short_description, 'actions')
self.assertIn("function ExtendedVersionAdminMixin._list_actions", list_display[-1].__str__())
Expand Down Expand Up @@ -179,3 +179,33 @@ def test_admin_form_edit_when_locked(self):
self.assertContains(response, '<div class="readonly">Test Snippet</div>')
# We should have the same number of snippets as before
self.assertEqual(Snippet.objects.count(), 1)

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=False)
def test_slug_colomn_should_hyperlinked_with_versioning_disabled(self):
"""
Slug column should be visible and hyperlinked when versioning is disabled
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.get_superuser()):
response = self.client.get(self.changelist_url)
self.assertContains(response, '<th class="field-slug"><a href="/en/admin/djangocms_snippet/'
'snippet/1/change/">test-snippet</a></th>')

@override_settings(DJANGOCMS_SNIPPET_VERSIONING_ENABLED=True)
def test_name_colomn_should_not_be_hyperlinked_with_versioning_enabled(self):
"""
Name column should be visible and not hyperlinked when versioning is enabled.
Slug column should not be visible when versioning is enabled.
"""
admin.site.unregister(Snippet)
reload(cms_config)
reload(snippet_admin)

with self.login_user_context(self.get_superuser()):
response = self.client.get(self.changelist_url)
self.assertContains(response, '<td class="field-name">Test Snippet</td>')
self.assertNotContains(response, '<th class="field-slug"><a href="/en/admin/djangocms_snippet/'
'snippet/1/change/">test-snippet</a></th>')