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

Truncate message bodies on ContentPage admin list view #139

Merged
merged 2 commits into from
Jul 5, 2023
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
29 changes: 29 additions & 0 deletions home/tests/test_wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.test import TestCase

from home.models import ContentPage
from home.wagtail_hooks import ContentPageAdmin


class ContentPageAdminTests(TestCase):
def test_body_text_truncation(self):
"""
Body text for web, whatsapp, messenger, and viber, should be truncated in this
list view
"""
page = ContentPage(
body=[("paragraph", "test " * 100)],
)
page.whatsapp_body.append(("Whatsapp_Message", {"message": "test " * 100}))
page.messenger_body.append(("messenger_block", {"message": "test " * 100}))
page.viber_body.append(("viber_message", {"message": "test " * 100}))
admin = ContentPageAdmin()

self.assertEqual(len(admin.web_body(page)), 200)
self.assertEqual(len(admin.wa_body(page)), 200)
self.assertEqual(len(admin.mess_body(page)), 200)
self.assertEqual(len(admin.vib_body(page)), 200)
# Web body is different to the rest because of the html
self.assertEqual(admin.web_body(page)[-6:], "test …")
self.assertEqual(admin.wa_body(page)[-5:], "test…")
self.assertEqual(admin.mess_body(page)[-5:], "test…")
self.assertEqual(admin.vib_body(page)[-5:], "test…")
19 changes: 9 additions & 10 deletions home/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.template.defaultfilters import truncatechars
from django.urls import path, reverse
from wagtail import hooks
from wagtail.admin import widgets as wagtailadmin_widgets
Expand Down Expand Up @@ -78,6 +79,7 @@ def register_page_views_report_url():


class ContentPageAdmin(ModelAdmin):
body_truncate_size = 200
model = ContentPage
menu_label = "ContentPages"
menu_icon = "pilcrow"
Expand Down Expand Up @@ -126,28 +128,25 @@ def tag(self, obj):
tag.short_description = "Tags"

def wa_body(self, obj):
return [m.value["message"] for m in obj.whatsapp_body]
body = "\n".join(m.value["message"] for m in obj.whatsapp_body)
return truncatechars(str(body), self.body_truncate_size)

wa_body.short_description = "Whatsapp Body"

def mess_body(self, obj):
body = ""
for message in obj.messenger_body:
body = body + message.value["message"]
return body
body = "\n".join(m.value["message"] for m in obj.messenger_body)
return truncatechars(str(body), self.body_truncate_size)

mess_body.short_description = "Messenger Body"

def vib_body(self, obj):
body = ""
for message in obj.viber_body:
body = body + message.value["message"]
return body
body = "\n".join(m.value["message"] for m in obj.viber_body)
return truncatechars(str(body), self.body_truncate_size)

vib_body.short_description = "Viber Body"

def web_body(self, obj):
return obj.body
return truncatechars(str(obj.body), self.body_truncate_size)

web_body.short_description = "Web Body"

Expand Down
Loading