From c94bfe50d188ab24830a7b0092af882cf2e926ed Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 27 Jul 2018 19:20:13 +0100 Subject: [PATCH] Correctly get short body --- Pipfile | 1 + Pipfile.lock | 9 ++++++++- project/common/models.py | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Pipfile b/Pipfile index cbed4e7..d212930 100644 --- a/Pipfile +++ b/Pipfile @@ -15,6 +15,7 @@ whitenoise = "*" waitress = "*" wagtail-markdown = "*" wagtail-metadata = "*" +"bs4" = "*" [dev-packages] mypy = "*" diff --git a/Pipfile.lock b/Pipfile.lock index b841ea7..f639f0c 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "5ffa0692528697679dcd7085a5f33c448b989d70f53afa989c7a6a213377fc3e" + "sha256": "6398b48732255dbfe41aeaace62f3f4e09fd282f2521716bdb43d388cee0e928" }, "pipfile-spec": 6, "requires": { @@ -31,6 +31,13 @@ ], "version": "==2.1.3" }, + "bs4": { + "hashes": [ + "sha256:36ecea1fd7cc5c0c6e4a1ff075df26d50da647b75376626cc186e2212886dd3a" + ], + "index": "pypi", + "version": "==0.0.1" + }, "certifi": { "hashes": [ "sha256:13e698f54293db9f89122b0581843a782ad0934a4fe0172d2a980ba77fc61bb7", diff --git a/project/common/models.py b/project/common/models.py index 9b9b610..9ded08f 100644 --- a/project/common/models.py +++ b/project/common/models.py @@ -3,6 +3,10 @@ from django.db import models from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel from wagtail.images.edit_handlers import ImageChooserPanel from wagtailmetadata.models import MetadataPageMixin +from bs4 import BeautifulSoup + + +SHORT_BODY_LENGTH = 30 class Entity(MetadataPageMixin, Page): @@ -30,7 +34,9 @@ class Entity(MetadataPageMixin, Page): return self.search_description def get_short_body(self): - return "Short body" + body_words = BeautifulSoup(str(self.body), 'html5lib').get_text().split(' ') + ending = '...' if len(body_words) > SHORT_BODY_LENGTH else '' + return ' '.join(body_words[:SHORT_BODY_LENGTH]) + ending # limit to 30 words (ish) class Meta: abstract = True