Skip to content
Snippets Groups Projects
Commit 8721c410 authored by xaralis's avatar xaralis Committed by jan.bednarik
Browse files

feat: improve meta tag fallback handling to provide more meaningful values...

feat: improve meta tag fallback handling to provide more meaningful values when not overriden manually
parent e6167d15
No related branches found
No related tags found
2 merge requests!518Release,!515feat: improve meta tag fallback handling to provide more meaningful values when not overriden manually
Pipeline #8233 canceled
...@@ -40,7 +40,6 @@ from maps_utils.const import ( ...@@ -40,7 +40,6 @@ from maps_utils.const import (
from maps_utils.validation import validators as maps_validators from maps_utils.validation import validators as maps_validators
from shared.blocks import ( from shared.blocks import (
DEFAULT_CONTENT_BLOCKS, DEFAULT_CONTENT_BLOCKS,
ButtonBlock,
ButtonGroupBlock, ButtonGroupBlock,
FigureBlock, FigureBlock,
YouTubeVideoBlock, YouTubeVideoBlock,
...@@ -53,7 +52,7 @@ from shared.models import ( ...@@ -53,7 +52,7 @@ from shared.models import (
MenuMixin, MenuMixin,
SubpageMixin, SubpageMixin,
) )
from shared.utils import make_promote_panels from shared.utils import make_promote_panels, strip_all_html_tags, trim_to_length
from tuning import admin_help from tuning import admin_help
from . import blocks from . import blocks
...@@ -752,6 +751,18 @@ class DistrictPersonPage( ...@@ -752,6 +751,18 @@ class DistrictPersonPage(
context["random_people"] = context["random_people"][:3] context["random_people"] = context["random_people"][:3]
return context return context
def get_meta_image(self):
return self.search_image or self.profile_photo
def get_meta_description(self):
if self.search_description:
return self.search_description
if self.text:
return trim_to_length(strip_all_html_tags(self.text))
return None
class DistrictPeoplePage( class DistrictPeoplePage(
ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page
...@@ -853,6 +864,11 @@ class DistrictPostElectionStrategyPage( ...@@ -853,6 +864,11 @@ class DistrictPostElectionStrategyPage(
class Meta: class Meta:
verbose_name = "Povolební strategie" verbose_name = "Povolební strategie"
def get_meta_description(self):
if self.search_description:
return self.search_description
return self.perex
class DistrictElectionProgramPage( class DistrictElectionProgramPage(
DistrictElectionSubCampaignPageMixin, DistrictElectionBasePage DistrictElectionSubCampaignPageMixin, DistrictElectionBasePage
...@@ -886,6 +902,14 @@ class DistrictElectionProgramPage( ...@@ -886,6 +902,14 @@ class DistrictElectionProgramPage(
class Meta: class Meta:
verbose_name = "Bod programu voleb" verbose_name = "Bod programu voleb"
def get_meta_image(self):
return self.search_image or self.image or self.root_page.get_meta_image()
def get_meta_description(self):
if self.search_description:
return self.search_description
return self.perex
class DistrictElectionCampaignPage(DistrictElectionBasePage): class DistrictElectionCampaignPage(DistrictElectionBasePage):
### FIELDS ### FIELDS
...@@ -1007,6 +1031,20 @@ class DistrictElectionCampaignPage(DistrictElectionBasePage): ...@@ -1007,6 +1031,20 @@ class DistrictElectionCampaignPage(DistrictElectionBasePage):
def program_points(self): def program_points(self):
return self.get_children().type(DistrictElectionProgramPage).live().specific() return self.get_children().type(DistrictElectionProgramPage).live().specific()
def get_meta_image(self):
return (
self.search_image
or self.hero_candidates_image
or self.hero_image
or self.root_page.get_meta_image()
)
def get_meta_description(self):
if self.search_description:
return self.search_description
return self.hero_motto
class DistrictElectionRootPage(RoutablePageMixin, Page): class DistrictElectionRootPage(RoutablePageMixin, Page):
"""The election root page only serves as a wrapper for sharing stuff among campaign pages. """The election root page only serves as a wrapper for sharing stuff among campaign pages.
...@@ -1093,6 +1131,11 @@ class DistrictProgramPage( ...@@ -1093,6 +1131,11 @@ class DistrictProgramPage(
fill_data_from_redmine_for_page(self) fill_data_from_redmine_for_page(self)
return super().save(**kwargs) return super().save(**kwargs)
def get_meta_description(self):
if self.search_description:
return self.search_description
return self.perex
class DistrictCenterPage( class DistrictCenterPage(
CalendarMixin, ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page CalendarMixin, ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page
...@@ -1169,6 +1212,26 @@ class DistrictCenterPage( ...@@ -1169,6 +1212,26 @@ class DistrictCenterPage(
def has_calendar(self): def has_calendar(self):
return self.calendar_id is not None return self.calendar_id is not None
def get_meta_image(self):
return (
self.search_image
or self.background_photo
or self.root_page.get_meta_image()
)
def get_meta_description(self):
if self.search_description:
return self.search_description
desc = None
if self.perex:
desc = self.perex
elif self.text:
desc = trim_to_length(strip_all_html_tags(self.text))
return desc
class DistrictCrossroadPage( class DistrictCrossroadPage(
ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page ExtendedMetadataPageMixin, SubpageMixin, MetadataPageMixin, Page
...@@ -1404,6 +1467,19 @@ class DistrictGeoFeatureCollectionPage( ...@@ -1404,6 +1467,19 @@ class DistrictGeoFeatureCollectionPage(
return context return context
def get_meta_image(self):
return (
self.search_image
or self.logo_image
or self.image
or self.root_page.get_meta_image()
)
def get_meta_description(self):
if self.search_description:
return self.search_description
return self.perex
class DistrictGeoFeatureCollectionCategory(Orderable): class DistrictGeoFeatureCollectionCategory(Orderable):
name = models.CharField("Název", max_length=100) name = models.CharField("Název", max_length=100)
...@@ -1548,13 +1624,11 @@ class DistrictGeoFeatureDetailPage( ...@@ -1548,13 +1624,11 @@ class DistrictGeoFeatureDetailPage(
return cached_index return cached_index
def get_meta_image(self): def get_meta_image(self):
return self.image return self.search_image or self.image
def get_meta_description(self): def get_meta_description(self):
if self.search_description: if self.search_description:
return self.search_description return self.search_description
if len(self.perex) > 150:
return str(self.perex)[:150] + "..."
return self.perex return self.perex
def get_context(self, request): def get_context(self, request):
......
...@@ -8,6 +8,8 @@ import bleach ...@@ -8,6 +8,8 @@ import bleach
import bs4 import bs4
from django.utils.text import slugify from django.utils.text import slugify
from shared.utils import strip_all_html_tags
from .constants import BENEFITS from .constants import BENEFITS
KNOWN_KEYS = [ KNOWN_KEYS = [
...@@ -202,10 +204,6 @@ def parse_program_html(fp): ...@@ -202,10 +204,6 @@ def parse_program_html(fp):
} }
def strip_html(value):
return bleach.clean(value, tags=[], attributes={}, styles=[], strip=True)
def strip_div(value): def strip_div(value):
return value.replace("<div>", "").replace("</div>", "") return value.replace("<div>", "").replace("</div>", "")
...@@ -233,7 +231,7 @@ def clean_point(point): ...@@ -233,7 +231,7 @@ def clean_point(point):
raise ValueError(f"Unknown key: {old_key}") raise ValueError(f"Unknown key: {old_key}")
if key in ["nadpis"]: if key in ["nadpis"]:
out[key] = strip_html(val) out[key] = strip_all_html_tags(val)
else: else:
out[key] = replace_tags(val) out[key] = replace_tags(val)
...@@ -259,7 +257,7 @@ def prepare_faq(value): ...@@ -259,7 +257,7 @@ def prepare_faq(value):
def prepare_horizon(value): def prepare_horizon(value):
raw = strip_html(value) raw = strip_all_html_tags(value)
m = re.match(r"^(\d+)\s(\w+)$", raw) m = re.match(r"^(\d+)\s(\w+)$", raw)
if m: if m:
return None, m.group(1), m.group(2) return None, m.group(1), m.group(2)
......
...@@ -8,24 +8,11 @@ from wagtail.admin.edit_handlers import ( ...@@ -8,24 +8,11 @@ from wagtail.admin.edit_handlers import (
PublishingPanel, PublishingPanel,
StreamFieldPanel, StreamFieldPanel,
) )
from wagtail.core.blocks import RichTextBlock
from wagtail.core.fields import StreamField from wagtail.core.fields import StreamField
from wagtail.core.models import Page from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.images.edit_handlers import ImageChooserPanel
from maps_utils.blocks import MapFeatureCollectionBlock, MapPointBlock from shared.blocks import DEFAULT_CONTENT_BLOCKS, MenuItemBlock, MenuParentBlock
from shared.blocks import (
DEFAULT_CONTENT_BLOCKS,
CardBlock,
FigureBlock,
GalleryBlock,
MenuItemBlock,
MenuParentBlock,
ThreeColumnBlock,
TwoColumnBlock,
YouTubeVideoBlock,
)
from shared.const import RICH_TEXT_DEFAULT_FEATURES
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -100,13 +87,13 @@ class ArticleMixin(models.Model): ...@@ -100,13 +87,13 @@ class ArticleMixin(models.Model):
return self.get_parent() return self.get_parent()
def get_meta_image(self): def get_meta_image(self):
if hasattr(self, "search_image") and self.search_image:
return self.search_image
return self.image return self.image
def get_meta_description(self): def get_meta_description(self):
if self.search_description: if hasattr(self, "search_description") and self.search_description:
return self.search_description return self.search_description
if len(self.perex) > 150:
return str(self.perex)[:150] + "..."
return self.perex return self.perex
......
import json import json
import logging import logging
import bleach
import requests import requests
from django.conf import settings from django.conf import settings
from django.utils.translation import gettext_lazy from django.utils.translation import gettext_lazy
...@@ -63,3 +64,24 @@ def subscribe_to_newsletter(email, news_id, source): ...@@ -63,3 +64,24 @@ def subscribe_to_newsletter(email, news_id, source):
"Failed to subscribe!", "Failed to subscribe!",
extra={"payload": payload, "response": response.text}, extra={"payload": payload, "response": response.text},
) )
def strip_all_html_tags(value: str):
"""Drop all HTML tags from given value.
:param value: string to sanitize
"""
return bleach.clean(
value, tags=[], attributes=[], styles=[], strip=True, strip_comments=True
)
def trim_to_length(value: str, max_length: int = 150):
"""Trim value to max length shall it exceed it.
:param value: input string
:param max_length: max allowed length
"""
if len(value) > max_length:
return value[:max_length] + "..."
return value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment