From 9a422aef3af5f18f82438d6c5db3423ed7c5f939 Mon Sep 17 00:00:00 2001 From: Ketan <73937490+devketanpro@users.noreply.github.com> Date: Wed, 25 Oct 2023 11:31:20 +0530 Subject: [PATCH] enable urls from rich text in planning xml [STTNHUB-252] (#1870) * enable urls from rich text in planning xml [STTNHUB-252] * addressed comment * Add new method for update the richtext into html * refactore code via black * remove unwanted code --- server/planning/content_profiles/utils.py | 10 ++++++++++ server/planning/feed_parsers/events_ml.py | 12 ++---------- .../planning/feed_parsers/superdesk_planning_xml.py | 7 +++++++ server/planning/feed_parsers/utils.py | 7 +++++++ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/server/planning/content_profiles/utils.py b/server/planning/content_profiles/utils.py index b5ee03682..c753a9df8 100644 --- a/server/planning/content_profiles/utils.py +++ b/server/planning/content_profiles/utils.py @@ -45,3 +45,13 @@ def get_multilingual_fields(resource: str) -> Set[str]: ) ) ) + + +def get_editor3_fields(resource: str) -> Set[str]: + content_type = get_planning_schema(resource) + resource_schema = content_type.get("schema") or {} + return set( + field_name + for field_name, field_schema in resource_schema.items() + if (is_field_enabled(field_name, content_type) and ((field_schema or {})).get("field_type", "") == "editor_3") + ) diff --git a/server/planning/feed_parsers/events_ml.py b/server/planning/feed_parsers/events_ml.py index 2085843f3..528ee3db0 100644 --- a/server/planning/feed_parsers/events_ml.py +++ b/server/planning/feed_parsers/events_ml.py @@ -27,8 +27,7 @@ ) from superdesk.errors import ParserError from superdesk.utc import local_to_utc, utc_to_local -from superdesk.text_utils import plain_text_to_html -from planning.content_profiles.utils import get_planning_schema, is_field_enabled, is_field_editor_3 +from planning.content_profiles.utils import get_planning_schema, is_field_enabled from planning.common import POST_STATE from . import utils @@ -95,7 +94,7 @@ def parse(self, tree: Element, provider=None): self.parse_content_meta(tree, item) self.parse_concept(tree, item) self.parse_event_details(tree, item) - + utils.upgrade_rich_text_fields(item, "event") return [item] except Exception as ex: @@ -155,10 +154,6 @@ def parse_concept(self, tree, item): try: definition = concept.find(self.qname("definition")).text or "" - # if is_field_editor_3("event", "definition_short"): - if is_field_editor_3("definition_short", get_planning_schema("event")): - definition = plain_text_to_html(definition) - item["definition_short"] = definition except Exception: pass @@ -251,9 +246,6 @@ def parse_registration_details(self, event_details, item): registration = event_details.find(self.qname("registration")) if registration is not None and registration.text: registration_details = registration.text - if is_field_editor_3("registration_details", event_type): - registration_details = plain_text_to_html(registration_details) - item["registration_details"] = registration_details except Exception: pass diff --git a/server/planning/feed_parsers/superdesk_planning_xml.py b/server/planning/feed_parsers/superdesk_planning_xml.py index d6fd87ee2..ed41daa53 100644 --- a/server/planning/feed_parsers/superdesk_planning_xml.py +++ b/server/planning/feed_parsers/superdesk_planning_xml.py @@ -21,6 +21,9 @@ POST_STATE, ) +from planning.content_profiles.utils import get_planning_schema +from .utils import upgrade_rich_text_fields + utc = pytz.UTC logger = logging.getLogger(__name__) @@ -91,6 +94,10 @@ def parse(self, tree: Element, provider=None): self.parse_news_coverage_set(tree, item) self.parse_news_coverage_status(tree, item) + upgrade_rich_text_fields(item, "planning") + for coverage in item.get("coverages") or []: + upgrade_rich_text_fields(coverage.get("planning") or {}, "coverage") + return [item] except Exception as ex: diff --git a/server/planning/feed_parsers/utils.py b/server/planning/feed_parsers/utils.py index f8acc6481..7e84f955a 100644 --- a/server/planning/feed_parsers/utils.py +++ b/server/planning/feed_parsers/utils.py @@ -1,6 +1,9 @@ import re import pytz import datetime +from typing import Dict, Any +from planning.content_profiles.utils import get_editor3_fields +from superdesk.text_utils import plain_text_to_html def has_time(value: str): @@ -25,3 +28,7 @@ def parse_duration(value: str) -> datetime.timedelta: if kwargs: return datetime.timedelta(**kwargs) raise ValueError(f"Could not parse duration string {value!r}") + + +def upgrade_rich_text_fields(item: Dict[str, Any], resource: str): + item.update({field: plain_text_to_html(item[field]) for field in get_editor3_fields(resource) if item.get(field)})