Skip to content

Commit

Permalink
Add custom ExcerptCreator and use it within the API reponses
Browse files Browse the repository at this point in the history
  • Loading branch information
ulliholtgrave committed May 11, 2022
1 parent e9cf59a commit e0fa2d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
5 changes: 4 additions & 1 deletion integreat_cms/api/v3/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.http import JsonResponse
from django.utils import timezone

from .utils import ExcerptGenerator
from ...cms.models.events.event_translation import EventTranslation
from ...cms.utils.slug_utils import generate_unique_slug
from ..decorators import json_response
Expand Down Expand Up @@ -46,6 +47,8 @@ def transform_event_translation(event_translation):
"""

event = event_translation.event
excerpt_generator = ExcerptGenerator()
excerpt_generator.feed(event_translation.content)
if event.location:
location_translation = (
event.location.get_public_translation(event_translation.language.slug)
Expand All @@ -60,7 +63,7 @@ def transform_event_translation(event_translation):
"path": event_translation.get_absolute_url(),
"title": event_translation.title,
"modified_gmt": event_translation.last_updated.strftime("%Y-%m-%d %H:%M:%S"),
"excerpt": event_translation.content,
"excerpt": excerpt_generator.text,
"content": event_translation.content,
"available_languages": event_translation.available_languages,
"thumbnail": event.icon.url if event.icon else None,
Expand Down
5 changes: 4 additions & 1 deletion integreat_cms/api/v3/imprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from django.http import JsonResponse

from .utils import ExcerptGenerator
from ..decorators import json_response

logger = logging.getLogger(__name__)
Expand All @@ -21,14 +22,16 @@ def transform_imprint(imprint_translation):
:return: data necessary for API
:rtype: dict
"""
excerpt_generator = ExcerptGenerator()
excerpt_generator.feed(imprint_translation.content)
absolute_url = imprint_translation.get_absolute_url()
return {
"id": imprint_translation.id,
"url": settings.BASE_URL + absolute_url,
"path": absolute_url,
"title": imprint_translation.title,
"modified_gmt": imprint_translation.last_updated,
"excerpt": imprint_translation.content,
"excerpt": excerpt_generator.text,
"content": imprint_translation.content,
"parent": None,
"available_languages": imprint_translation.available_languages,
Expand Down
5 changes: 4 additions & 1 deletion integreat_cms/api/v3/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.shortcuts import get_object_or_404
from django.views.decorators.csrf import csrf_exempt

from .utils import ExcerptGenerator
from ...cms.models import Page
from ...cms.forms import PageTranslationForm
from ..decorators import json_response, matomo_tracking
Expand Down Expand Up @@ -61,14 +62,16 @@ def transform_page(page_translation):
# use tree id of mptt model for ordering of root pages
order = page_translation.page.tree_id

excerpt_generator = ExcerptGenerator()
excerpt_generator.feed(page_translation.combined_text)
absolute_url = page_translation.get_absolute_url()
return {
"id": page_translation.id,
"url": settings.BASE_URL + absolute_url,
"path": absolute_url,
"title": page_translation.title,
"modified_gmt": page_translation.combined_last_updated,
"excerpt": page_translation.content,
"excerpt": excerpt_generator.text,
"content": page_translation.combined_text,
"parent": parent,
"order": order,
Expand Down
32 changes: 32 additions & 0 deletions integreat_cms/api/v3/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This module includes some utility classes and functions used within the API module.
"""
import logging

from html.parser import HTMLParser

logger = logging.getLogger(__name__)


class ExcerptGenerator(HTMLParser):
"""
Class that can be used to convert a string containing HTML into a pure text string.
"""

text = ""

def handle_data(self, data):
"""
Function that overwrites the HTMLParser function and returns a a pure text representations.
:param data: The original HTML content.
:type data: str
"""
self.text += data

def error(self, message):
logger.warning(
"The ExcerptGenerator couldn't create a pure text for %s and returns the following error message: %s",
self.text,
message,
)

0 comments on commit e0fa2d3

Please sign in to comment.